简体   繁体   中英

How to Configure a WCF service to work over both https and http

I have been fighting with configuring a WCF service hosted in a silverlight 4 application work over both https and http. so far i have only managed to get it work over either http or https but not both. I need it to be called on both.

Below is my full system.serviceModel section in the web.config file.

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

And below is my full 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>


With the above configurations, i'm only able to call it over https, but i also need to be able to call it over http.

When i try to call it over http, i get below error message

The provided URI scheme 'http' is invalid; expected 'https'. Parameter name: via

What change i'm i supposed to make to those configurations to get this WCF thing work over both https & http.

Any specific reason on using a custom binding?

I used the below config settings using basicHttpBinding to have my service running on both Http and https. Based on your authentication mechanism you might have to change settings or bindings.

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

I could browse my service and also see the WSDL from the browser.

You have to give different addresses to the bindings. Both cannot remain the same. In your case those are blank. Since both endpoints are working at the same addresses and in one of them you provided a security therefore WCF is also requiring the other one on the same address to be secured(https)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM