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

use*_*704 6 wcf json get endpoint

我已经在.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>
Run Code Online (Sandbox Code Playgroud)

然后,在服务文件中:

namespace MarathonInfo
{
    public class MarathonInfoService : IMarathonInfo
    {
        public String GetData()
        {
            return "Hello World";
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

并在界面中:

namespace MarathonInfo
{
    [ServiceContract]
    public interface IMarathonInfo
    {

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

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

http://localhost:10298/MarathonInfoService.svc/GetData
Run Code Online (Sandbox Code Playgroud)

我收到此错误:

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

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

我究竟做错了什么?

谢谢!

卡西

car*_*ira 15

如果要创建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> 
Run Code Online (Sandbox Code Playgroud)