How do you setup HTTP and HTTPS WCF 4 RESTful services?

时光总嘲笑我的痴心妄想 提交于 2020-01-01 10:00:50

问题


I'm trying to use WCF 4 to set up a RESTful web service. I'd like the service to be accessible both using HTTP and HTTPS. By default the service is created with the following configuration which works for http but not https:

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

I can then turn on HTTPS for the service by changing the configuration slightly to this:

<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>

My question is how do I get the service working with both?


回答1:


You should try to create two separate end-points. For example,

<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>


来源:https://stackoverflow.com/questions/8059848/how-do-you-setup-http-and-https-wcf-4-restful-services

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!