无法通过AJAX连接到WCF RoutingService:“找不到端点”。

时间:2013-05-29 14:08:26

标签: ajax iis wcf-routing

我已经设置了WCF RoutingService的一个实例,以及它要路由到的具体服务。我可以从测试控制台应用程序连接到它,但我得到一个“未找到端点”。我尝试通过AJAX连接时出错。

具体服务的实现如下:

[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";
    }
}

它的App.config文件包含:

<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>

路由服务的Web.config是这样的:

<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>

当我像这样使用SOAP端点调用服务时,它可以正常工作:

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());

但是当我尝试使用此脚本通过AJAX调用它(在与路由服务相同的IIS站点上运行)时,它会失败:

$.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);
        }
});

我得到的回复是这个(用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.

最奇怪的是,当我转到该URL时,它表明我使用Uri“ProcessRequest”来访问“http://localhost:5725/RouterService.svc/webhello/ProcessRequest处的服务”。

任何人都可以看到我在这里做错了吗?

1 个答案:

答案 0 :(得分:0)

仅仅因为WCF-Routing是基于SOAP的,它不起作用。 看看有关WCF-Routing的MSDN article,它说:

  

路由服务当前不支持WCF REST的路由   服务