如何设置HTTP和HTTPS WCF 4 RESTful服务?

Kel*_*len 6 .net c# wcf web-config

我正在尝试使用WCF 4来设置RESTful Web服务.我希望使用HTTP和HTTPS可以访问该服务.默认情况下,使用以下配置创建服务,该配置适用于http但不适用于https:

<system.serviceModel>
 <behaviors>
   <endpointBehaviors>
     <behavior>
       <webHttp helpEnabled="true" />
     </behavior>
   </endpointBehaviors>
 </behaviors>
 <protocolMapping>
   <add scheme="http" binding="webHttpBinding" />
 </protocolMapping>
</system.serviceModel>
Run Code Online (Sandbox Code Playgroud)

然后,我可以通过稍微更改配置为此服务打开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>
Run Code Online (Sandbox Code Playgroud)

我的问题是如何让服务与两者兼容?

Vin*_*ayC 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>
Run Code Online (Sandbox Code Playgroud)