C# 使用Http和Https的WebHttpBinding

C# 使用Http和Https的WebHttpBinding,c#,asp.net,web-services,wcf,webhttpbinding,C#,Asp.net,Web Services,Wcf,Webhttpbinding,我正在尝试使用https和http的网站。该网站有.svc文件,这些文件充当REST服务并从JavaScript调用 我的配置: <system.serviceModel> <behaviors> <endpointBehaviors> <behavior name="AjaxBehavior"> <webHttp /> </behavior>

我正在尝试使用https和http的网站。该网站有.svc文件,这些文件充当REST服务并从JavaScript调用

我的配置:

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

我已经重新创建了您的场景,并使用您的web.config为我的测试服务配置端点。您的配置正常,工作正常。不适合您的部分可能是IIS中的https配置。确保已启用对服务的https访问。如果您使用Visual Studio中的IISExpress进行测试,然后左键单击您的项目,并在属性窗口(视图->属性窗口)中选择SSL Enabled=True。

正如@Lesmian在其回答中指出的,问题在于您的IIS配置。 具体而言:

注意:在IIS中,有两个网站,一个在http上侦听,另一个在https上侦听。两者在物理文件夹中共享相同的代码

原因是IIS无法处理其不支持的架构上的终结点

您有两个站点,其中一个有HTTP绑定但没有HTTPS,另一个有HTTPS但没有HTTP

因此,当您浏览到http://URL时,IIS会将您引导到(令人惊讶的!)启用http的站点,读取web.config,看到它注册了https端点(该站点不支持此端点)并抛出异常,告知仅启用http的站点上不支持https方案

浏览到https://URL时,情况类似-IIS不允许您在仅启用https的站点上使用http端点

要处理这个问题,最好使用一个带有两个绑定的站点

另一个(更复杂的)选项是为站点使用不同的web.config:设置单独的站点(指向单独的文件夹),并使用Visual Studio的发布和web.config转换工具添加此代码

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

我不知道此查询是否仍处于活动状态,但请检查

同时添加
binding=“mexHttpsBinding”
使用具有不同端点的
binding=“mexHttpBinding”


这对我很有帮助。

对于
httpsGetEnabled=“true”
必须有
https
url,但您尚未定义任何url。是的。在服务器中,我们创建了
https
绑定并放置了一个自签名证书。由于我们没有使用相互身份验证,我们没有在IIS 8.5的SSL设置中启用
RequireSSL
标志
<protocolMapping>
  <add scheme="http"  binding="webHttpBinding" bindingConfiguration="httpWebBinding"/>
  <add scheme="https" binding="webHttpBinding" bindingConfiguration="httpsWebBinding"/>
</protocolMapping>