Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Configuring wcf rest services in web.config

Where in the web.config should the following blocks of code go for a WCF RESTful service?

<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>
</behaviors>
like image 583
user2059013 Avatar asked Jul 14 '13 22:07

user2059013


People also ask

Is it possible to use RESTful services using WCF?

You can use WCF to build RESTful services in . NET. REST (Representational State Transfer) is an architecture paradigm that conforms to the REST architecture principles. The REST architecture is based on the concept of resources: It uses resources to represent the state and functionality of an application.

Is WCF RESTful or SOAP?

By default, Windows Communication Foundation (WCF) makes endpoints available only to SOAP clients.

What is behaviorConfiguration in WCF?

When using a configuration file, behavior configuration is a named collection of configuration settings. The name of each behavior configuration must be unique. This string is used in the behaviorConfiguration attribute of an endpoint configuration to link the endpoint to the behavior.

What is endpoint address in web config?

The endpoint address is represented by the EndpointAddress class, which contains a Uniform Resource Identifier (URI) that represents the address of the service, an Identity, which represents the security identity of the service, and a collection of optional Headers.


2 Answers

In order to configure a WCF REST service, you need a few things in your web.config file

1) Declare your service and its endpoint

<services>
  <service name="SparqlService.SparqlService" behaviorConfiguration="ServiceBehavior">
    <endpoint binding="webHttpBinding" contract="SparqlService.ISparqlService"
              behaviorConfiguration="webHttp"/>
  </service>
</services>

Service name will be [project name].[service name] Behavior configuration will be same name as the behavior you declare in the next step Binding must be webHttpBinding because you want it as REST. If you want SOAP, you declare as basicHttpBinding Contract is the [project name].[interface name] Behavior configuration in the endpoint will be the name you declare in next step

2) Declare the service behavior (usually default)

    <behavior name="ServiceBehavior" >
      <serviceMetadata httpGetEnabled="true" />
      <serviceDebug includeExceptionDetailInFaults="false" />
    </behavior>

Behavior name can be anything, but it will be used to match BehaviorConfiguration you declared in step 1 Leave the rest alone

3) Declare your endpoint behavior

  <endpointBehaviors>
    <behavior name="webHttp">
      <webHttp/>
    </behavior>
  </endpointBehaviors>

Behavior name can be anything, but it will be used to match the behaviorConfiguration in endpoint.

In the end, this is what the 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>
like image 184
Charlie Ou Yang Avatar answered Nov 15 '22 22:11

Charlie Ou Yang


for the rest type 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>
like image 26
Gabbru Avatar answered Nov 15 '22 20:11

Gabbru