我是WCF新手并且在WCF中创建了一个rest api服务,我正在关注this tutorial。
这是我的合同代码。
[ServiceContract]
public interface IPayrollRest
{
[OperationContract]
[WebInvoke(BodyStyle = WebMessageBodyStyle.Wrapped, Method = "GET", ResponseFormat = WebMessageFormat.Xml, UriTemplate = "xml/{EmployeeId}")]
string XMLData(string EmployeeId);
[OperationContract]
[WebInvoke(BodyStyle = WebMessageBodyStyle.Wrapped, Method = "GET", ResponseFormat = WebMessageFormat.Json, UriTemplate = "json/{EmployeeId}")]
string JSONData(string EmployeeId);
}
并执行本合同如下
public string XMLData(string EmployeeId )
{
return "Your EmployeeId " + EmployeeId;
}
public string JSONData(string EmployeeId)
{
return "Your EmployeeId " + EmployeeId;
}
我对此服务的配置如下
<system.serviceModel>
<services>
<service behaviorConfiguration="PayrollService.Service1Behavior" name="PayrollService.Service1">
<endpoint address="" binding="wsHttpBinding" contract="PayrollService.IService1">
<identity>
<dns value="localhost"/>
</identity>
</endpoint>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
</service>
<service behaviorConfiguration="PayrollService.PayrollRestBehavior" name="PayrollService.PayrollRest">
<endpoint address="" binding="wsHttpBinding" contract="PayrollService.IPayrollRest">
<identity>
<dns value="localhost"/>
</identity>
</endpoint>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="PayrollService.Service1Behavior">
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
<behavior name="PayrollService.PayrollRestBehavior">
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
当我在网络浏览器中运行我的代码时
**http://localhost:56140/PayrollRest.svc/xml/123**
浏览器中没有任何内容。
答案 0 :(得分:2)
您需要使用webHttpBinding,如文章中所示。 我已使用以下配置对其进行了测试:
<system.serviceModel>
<services>
<service name="PayrollService.Service1Behavior"
behaviorConfiguration="ServiceBehaviour">
<endpoint address="" binding="webHttpBinding"
contract="PayrollService.IPayrollRest"
behaviorConfiguration="web"/>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="ServiceBehaviour">
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="web">
<webHttp/>
</behavior>
</endpointBehaviors>
</behaviors>
</system.serviceModel>