如何配置WCF服务以使用HTTP和HTTPS

时间:2014-07-31 13:31:25

标签: web-services wcf service-model

Iam尝试通过silverlight 4应用程序配置https和http访问服务。我可以通过https而不是http来访问服务。我已经对网络进行了一些研究,但似乎无法正确配置。

以下是我的web.config文件中的当前设置。

    <system.serviceModel>

    <bindings>
      <customBinding>
          <binding name="MyhttpsBinding">
          <binaryMessageEncoding/>
          <httpsTransport/> 
          </binding>
          <binding name="MyhttpBinding">
              <binaryMessageEncoding/>
              <httpTransport/>
          </binding>
      </customBinding>
    </bindings>

    <services>
      <service name="MyData" behaviorConfiguration="MyData">
        <endpoint address="" binding="customBinding" bindingConfiguration="MyData.customBinding.https" contract="MyData"/>
        <endpoint address="" binding="customBinding" bindingConfiguration="MyData.customBinding.http" contract="MyData"/>
        <endpoint address="mex" binding="mexHttpsBinding" contract="IMetadataExchange"/>
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
      </service>
    </services>

    <behaviors>
      <serviceBehaviors>
        <behavior name="MyData" >
          <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>

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

以下是我的ServiceReferences.ClientConfig文件

<configuration>
<system.serviceModel>
    <bindings>
        <basicHttpBinding>
            <binding name="DataS" maxBufferSize="2147483647" maxReceivedMessageSize="2147483647">
                <security mode="None" />
            </binding>
            <binding name="DataS1" maxBufferSize="2147483647" maxReceivedMessageSize="2147483647">
                <security mode="None" />
            </binding>
        </basicHttpBinding>
        <customBinding>
            <binding name="CustomBinding_GetData">
                <binaryMessageEncoding />
                <httpsTransport maxReceivedMessageSize="2147483647" maxBufferSize="2147483647" />
            </binding>
        </customBinding>
    </bindings>

    <client>
        <endpoint address="//localhost/MyApp/Webservice/Data.asmx"
            binding="basicHttpBinding" bindingConfiguration="DataS1"
            contract="ServiceReference1.DataS" name="DataS" />

        <endpoint address="//localhost/MyApp/Webservice/GetData.svc"
            binding="customBinding" bindingConfiguration="CustomBinding_GetData"
            contract="GetData.GetData" name="CustomBinding_GetData" />
    </client>

</system.serviceModel>

我上面的错误配置是什么使得对服务的调用在http上失败。

1 个答案:

答案 0 :(得分:0)

使用mexHttpBinding再添加一个端点,如下所示     端点地址=&#34; mexhttp&#34;结合=&#34; mexHttpBinding&#34;合同=&#34; IMetadataExchange接口&#34; /&GT;

以及

将服务行为添加到服务标签

<service name="MyData" behaviorConfiguration="MyData">