WCF Json GET 服务:检查发送方和接收方的 EndpointAddresses 是否一致

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10764348/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 18:21:14  来源:igfitidea点击:

WCF Json GET Service: Check that the sender and receiver's EndpointAddresses agree

jsonwcfgetendpoint

提问by user1418704

I've been working in .NET for a while now, but I'm new to WCF. I'm trying to create my very first WCF service using JSON. I thought I would start really, really simple and then build from there. But I have somehow managed to screw up even the most simple of services. Here's what I've got so far.

我已经在 .NET 中工作了一段时间,但我是 WCF 的新手。我正在尝试使用 JSON 创建我的第一个 WCF 服务。我想我会从非常非常简单的开始,然后从那里开始构建。但我以某种方式设法搞砸了即使是最简单的服务。这是我到目前为止所得到的。

Web.Config:

网络配置:

   <?xml version="1.0"?>
<configuration>

  <system.web>
    <compilation debug="true" targetFramework="4.0" />
  </system.web>
  <system.serviceModel>
    <services>
      <service name="MarathonInfo.MarathonInfoService">
        <endpoint address="http://localhost:10298/MarathonInfoService.svc" binding="webHttpBinding" contract="MarathonInfo.IMarathonInfo" />
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior>
          <!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment -->
          <serviceMetadata httpGetEnabled="true"/>
          <!-- To receive exception details in faults for debugging purposes, set the value below to true.  Set to false before deployment to avoid disclosing exception information -->
          <serviceDebug includeExceptionDetailInFaults="false"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <serviceHostingEnvironment multipleSiteBindingsEnabled="false" />
  </system.serviceModel>
 <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
  </system.webServer>

</configuration>

Then, in the service file:

然后,在服务文件中:

namespace MarathonInfo
{
    public class MarathonInfoService : IMarathonInfo
    {
        public String GetData()
        {
            return "Hello World";
        }
    }
}

And in the interface:

并在界面中:

namespace MarathonInfo
{
    [ServiceContract]
    public interface IMarathonInfo
    {

        [OperationContract]
        [WebInvoke(Method = "GET", UriTemplate = "/GetData", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
        String GetData();
    }
}

So, when I go to this url:

所以,当我去这个网址时:

http://localhost:10298/MarathonInfoService.svc/GetData

I get this error:

我收到此错误:

The message with To 'http://localhost:10298/MarathonInfoService.svc/GetData' cannot be processed at the receiver, due to an AddressFilter mismatch at the EndpointDispatcher. Check that the sender and receiver's EndpointAddresses agree.

由于 EndpointDispatcher 处的 AddressFilter 不匹配,无法在接收方处理带有 To 'http://localhost:10298/MarathonInfoService.svc/GetData' 的消息。检查发送方和接收方的 EndpointAddresses 是否一致。

I am able to execute the service just fine through Visual Studio in debug mode. But in the browser, I only get that error.

我能够在调试模式下通过 Visual Studio 很好地执行服务。但是在浏览器中,我只会收到那个错误。

What am I doing wrong?

我究竟做错了什么?

Thanks!

谢谢!

Casey

凯西

回答by carlosfigueira

If you want to create a WCF WebHTTP Endpoint (i.e., one which returns JSON, and uses the [WebGet] / [WebInvoke] attributes), the endpoint needs to have the <webHttp/>behavior associated with it.

如果要创建 WCF WebHTTP 端点(即,返回 JSON 并使用 [WebGet] / [WebInvoke] 属性的端点),端点需要具有<webHttp/>与其关联的行为。

<system.serviceModel> 
  <services> 
    <service name="MarathonInfo.MarathonInfoService"> 
      <endpoint address="http://localhost:10298/MarathonInfoService.svc"
                binding="webHttpBinding"
                contract="MarathonInfo.IMarathonInfo"
                behaviorConfiguration="Web"/> 
    </service> 
  </services> 
  <behaviors> 
    <serviceBehaviors> 
      <behavior> 
        <serviceMetadata httpGetEnabled="true"/> 
        <serviceDebug includeExceptionDetailInFaults="false"/> 
      </behavior> 
    </serviceBehaviors>
    <endpointBehaviors>
      <behavior name="Web">
        <webHttp/>
      </behavior>
    </endpointBehaviors>
  </behaviors> 
  <serviceHostingEnvironment multipleSiteBindingsEnabled="false" /> 
</system.serviceModel>