Reputation: 10777
I am new to WCF and for creating a rest api service in WCF, I am following this tutorial .
here is my contract code.
[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);
}
and implementation of this contract as follow
public string XMLData(string EmployeeId )
{
return "Your EmployeeId " + EmployeeId;
}
public string JSONData(string EmployeeId)
{
return "Your EmployeeId " + EmployeeId;
}
and my configuration for this service is as follow
<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>
when i run my code in web browser
**http://localhost:56140/PayrollRest.svc/xml/123**
nothing come up in browser.
Upvotes: 0
Views: 653
Reputation: 5944
You need to use webHttpBinding as shown in the article. I have tested it with the following configuration:
<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>
Upvotes: 2