To set up a REST service for WCF, you need a few things in the web.config file
1) Announce your service and its endpoint
<services> <service name="SparqlService.SparqlService" behaviorConfiguration="ServiceBehavior"> <endpoint binding="webHttpBinding" contract="SparqlService.ISparqlService" behaviorConfiguration="webHttp"/> </service> </services>
The service name will be [project name]. [service name] The behavior configuration will be the same name as the behavior you declare in the next step. Binding must be webHttpBinding because you want it as REST. If you want to use SOAP, you declare basicHttpBinding The contract is [project name]. [Interface Name] The endpoint behavior configuration will be the name that you declare in the next step.
2) Declare the behavior of the service (usually the default)
<behavior name="ServiceBehavior" > <serviceMetadata httpGetEnabled="true" /> <serviceDebug includeExceptionDetailInFaults="false" /> </behavior>
The behavior name can be anything, but it will be used to match the BehaviorConfiguration that you specified in step 1. Leave the rest alone
3) Declare your endpoint behavior
<endpointBehaviors> <behavior name="webHttp"> <webHttp/> </behavior> </endpointBehaviors>
The behavior name can be anything, but it will be used to match the behavior configuration at the endpoint.
In the end, this is what web.config should look like for a simple REST service:
<?xml version="1.0"?> <configuration> <system.web> <compilation debug="true" targetFramework="4.0" /> </system.web> <system.serviceModel> <services> <service name="SparqlService.SparqlService" behaviorConfiguration="ServiceBehavior"> <endpoint binding="webHttpBinding" contract="SparqlService.ISparqlService" behaviorConfiguration="webHttp"/> </service> </services> <behaviors> <serviceBehaviors> <behavior name="ServiceBehavior" > <serviceMetadata httpGetEnabled="true" /> <serviceDebug includeExceptionDetailInFaults="false" /> </behavior> <behavior> <serviceMetadata httpGetEnabled="true"/> <serviceDebug includeExceptionDetailInFaults="false"/> </behavior> </serviceBehaviors> <endpointBehaviors> <behavior name="webHttp"> <webHttp/> </behavior> </endpointBehaviors> </behaviors> </system.serviceModel> <system.webServer> <modules runAllManagedModulesForAllRequests="true"/> </system.webServer> </configuration>
Charlie ou yang
source share