在web.config中配置wcf rest服务

前端之家收集整理的这篇文章主要介绍了在web.config中配置wcf rest服务前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
在web.config中,以下代码块应该用于WCF RESTful服务吗?
  1. <endpoint address="" binding="webHttpBinding"contract="Wcf_Test.IMyService"
  2. behaviorConfiguration="httpEndpointBehavour">
  3. <identity>
  4. <dns value="localhost"/>
  5. <Identity>
  6. </endpoint>

  1. <behaviors>
  2. <serviceBehaviors>
  3. <behavior name="httpBehavIoUr"> <serviceMetadata httpGetEnabled="True"/>
  4. <serviceDebug includeExceptionDetailInFaults="False"/>
  5. </behavior>
  6. </serviceBehaviors>

  1. <endpointBehaviors>
  2. <behavior name="httpEndpointBehavour">
  3. <webHttp />
  4. </behavior>
  5. </endpointBehaviors>
  6. </behaviors>

解决方法

要配置WCF REST服务,您需要在web.config文件中执行一些操作

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

  1. <services>
  2. <service name="SparqlService.SparqlService" behaviorConfiguration="ServiceBehavior">
  3. <endpoint binding="webHttpBinding" contract="SparqlService.ISparqlService"
  4. behaviorConfiguration="webHttp"/>
  5. </service>
  6. </services>

服务名称为[项目名称].[服务名称]
行为配置将与您在下一步中声明的行为相同
绑定必须是webHttpBinding,因为您希望它作为REST.如果需要SOAP,则声明为basicHttpBinding
合同是[项目名称].[接口名称]
端点中的行为配置将是您在下一步中声明的名称

2)声明服务行为(通常是默认)

  1. <behavior name="ServiceBehavior" >
  2. <serviceMetadata httpGetEnabled="true" />
  3. <serviceDebug includeExceptionDetailInFaults="false" />
  4. </behavior>

行为名称可以是任何名称,但它将用于匹配您在步骤1中声明的BehaviorConfiguration
剩下的就是一个人

3)声明您的端点行为

  1. <endpointBehaviors>
  2. <behavior name="webHttp">
  3. <webHttp/>
  4. </behavior>
  5. </endpointBehaviors>

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

最后,这就是web.config对于简单的REST服务应该是什么样子:

  1. <?xml version="1.0"?>
  2. <configuration>
  3.  
  4. <system.web>
  5. <compilation debug="true" targetFramework="4.0" />
  6. </system.web>
  7. <system.serviceModel>
  8.  
  9. <services>
  10. <service name="SparqlService.SparqlService" behaviorConfiguration="ServiceBehavior">
  11. <endpoint binding="webHttpBinding" contract="SparqlService.ISparqlService"
  12. behaviorConfiguration="webHttp"/>
  13. </service>
  14. </services>
  15.  
  16. <behaviors>
  17. <serviceBehaviors>
  18.  
  19. <behavior name="ServiceBehavior" >
  20. <serviceMetadata httpGetEnabled="true" />
  21. <serviceDebug includeExceptionDetailInFaults="false" />
  22. </behavior>
  23.  
  24.  
  25. <behavior>
  26. <!-- To avoid disclosing Metadata information,set the value below to false and remove the Metadata endpoint above before deployment -->
  27. <serviceMetadata httpGetEnabled="true"/>
  28. <!-- 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 -->
  29. <serviceDebug includeExceptionDetailInFaults="false"/>
  30. </behavior>
  31. </serviceBehaviors>
  32.  
  33. <endpointBehaviors>
  34. <behavior name="webHttp">
  35. <webHttp/>
  36. </behavior>
  37. </endpointBehaviors>
  38.  
  39. </behaviors>
  40. </system.serviceModel>
  41. <system.webServer>
  42. <modules runAllManagedModulesForAllRequests="true"/>
  43. </system.webServer>
  44.  
  45. </configuration>

猜你在找的HTML相关文章