在 web.config 中配置 wcf 休息服务 [英] Configuring wcf rest services in web.config

查看:40
本文介绍了在 web.config 中配置 wcf 休息服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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>

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

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

推荐答案

为了配置 WCF REST 服务,您需要在 web.config 文件中添加一些内容

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

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

1) Declare your service and its endpoint

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

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

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) 声明服务行为(通常为默认)

2) Declare the service behavior (usually default)

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

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

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

3) 声明您的端点行为

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.

最后,对于一个简单的 REST 服务来说,web.config 应该是这样的:

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>

这篇关于在 web.config 中配置 wcf 休息服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆