2011-11-09 38 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为您的示例中首先定义的端点。 –