【问题标题】:Can't connect to WCF RoutingService via AJAX: "Endpoint not found."无法通过 AJAX 连接到 WCF RoutingService:“找不到端点。”
【发布时间】:2017-03-21 11:20:30
【问题描述】:

我已经设置了 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 上的服务”。

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

【问题讨论】:

    标签: ajax iis wcf-routing


    【解决方案1】:

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

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

    【讨论】:

    • 我想这就是答案!谢谢。
    • 这么长的问题,这么简短的答案 ;-) 如果你觉得有用,别忘了点赞!
    猜你喜欢
    • 2012-07-19
    • 2013-12-22
    • 1970-01-01
    • 1970-01-01
    • 2011-11-26
    • 2015-04-05
    • 2011-12-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多