0

我已经设置了 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”来访问“Service at http://localhost:5725/RouterService.svc/webhello/ProcessRequest”。

谁能看到我在这里做错了什么?

4

1 回答 1

0

它不会仅仅因为 WCF-Routing 是基于 SOAP 的。看看这篇关于 WCF-Routing 的MSDN 文章,它说:

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

于 2013-05-29T14:30:50.410 回答