Configuring wcf rest services in web.config

Where should the following code blocks for the WCF RESTful service appear in web.config ?

 <endpoint address="" binding="webHttpBinding"contract="Wcf_Test.IMyService" behaviorConfiguration="httpEndpointBehavour"> <identity> <dns value="localhost"/> <Identity> </endpoint> 

and

 <behaviors> <serviceBehaviors> <behavior name="httpBehaviour"> <serviceMetadata httpGetEnabled="True"/> <serviceDebug includeExceptionDetailInFaults="False"/> </behavior></serviceBehaviors> 

and

 <endpointBehaviors> <behavior name="httpEndpointBehavour"> <webHttp /> </behavior> </endpointBehaviors> 
+7
rest web-config wcf
source share
2 answers

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> <!-- 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> <endpointBehaviors> <behavior name="webHttp"> <webHttp/> </behavior> </endpointBehaviors> </behaviors> </system.serviceModel> <system.webServer> <modules runAllManagedModulesForAllRequests="true"/> </system.webServer> </configuration> 
+24
source share

for other types using WCFservice

 <configuration> <system.serviceModel> <services> <service> <-- "place the first code snippet here " it will contain the endpoint details for WCFrestfulServices it will have 'A' ,'B' and 'C' that is address, binding and contract --> </service> </services> <behaviors> <servicebehaviours> <-- "place the second code snippet" the name of the behavior should be the same to that of the behavior configuration attribute value of service tag --> </servicebehaviours> <endpointBehaviors> <-- "place your third code snippet" the name of the behavior should be the same to that of the behavior configuration attribute value of endpoint tag --> </endpointBehaviors> </behaviors> </system.serviceModel> </configuration> 
+1
source share

All Articles