如何配置WCF服务以使用https和http

时间:2015-04-24 08:58:02

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

我一直在努力配置通过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方案&#39; http&#39;是无效的;预期&#39; https&#39;。   参数名称:via

我应该对这些配置做出哪些改变,以使这个WCF工作在https&amp; HTTP。

2 个答案:

答案 0 :(得分: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。

答案 1 :(得分:0)

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