支持HTTP和HTTPS Web.Config WCF服务

时间:2015-06-28 23:03:59

标签: c# web-services wcf azure

在尝试同时支持https和https时,我遇到了WCF服务的配置问题。理想情况下,我喜欢在我的开发机器上运行http,然后发布到运行https的azure。

我按照这些帖子尝试运行配置: http://jayakrishnagudla.blogspot.com/2009/12/configuring-wcf-services-to-work-with.html

How to configure a single WCF Service to have multiple HTTP and HTTPS endpoints?

我的Web.Config如下:

<system.serviceModel>
    <services>
      <service name="Backend.Services.UserService.UserService" behaviorConfiguration="">

        <endpoint address=""
           binding="webHttpBinding"
           contract="Backend.Services.UserService.IUserService"
           bindingConfiguration="HttpsBinding"
           behaviorConfiguration="Web">

        </endpoint>

        <endpoint address=""
                  binding="webHttpBinding"
                  contract="Backend.Services.UserService.IUserService"
                  bindingConfiguration="HttpBinding"
                  behaviorConfiguration="Web"
                  >

        </endpoint>
      </service>
    </services>
      <bindings>
        <webHttpBinding>
          <binding name ="HttpBinding">
            <security mode="None">
              <transport clientCredentialType="None"/>
            </security>
          </binding>

          <binding name="HttpsBinding">
            <security mode="Transport"/>
          </binding>
        </webHttpBinding>

    </bindings>
    <behaviors>

      <serviceBehaviors>
        <behavior name="ServiceBehavior">
          <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="true" />
        </behavior>
      </serviceBehaviors>

      <endpointBehaviors>
        <behavior name="Web">
          <webHttp />
        </behavior>
      </endpointBehaviors>
    </behaviors>
    <protocolMapping>
    </protocolMapping>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
  </system.serviceModel>

据我所知,根据上述链接,此配置应该是正确的。但是,当我通过http在本地构建和运行此服务时,我收到以下错误:

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

我不太确定问题所在,并假设其配置错误。任何帮助,将不胜感激。

3 个答案:

答案 0 :(得分:2)

我一直试图这样做几天(实际上是几年,但几天前刚刚重启),上面的帖子用protocolMapping向我指出了正确的方向,但你需要指定bindingConfiguration部分中的protocolMapping使其选择&lt; security mode = None&gt;或&lt; security mode = Transport&gt;:

<system.serviceModel>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true"/>
  <protocolMapping>
    <add scheme="http"  binding="basicHttpBinding" bindingConfiguration="ServiceBinding"/>
    <add scheme="https" binding="basicHttpBinding" bindingConfiguration="ServiceBindingSSL"/>      
  </protocolMapping>

bindingConfiguration属性保留在端点之外 - 它将根据协议自动设置。然后在绑定部分中设置第二个绑定配置:

        <basicHttpBinding>
            <binding name="ServiceBinding"  ...>
                <security mode="None">
                </security>
            </binding>
            <binding name="ServiceBindingSSL" ... >
                <security mode="Transport">
                </security>
            </binding>
        </basicHttpBinding>

这项技术对我有用。希望它能帮到你!

答案 1 :(得分:1)

基于Rick Moss的回答,我终于使它适用于HTTP和HTTPS。除非我没有在端点上保留bindingConfiguration,因为它没有引起端点侦听错误:

<configuration>
  ...
  <system.serviceModel>
    <services>
      <service name="WcfService1.Service1">
        <endpoint address="" name="ep1" binding="webHttpBinding" bindingConfiguration="ServiceBinding" contract="WcfService1.IService1" behaviorConfiguration="WebBehavior" />
        <endpoint address="" name="ep1" binding="webHttpBinding" bindingConfiguration="ServiceBindingSSL" contract="WcfService1.IService1" behaviorConfiguration="WebBehavior" />
      </service>
    </services>

    <bindings>
      <webHttpBinding>
        <binding name="ServiceBinding" ...>
          <security mode="None">
          </security>
        </binding>
        <binding name="ServiceBindingSSL" ...>
          <security mode="Transport">
            <transport clientCredentialType="None" />
          </security>
        </binding>
      </webHttpBinding>
    </bindings>

    <behaviors>
      <endpointBehaviors>
        <behavior name="WebBehavior">
          <webHttp />
        </behavior>
      </endpointBehaviors>
      ...
    </behaviors>

    <protocolMapping>
        <add scheme="http"  binding="basicHttpBinding" bindingConfiguration="ServiceBinding"/>
        <add scheme="https" binding="basicHttpBinding" bindingConfiguration="ServiceBindingSSL"/>      
    </protocolMapping>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />

  </system.serviceModel>
  ...
</configuration>

编辑:这似乎仅适用于IIS中的匿名身份验证。在IIS中同一网站下的其他Web服务中启用Windows身份验证会导致:

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

我讨厌Web.config文件。他们与IIS的联系太紧密了

答案 2 :(得分:0)

您是否查看了协议映射wcf配置部分?

<protocolMapping>
    <add scheme="http" binding="wsHttpBinding"/>
    <add scheme="https" binding="wsHttpBinding"/>      
</protocolMapping>

编辑:我不确定您为何配置了&#34; https&#34; webHttpBinding类型下的绑定。你是不是既定义了http webHttpBinding又定义了wsHttpBinding并将其分配给你的终端?

<webHttpBinding>
    <binding name ="HttpBinding">
        <security mode="None">
            <transport clientCredentialType="None"/>
        </security>
    </binding>
</webHttpBinding>

<wsHttpBinding>
    <binding name="wsHttpEndpointBinding">
        <security mode="TransportWithMessageCredential">
            <transport clientCredentialType="None" />
            <message clientCredentialType="UserName" />
        </security>
    </binding>
</wsHttpBinding>