【问题标题】:How to Configuring WCF services to work with both HTTP and HTTPS如何配置 WCF 服务以同时使用 HTTP 和 HTTPS
【发布时间】:2014-09-23 11:22:21
【问题描述】:

我正在尝试配置一项服务,以便从 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 上失败。

【问题讨论】:

  • 您已经直接检查了服务端点(通过浏览器,而不是 silverlight 应用程序)并且它不会在 HTTP 中加载?可以肯定的是,您没有用任何安全属性在代码中装饰您的服务类,对吗?
  • 通过浏览器直接检查服务端点适用于 http 和 https
  • 我相信从 https 托管的 Silverlight 根本不会调用 http 服务。就像您在 https 上浏览网站,但某些网站内容来自 http 时,浏览器会弹出该警告消息。对于 Silverlight,它发现了相同的漏洞,并且根本不发出请求。您可以尝试通过在应用打开的情况下监控网络流量来获取更多详细信息。运行您的 Silverlight,打开浏览器调试器工具,然后查找您预期的服务调用。

标签: web-services wcf service-model


【解决方案1】:

使用 mexHttpBinding 再添加一个端点,如下所示 端点地址="mexhttp" binding="mexHttpBinding" contract="IMetadataExchange"/>

还有

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

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

【讨论】:

  • 你的问题解决了吗?如果没有请显示最新的配置文件
  • 我已经用我当前(最新的)web.config 和 ServiceReferences.ClientConfig 设置更新了我的帖子。我仍然无法通过 http 访问该服务,但每个都可以通过 https 正常工作。而且我要求同时支持 https 和 http。
  • 更改 http 端点的 mex 端点名称并将您的端点的 bindingConfiguration 名称更改为 MyhttpBinding 并且合约应该是接口(服务合约)
  • 使用 mexhttp 端点浏览带有基地址的服务,例如:http:// localhost:234/Myservice/Mexhttp(你的 mexhttp 端点的名称)
  • 如果可能的话,将您的解决方案分享给 vinod.profileinfo@gmail.com,我们会尝试解决这个问题
猜你喜欢
  • 2014-09-29
  • 1970-01-01
  • 2023-03-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-06-19
  • 2011-06-24
  • 1970-01-01
相关资源
最近更新 更多