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