WebHttpBinding with Http and Https

拜拜、爱过 提交于 2019-12-21 07:57:27

问题


I am trying to use https & http for the website. The website has .svc files which act as REST service and called from JavaScript.

My Config:

<system.serviceModel>
    <behaviors>
      <endpointBehaviors>
        <behavior name="AjaxBehavior">
          <webHttp />
        </behavior>
      </endpointBehaviors>
      <serviceBehaviors>
        <behavior name="MyServiceBehaviour">
          <serviceDebug includeExceptionDetailInFaults="true" />
          <serviceMetadata httpGetEnabled="True" httpsGetEnabled="true"/>         
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" />
    <services>
      <service behaviorConfiguration="MyServiceBehaviour" name="MyService.Lookups">
        <endpoint address="" behaviorConfiguration="AjaxBehavior"
          binding="webHttpBinding" bindingConfiguration="httpWebBinding" contract="MyService.Lookups" >         
        </endpoint>
        <endpoint address="" behaviorConfiguration="AjaxBehavior"
          binding="webHttpBinding" bindingConfiguration="httpsWebBinding" contract="MyService.Lookups" >          
        </endpoint>
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
      </service>
    </services>
    <bindings>      
      <webHttpBinding>
        <binding name="httpsWebBinding">
          <security mode="Transport">
            <transport clientCredentialType="None" proxyCredentialType="None" />
          </security>
        </binding>
        <binding name="httpWebBinding">
          <security mode="None">
            <transport clientCredentialType="None" proxyCredentialType="None" />
          </security>
        </binding>
      </webHttpBinding>
    </bindings>   
  </system.serviceModel>

Browsing https://myserver/services/Lookups.svc/Hello gives

Could not find a base address that matches scheme http for the endpoint with binding WebHttpBinding. Registered base address schemes are [https]

Browsing http://myserver/services/Lookups.svc/Hello gives

Could not find a base address that matches scheme https for the endpoint with binding WebHttpBinding. Registered base address schemes are [http]

If I remove any one endpoint it works. Example removing endpoint configured with bindingConfiguration="httpWebBinding" works for HTTPS ,

How can I make it work with HTTP and HTTPS? As of now, I can able to use either http or https by removing one endpoint.

Referred How can I combine the WCF services config for both http and https in one web.config? and How do you setup HTTP and HTTPS WCF 4 RESTful services?

Note: In IIS, it is two web sites one listen on http and another on https. Both sharing same code in physical folder

UPDATE: As of now, I removed endpoints and it works. But my concern is removing endpoing configured with behaviourConfiguration doesnt look great solution to me.

This works for both http & https

  <services>
      <service behaviorConfiguration="MyServiceBehaviour" name="MyService.Lookups">

      </service>
    </services>

回答1:


I've recreated your scenario and used your web.config to configure endpoints for my test service. Your configuration is ok and works correctly. The part that don't works for you is probably your https configuration in IIS. Make sure you have enabled https access to your service. If you test it with IISExpress from Visual Studio then left click on your project and in the properties window (View -> Properties Window ) select for SSL Enabled = True.




回答2:


As @Lesmian pointed out in his answer, the issue is in your IIS configuration. More specifically in:

Note: In IIS, it is two web sites one listen on http and another on https. Both sharing same code in physical folder

The reason is that IIS can not handle endpoints on a schema which it does not support.

You have two sites, and one of them has HTTP binding but does not has HTTPS, and the other has HTTPS but not HTTP.

So when you browse to http:// URL, IIS directs you to the (surprise!) http-enabled site, reads web.config, sees that it registers https endpoint (which is not supported by the site) and throws the exception telling that there is no https scheme support on the http-enabled-only site.

When you browse to the https:// URL the situation is similar - IIS does not allow you to use http endpoint on the https-enabled-only site.

To handle the issue you better use a single site with two bindings.

Another (and more complex) option would be using different web.configs for sites: set up separate sites (pointed to separate folders) and use the publishing and web.config transforming tools of the Visual Studio




回答3:


Add This Code.

<protocolMapping>
  <add scheme="http"  binding="webHttpBinding" bindingConfiguration="httpWebBinding"/>
  <add scheme="https" binding="webHttpBinding" bindingConfiguration="httpsWebBinding"/>
</protocolMapping>



回答4:


If you update your endpoint configs like such:

        <endpoint address="http://myserver/services/Lookups.svc" behaviorConfiguration="AjaxBehavior"
          binding="webHttpBinding" bindingConfiguration="httpWebBinding" contract="MyService.Lookups" >         
        </endpoint>
        <endpoint address="https://myserver/services/Lookups.svc" behaviorConfiguration="AjaxBehavior"
          binding="webHttpBinding" bindingConfiguration="httpsWebBinding" contract="MyService.Lookups" >          
        </endpoint>

I have done a similar dance with a prod system, and this was my solution. Hope it works for you as well.




回答5:


I dont't know this query is still active or not but as i checked Please

Add binding="mexHttpsBinding" also with binding="mexHttpBinding" with different endpoint

This helps me.



来源:https://stackoverflow.com/questions/36578047/webhttpbinding-with-http-and-https

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