在web.config中配置wcf rest服务

use*_*013 8 rest wcf web-config

web.config应该在以下哪个代码块中使用WCF RESTful服务?

<endpoint address="" binding="webHttpBinding"contract="Wcf_Test.IMyService"    
behaviorConfiguration="httpEndpointBehavour"> 
    <identity> 
        <dns value="localhost"/> 
    <Identity>  
</endpoint>
Run Code Online (Sandbox Code Playgroud)

<behaviors>
    <serviceBehaviors> 
        <behavior name="httpBehaviour"> <serviceMetadata httpGetEnabled="True"/>
            <serviceDebug includeExceptionDetailInFaults="False"/>
        </behavior>
    </serviceBehaviors>
Run Code Online (Sandbox Code Playgroud)

    <endpointBehaviors> 
        <behavior name="httpEndpointBehavour"> 
            <webHttp />
        </behavior> 
    </endpointBehaviors>
</behaviors>
Run Code Online (Sandbox Code Playgroud)

小智 27

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

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

<services>
  <service name="SparqlService.SparqlService" behaviorConfiguration="ServiceBehavior">
    <endpoint binding="webHttpBinding" contract="SparqlService.ISparqlService"
              behaviorConfiguration="webHttp"/>
  </service>
</services>
Run Code Online (Sandbox Code Playgroud)

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

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

    <behavior name="ServiceBehavior" >
      <serviceMetadata httpGetEnabled="true" />
      <serviceDebug includeExceptionDetailInFaults="false" />
    </behavior>
Run Code Online (Sandbox Code Playgroud)

行为名称可以是任何内容,但它将用于匹配您在步骤1中声明的BehaviorConfiguration,其余部分保持不变

3)声明您的端点行为

  <endpointBehaviors>
    <behavior name="webHttp">
      <webHttp/>
    </behavior>
  </endpointBehaviors>
Run Code Online (Sandbox Code Playgroud)

行为名称可以是任何名称,但它将用于匹配端点中的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>
Run Code Online (Sandbox Code Playgroud)

  • 这篇文章对初学者很有用.如果有关于约束及其含义的解释会更好.无论如何,谢谢. (4认同)