ajaxiiswcf-routing

Can't connect to WCF RoutingService via AJAX: "Endpoint not found."


I've set up an instance of the WCF RoutingService, plus a concrete service for it to route to. I can connect to it fine from a test console app, but I get an "Endpoint not found." error when I try to connect via AJAX.

The concrete service's implementation looks like this:

[ServiceContract]
public interface IHelloService
{
    [OperationContract]
    string SayHi();
}

public class HelloService : IHelloService
{
    [WebInvoke(Method = "POST", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
    public string SayHi()
    {
        return "Hello World";
    }
}

And its App.config file contains this:

<system.serviceModel>
    <services>
        <service name="Rashim.RND.WCFRouting.HelloWorldService.HelloService">
            <host>
                <baseAddresses>
                    <add baseAddress="http://localhost:7222/HelloWorldService/HelloService/"/>
                </baseAddresses>
            </host>
            <endpoint address="/soap" binding="wsHttpBinding" contract="Rashim.RND.WCFRouting.HelloWorldService.IHelloService"/>
            <endpoint address="/web" kind="webHttpEndpoint" contract="Rashim.RND.WCFRouting.HelloWorldService.IHelloService"/>
            <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
        </service>
    </services>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>
    <standardEndpoints>
        <webHttpEndpoint>
            <standardEndpoint helpEnabled="true" automaticFormatSelectionEnabled="true" crossDomainScriptAccessEnabled="true"/>
        </webHttpEndpoint>
    </standardEndpoints>
    <behaviors>
        <serviceBehaviors>
            <behavior>
                <serviceMetadata httpGetEnabled="True"/>
                <serviceDebug includeExceptionDetailInFaults="True"/>
            </behavior>
        </serviceBehaviors>
    </behaviors>
</system.serviceModel>

The routing service's Web.config is like this:

<system.serviceModel>
    <behaviors>
        <serviceBehaviors>
            <behavior name="routingBehavior">
                <routing routeOnHeadersOnly="false" filterTableName="filters"/>
                <serviceDebug includeExceptionDetailInFaults="true"/>
                <serviceMetadata httpGetEnabled="true"/>
            </behavior>
        </serviceBehaviors>
    </behaviors>
    <routing>
        <filters>
            <filter name="SoapHelloServiceFilter" filterType="EndpointName" filterData="SoapHelloServiceIn"/>
            <filter name="WebHelloServiceFilter" filterType="EndpointName" filterData="WebHelloServiceIn"/>
        </filters>
        <filterTables>
            <filterTable name="filters">
                <add filterName="SoapHelloServiceFilter" endpointName="SoapHelloServiceOut"/>
                <add filterName="WebHelloServiceFilter" endpointName="WebHelloServiceOut"/>
            </filterTable>
        </filterTables>
    </routing>
    <services>
        <service name="System.ServiceModel.Routing.RoutingService" behaviorConfiguration="routingBehavior">
            <endpoint address="/soapHello" binding="wsHttpBinding" contract="System.ServiceModel.Routing.IRequestReplyRouter" name="SoapHelloServiceIn"/>
            <endpoint address="/webHello" kind="webHttpEndpoint" contract="System.ServiceModel.Routing.IRequestReplyRouter" name="WebHelloServiceIn"/>
            <host>
                <baseAddresses>
                    <add baseAddress="http://localhost:7111/RouterService.svc"/>
                </baseAddresses>
            </host>
        </service>
    </services>
    <client>
        <endpoint address="http://localhost:7222/HelloWorldService/HelloService/soap" binding="wsHttpBinding" contract="*" name="SoapHelloServiceOut"/>
        <endpoint address="http://localhost:7222/HelloWorldService/HelloService/web" binding="wsHttpBinding" contract="*" name="WebHelloServiceOut"/>
    </client>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>
    <standardEndpoints>
        <webHttpEndpoint>
            <standardEndpoint helpEnabled="true" automaticFormatSelectionEnabled="true" crossDomainScriptAccessEnabled="true"/>
        </webHttpEndpoint>
    </standardEndpoints>
</system.serviceModel>

When I call the service using the SOAP endpoint like this, it works fine:

string address = "http://localhost:7111/RouterService.svc/soapHello";
Console.WriteLine("Address: {0}", address);

var binding = new WSHttpBinding();
var endpoint = new EndpointAddress(address);
var proxy = ChannelFactory<IHelloService>.CreateChannel(binding, endpoint);
Console.WriteLine("Reply from server: " + proxy.SayHi());

But when I try to call it via AJAX with this script (running on the same IIS site as the routing service), it fails:

$.ajax(
{
    type: 'POST',
    url: "RouterService.svc/webHello/SayHi",
    data: null,
    contentType: "application/json; charset=utf-8",
    success:
        function(msg)
        {
            alert(msg);
        },
    error:
        function(xhr, ajaxOptions, thrownError)
        {
            alert('Error ' + thrownError + '. Response: ' + xhr.responseText);
        }
});

The response I get back is this (wrapped in HTML):

Endpoint not found. Please see the <a rel="help-page" href="http://localhost:7111/RouterService.svc/webHello/help">service help page</a> for constructing valid requests to the service.

And most strangely, when I go to that URL, it suggests that I use the Uri "ProcessRequest" to access "Service at http://localhost:5725/RouterService.svc/webhello/ProcessRequest".

Can anyone see what it is I'm doing wrong here?


Solution

  • It won't work just because WCF-Routing is SOAP based. Have a look at this MSDN article about WCF-Routing, it says:

    The Routing Service does not currently support routing of WCF REST services