c# - WebHttpBinding 与 Http 和 Https

标签 c# asp.net web-services wcf webhttpbinding

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

我的配置:

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

浏览 https://myserver/services/Lookups.svc/Hello

找不到与具有绑定(bind) WebHttpBinding 的端点的方案 http 相匹配的基址。注册的基地址方案是 [https]

浏览 http://myserver/services/Lookups.svc/Hello

找不到与具有绑定(bind) WebHttpBinding 的端点的方案 https 相匹配的基址。注册的基地址方案是 [http]

如果我删除任何一个端点,它就会起作用。删除使用 bindingConfiguration="httpWebBinding" 配置的端点的示例适用于 HTTPS,

如何让它与 HTTP 和 HTTPS 一起工作?到目前为止,我可以通过删除一个端点来使用 http 或 https。

引用How can I combine the WCF services config for both http and https in one web.config?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

更新:截至目前,我删除了端点并且它有效。但我担心的是删除配置了 behaviorConfiguration 的端点对我来说并不是很好的解决方案。

这适用于 http 和 https

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

      </service>
    </services>

最佳答案

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

关于c# - WebHttpBinding 与 Http 和 Https,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36578047/

相关文章:

c# - 扩展方法的缓存结果

java - prb 和我的 json 响应

java - 监控应用程序 Web 服务请求和响应时间

c# - MSBUILD 引发错误 : The SDK 'Microsoft.NET.Sdk' specified could not be found

c# - Nhibernate 和枚举

css - Ajax:Calendar - 日历的内容很小,我无法看到所有的日子

javascript - ASP.Net Webforms : Can a RequiredFieldValidator be listened to?(事件)

jquery - angularjs - 预检响应中的 Access-Control-Allow-Headers 不允许请求 header 字段

php - 如何在没有Flash的情况下从网站上录制用户的声音?

c# - 什么会导致应用程序的 RAM 使用率线性增长?