2013-07-14 135 views
7

web.config应该在以下代码块中使用WCF RESTful服务吗?在web.config中配置wcf rest服务

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

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

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

使用WCF SVC配置编辑器中的其余类型。 – avi

回答

24

为了配置一个WCF REST服务,你需要在你的web.config文件中的几件事情

1)声明您的服务和其端点

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

服务名称为[项目名称]。[服务名称] 行为的配置将是相同的名称为您在下一步 申报行为绑定必须的WebHttpBinding,因为你希望它是REST。如果你想SOAP,您声明为basicHttpBinding的 合同是[项目名称]。[接口名称]在端点 行为的配置将是您在下一步

2)声明服务行为申报的名称(通常是默认)

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

行为名称可以是任何东西,但它会被用来匹配BehaviorConfiguration您在步骤1中声明保留 仅剩下

3)声明你的终结点行为

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

行为名称可以是任何内容,但它将用于匹配端点中的behaviorConfiguration。

最后,这是在web.config应该是什么样子的一个简单的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

这篇文章对初学者很有用。如果有关于绑定及其含义的解释会更好。无论如何,谢谢。 –

+0

任何基本的HTTP绑定? – AskMe

1

使用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>