WCF使用SOAP和REST仅适用于某些方法

时间:2017-11-08 13:45:43

标签: wcf

我有一个带有SOAP端点的WCF服务,并且我使用以下代码/ config成功地公开了这两个方法:

服务合同

[ServiceContract]
public interface IService
{
    [OperationContract]
    string TestConn();

    [OperationContract]
    string AddRecord();
}

的Web.config

    <behaviors>
      <serviceBehaviors>
        <behavior name="ServiceBehavior">
          <serviceMetadata httpGetEnabled="True"/>
          <serviceDebug includeExceptionDetailInFaults="true" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <services>
      <service name="WSADPITG.Service" behaviorConfiguration="ServiceBehavior">
        <endpoint binding="basicHttpBinding" contract="WSADPITG.IService" address="" name="WSADPITG" />
        <endpoint binding="mexHttpBinding" contract="IMetadataExchange" address="mex" />
      </service>
    </services>

是否可以添加REST端点以公开 ONLY TestConn方法?代码/配置有什么变化?

1 个答案:

答案 0 :(得分:1)

您应该分开合同,因为单个服务可以有单一行为。因此,两个服务应该在您的单个web.config文件中托管。

WCF REST服务

 [ServiceContract]
    public interface IService
    {
        [OperationContract]
        [WebGet]
        string TestConn();

    }

现在是配置文件。

 <system.serviceModel>    
        <behaviors>    
          <serviceBehaviors >    
            <behavior name="ServiceBehavior">    
              <!-- To avoid disclosing metadata information, set the values below to false 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="web">                
              <webHttp/>                
            </behavior>                
          </endpointBehaviors>    

        </behaviors>    
       <services>    
      <service name="ServiceClassNamespace.YourServiceClasImplementation" behaviorConfiguration="ServiceBehavior">   
        <endpoint binding="webHttpBinding" contract="ServiceClassNamespace.IService" behaviorConfiguration="web">    
        </endpoint>    
      </service>            
    </services>    

    <serviceHostingEnvironment  multipleSiteBindingsEnabled="true" />    
  </system.serviceModel> 

纯WCF服务

[ServiceContract]
public interface IService2
{

    [OperationContract]
    string AddRecord();
}

上面提到的web.config必须附加到现有的(WCF REST)配置文件中。

<system.serviceModel>    
            <behaviors>    
              <serviceBehaviors >    
                <behavior name="ServiceBehavior">    
                  <!-- To avoid disclosing metadata information, set the values below to false 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="web">                
                  <webHttp/>                
                </behavior>                
              </endpointBehaviors>    

            </behaviors>    
           <services>    
          <service name="ServiceClassNamespace.YourServiceClasImplementation" behaviorConfiguration="ServiceBehavior">   
            <endpoint binding="webHttpBinding" contract="ServiceClassNamespace.IService" behaviorConfiguration="web">    
            </endpoint>    
          </service>            
          <service name="WSADPITG.Service" behaviorConfiguration="ServiceBehavior">
        <endpoint binding="basicHttpBinding" contract="WSADPITG.IService2" address="" name="WSADPITG" />
        <endpoint binding="mexHttpBinding" contract="IMetadataExchange" address="mex" />
        <host>

      </service>
        </services>    

        <serviceHostingEnvironment  multipleSiteBindingsEnabled="true" />    
      </system.serviceModel>