2013-07-14 3 views
7

Dove nel web.config devono essere utilizzati i seguenti blocchi di codice per un servizio RESTful WCF?Configurazione dei servizi wcf rest in web.config

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

e

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

e

<endpointBehaviors> 
<behavior name="httpEndpointBehavour"> 
<webHttp /> </behavior> 
</endpointBehaviors> 
+1

Usa WCF editor di SVC config. – avi

risposta

24

Al fine di configurare un servizio WCF REST, avete bisogno di un paio di cose nel file web.config

1) dichiarare il tuo servizio e il suo endpoint

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

Il nome del servizio sarà [nome progetto]. [Nome servizio] La configurazione del comportamento sarà lo stesso del comportamento dichiarato nel passaggio successivo. Il binding deve essere webHttpBinding perché lo si desidera come REST. Se si vuole SOAP, si dichiara come basicHttpBinding contratto è il [nome del progetto]. [Nome dell'interfaccia] Configurazione Comportamento nell'endpoint sarà il nome si dichiara nel prossimo passo

2) Dichiarare il comportamento di servizio (di solito di default)

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

nome comportamento può essere qualsiasi cosa, ma sarà utilizzato per corrispondere BehaviorConfiguration dichiarato nel passaggio 1 lasciare il resto da sola

3) dichiarare il vostro comportamento endpoint

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

Il nome del comportamento può essere qualsiasi cosa, ma verrà utilizzato per abbinare il comportamentoConfigurazione in endpoint.

Alla fine, questo è ciò che il web.config dovrebbe essere simile per un semplice servizio REST:

<?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> 
+3

Questo post è utile per i principianti. Sarebbe meglio se ci fosse una spiegazione sui legami e le loro implicazioni. Comunque, grazie. –

+0

Qualcosa per il binding HTTP di base? – AskMe

1

per il tipo di riposo utilizzando 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>