【问题标题】:Configuring wcf rest services in web.config在 web.config 中配置 wcf rest 服务
【发布时间】:2013-07-14 22:22:43
【问题描述】:

在web.config 中,以下代码块应该用于 WCF RESTful 服务吗?

<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 SVC 配置编辑器。

标签: wcf rest web-config


【解决方案1】:

为了配置 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>

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

3) 声明您的端点行为

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

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

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

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

【讨论】:

【解决方案2】:

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

【讨论】:

    【解决方案3】:

    网络配置更改。

    <system.serviceModel>
        <services>
          <service name="WcfService1.Service1">
            <endpoint address="" behaviorConfiguration="restbehavior" binding="webHttpBinding" bindingConfiguration=""
                      contract ="WcfService1.IBookService">          
            </endpoint>
            <host>
              <baseAddresses>
                <add baseAddress="http://localhost/bookservice"/>
              </baseAddresses>
            </host>                
          </service>
        </services>
        <behaviors>
          <endpointBehaviors>
            <behavior name="restbehavior">
              <webHttp/>
            </behavior>
          </endpointBehaviors>
        </behaviors>   
      </system.serviceModel>
    

    接口:我们必须使用 WebGet 来进行 httpGet / WebInvoke 来进行 HttpPost & Put & Delete。

     [ServiceContract]
        public interface IBookService
        {
            [OperationContract]
            [WebGet]
            List<BOOK> GetBooksList();
    
            [OperationContract]
            [WebGet(UriTemplate = "Book/{id}")]
            BOOK GetBookById(string id);
    
            [OperationContract]
            [WebInvoke(UriTemplate = "AddBook/{name}")]
            void AddBook(string name);
    }
    

    参考:https://www.codeproject.com/Articles/571813/A-Beginners-Tutorial-on-Creating-WCF-REST-Services

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-06-23
      • 2019-07-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-29
      • 1970-01-01
      相关资源
      最近更新 更多