【问题标题】:How to Configure a WCF service to work over both https and http如何配置 WCF 服务以在 https 和 http 上工作
【发布时间】:2015-04-24 08:58:02
【问题描述】:

我一直在努力通过 https 和 http 配置一个托管在 silverlight 4 应用程序中的 WCF 服务。到目前为止,我只设法让它通过 http 或 https 工作,但不能同时通过两者。我需要在两者上都调用它。

下面是我在 web.config 文件中的完整 system.serviceModel 部分。

<system.serviceModel>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true"/>

       <bindings>
        <customBinding>
           <binding name="TestApp.Data.customBinding0">
                <binaryMessageEncoding/>
                <httpTransport maxReceivedMessageSize="2147483647" maxBufferSize="2147483647"/>
            </binding>
            <binding name="TestApp.Data.customBinding0.https">
                <binaryMessageEncoding/>
                <httpsTransport maxReceivedMessageSize="2147483647" maxBufferSize="2147483647"/>
            </binding>
        </customBinding>
    </bindings>

    <services>
        <service name="TestApp.Data" behaviorConfiguration="TestApp.Data">
            <endpoint address="" binding="customBinding" bindingConfiguration="TestApp.Data.customBinding0" contract="TestApp.Data"/>
            <endpoint address="mex" binding="mexHttpBinding" name="" contract="IMetadataExchange"/>

            <endpoint address="" binding="customBinding" bindingConfiguration="TestApp.Data.customBinding0.https" contract="TestApp.Data"/>
            <endpoint address="mexhttp" binding="mexHttpsBinding" contract="IMetadataExchange"/>
        </service>
    </services>

 <behaviors>
        <serviceBehaviors>
            <behavior name="TestApp.Data" >
                <serviceMetadata httpsGetEnabled="true" httpGetEnabled="true"/>
                <serviceDebug includeExceptionDetailInFaults="true"/>
                <dataContractSerializer maxItemsInObjectGraph="2147483646"/>
            </behavior>
            <behavior name="">
                <serviceMetadata httpsGetEnabled="true" httpGetEnabled="true" />
                <serviceDebug includeExceptionDetailInFaults="true"/>
            </behavior>
        </serviceBehaviors>
    </behaviors>

下面是我完整的 ServiceReferences.ClientConfig

<configuration>
<system.serviceModel>
    <bindings>            
        <customBinding>
            <!--http-->
            <binding name="CustomBinding_Data_http">
                <binaryMessageEncoding />
                <httpTransport maxReceivedMessageSize="2147483647" maxBufferSize="2147483647" />
            </binding>
            <!--https-->
            <binding name="CustomBinding_Data">
                <binaryMessageEncoding />
                <httpsTransport maxReceivedMessageSize="2147483647" maxBufferSize="2147483647" />
            </binding>
        </customBinding>

    </bindings>

    <client>
        <endpoint address="//localhost/TestApp/Webservice/Data.svc" binding="customBinding" bindingConfiguration="CustomBinding_Data_http" contract="GetData.Data" name="CustomBinding_Data_http" />
        <endpoint address="//localhost/TestApp/Webservice/Data.svc" binding="customBinding" bindingConfiguration="CustomBinding_Data" contract="GetData.GetData" name="CustomBinding_Data" />            
    </client>


通过上述配置,我只能通过 https 调用它,但我还需要能够通过 http 调用它。

当我尝试通过 http 调用它时,我收到以下错误消息

提供的 URI 方案“http”无效;预期的“https”。 参数名称:via

我应该对这些配置进行哪些更改才能让这个 WCF 东西在 https 和 http 上工作。

【问题讨论】:

  • 我认为在同一个端点上不可能同时拥有 http 和 https。

标签: .net wcf wcf-binding wcf-security service-reference


【解决方案1】:

使用自定义绑定有什么具体原因吗?

我使用以下配置设置使用 basicHttpBinding 让我的服务在 Http 和 https 上运行。根据您的身份验证机制,您可能需要更改设置或绑定。

 <system.serviceModel>
<bindings>
<basicHttpBinding>        
        <binding name="secure">
          <readerQuotas maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="2147483647"
                        maxDepth="64" maxNameTableCharCount="2147483647" />
          <security mode="Transport">                
          </security>
        </binding>
        <binding name="noSecurity">
          <security mode="None"></security>
        </binding> 
      </basicHttpBinding>
     </bindings>
     <services>
        <service name="XMLService.Sample1" behaviorConfiguration="defaultBoth">           
        <endpoint address="unsecure" binding="basicHttpBinding" bindingConfiguration="noSecurity" contract="XMLService.ISample1" />
        <endpoint address="" binding="basicHttpBinding" bindingConfiguration="secure" contract="XMLService.ISample1" />
 </service>
 </services>
 <behaviors>
   <serviceBehaviors>
      <behavior name="defaultBoth">
          <dataContractSerializer maxItemsInObjectGraph="2147483647" />
          <serviceMetadata httpsGetEnabled="true" httpGetEnabled ="true"/>
          <serviceDebug includeExceptionDetailInFaults="true" />
        </behavior>
     </serviceBehaviors> 
    </behaviors>
</system.serviceModel>  

我可以浏览我的服务,也可以从浏览器中查看 WSDL。

【讨论】:

    【解决方案2】:

    您必须为绑定提供不同的地址。两者不能保持不变。在你的情况下,这些是空白的。由于两个端点都在相同的地址上工作,并且您在其中一个中提供了安全性,因此 WCF 还要求同一地址上的另一个端点受到保护(https)

    【讨论】:

    • 如果您想在同一绑定上同时启用 http 和 https,请删除安全端点并配置 IIS。在 IIS 上的 SSL 设置中,确保未选中需要 SSL,您可以看到端点在 http 和 https 中运行
    猜你喜欢
    • 2014-09-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-29
    • 1970-01-01
    • 2017-05-12
    相关资源
    最近更新 更多