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

我已经在.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>

然后,在服务文件中:

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

并在界面中:

namespace MarathonInfo
{
    [ServiceContract]
    public interface IMarathonInfo
    {

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

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

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

我收到此错误

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.

我能够在调试模式下通过Visual Studio很好地执行服务.但在浏览器中,我只得到了这个错误.

我究竟做错了什么?

谢谢!

卡西

解决方法

如果要创建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>

相关文章

前言 做过web项目开发的人对layer弹层组件肯定不陌生,作为l...
前言 前端表单校验是过滤无效数据、假数据、有毒数据的第一步...
前言 图片上传是web项目常见的需求,我基于之前的博客的代码...
前言 导出Excel文件这个功能,通常都是在后端实现返回前端一...
前言 众所周知,js是单线程的,从上往下,从左往右依次执行,...
前言 项目开发中,我们可能会碰到这样的需求:select标签,禁...