2011-11-09 35 views
6

我想使用WCF 4來設置一個REST風格的Web服務。我希望可以使用HTTP和HTTPS訪問該服務。如何設置HTTP和HTTPS WCF 4 RESTful服務?

<system.serviceModel> 
<behaviors> 
    <endpointBehaviors> 
    <behavior> 
     <webHttp helpEnabled="true" /> 
    </behavior> 
    </endpointBehaviors> 
</behaviors> 
<protocolMapping> 
    <add scheme="http" binding="webHttpBinding" /> 
</protocolMapping> 
</system.serviceModel> 

然後我就可以通過稍微改變配置來此開啓HTTPS的服務:默認情況下,該服務具有以下配置,其適用於HTTP,但沒有使用https創建

<system.serviceModel> 
<behaviors> 
    <endpointBehaviors> 
    <behavior> 
     <webHttp helpEnabled="true" /> 
    </behavior> 
    </endpointBehaviors> 
</behaviors> 
<bindings> 
    <webHttpBinding > 
    <binding name="SecureWebBinding" > 
     <security mode="Transport"></security> 
    </binding> 
    </webHttpBinding> 
</bindings> 
<protocolMapping> 
    <add scheme="http" binding="webHttpBinding" bindingConfiguration="SecureWebBinding"/> 
</protocolMapping> 
</system.serviceModel> 

我問題是我如何才能使用這兩種服務?

回答

6

您應該嘗試創建兩個單獨的端點。例如,

<system.serviceModel> 
    <services> 
     <service name="MyNameSpace.MyService"> 
      <endpoint address="https://www.example.com/MyService.svc" 
        binding="wsHttpBinding" bindingConfiguration="SecureWebBinding" 
        contract="MyNameSpace.IMyContract" /> 
      <endpoint address="http://www.example.com/MyService.svc" 
        binding="basicHttpBinding" 
        contract="MyNameSpace.IMyContract" /> 
     </service> 

     <bindings> 
      <webHttpBinding > 
       <binding name="SecureWebBinding" > 
       <security mode="Transport"></security> 
       </binding> 
      </webHttpBinding> 
     </bindings> 

    </services> 
</system.serviceModel> 
+1

我想它應該是** https **://ww.xyz.com/MyService.svc爲您的示例中首先定義的端點。 –