WCF服务中的web.config所需的更改以支持Rest

时间:2014-11-25 21:45:48

标签: c# wcf rest

我有一个与Soap一起使用的WCF服务。我也需要这个休息。我在方法中添加了WebGet属性,如下所示:

[OperationContract]
[WebGet(UriTemplate = "login/{username}/{password}/{PartnerID}")]
Model.PartnerAuthentication authenticate(Model.PartnerRequest request);

但是当我在配置文件中更改与rest相关的一些配置时,我收到错误。不确定我的配置与休息是否正确:

这是我的web.config

<services>
    <service behaviorConfiguration="HubBehavior" name="Acquisition.AcquisitionService">
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
        <endpoint 
            binding="basicHttpBinding" bindingConfiguration="basicBinding"
            contract="Acquisition.IAcquisition" />
        <endpoint name="wsEndpoint" 
            address="ws" 
            binding="wsHttpBinding" bindingConfiguration="wsBinding"
            contract="Acquisition.IAcquisition" />
        <endpoint 
            address="web"  
            behaviorConfiguration="restfulBehavior" 
            binding="webHttpBinding"     
            contract="Acquisition.IAcquisition"/>
    </service>
</services>

与休息和行为相关的最后一个终点是:

<behaviors>
    <endpointBehaviors>
        <behavior name="restfulBehavior">
            <webHttp/>
        </behavior>
    </endpointBehaviors>
    <serviceBehaviors>
        <behavior name="HubBehavior">
            <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
            <serviceDebug includeExceptionDetailInFaults="true"/>
        </behavior>
    </serviceBehaviors>
</behaviors>

1 个答案:

答案 0 :(得分:0)

以下是您可以使用的示例代码段。在这里,我添加了您必须在服务类中实现的示例方法,然后我为该WCF服务添加了Web配置。希望这会帮助你解决 你的问题。

    [OperationContract]
    [WebInvoke(Method = "GET",
    ResponseFormat = WebMessageFormat.Xml,
    BodyStyle = WebMessageBodyStyle.Wrapped,
    UriTemplate = "/GetNameXML?name={name}")]
    string GetNameXML(string name); 


<system.serviceModel>  
   <services>  
    <service name="WcfService1.TestService" behaviorConfiguration="ServiceBehaviour">  
     <endpoint address="" binding="webHttpBinding" contract="WcfService1.ITestService" behaviorConfiguration="web">  
     </endpoint>  
    </service>  
   </services>  
   <behaviors>  
    <serviceBehaviors>  
     <behavior name="ServiceBehaviour">  
      <serviceMetadata httpGetEnabled="true"/>  
      <serviceDebug includeExceptionDetailInFaults="false"/>  
     </behavior>  
    </serviceBehaviors>  
    <endpointBehaviors>  
     <behavior name="web">  
      <webHttp/>  
     </behavior>  
    </endpointBehaviors>  
   </behaviors>  
  </system.serviceModel>