使用https终结点添加WCF服务引用

26

我的WCF服务应用可以通过http和https工作,但是当我在客户端中添加一个服务引用(使用https url),Visual Studio 2010将配置文件中的终结点设置为http。这似乎不是简单地将配置终结点更改为https,因为有多个文件在后台处理xsd并引用http终结点。如何设置我的服务/客户端以强制使用https,以便正确设置终结点?

当我尝试手动更改配置文件中的终结点并将安全模式设置为“传输”时,我会收到以下错误:

异常消息:没有终结点正在侦听https://myservice/AvailabilityService.svc ,该终结点可以接受消息。 这通常是由于地址或SOAP操作不正确引起的。 如有必要,请参见InnerException获取更多详细信息。

然而,我可以在IE中看到该终结点,并且正在本地调试。在我使用https添加服务引用并搜索其http同等项后,它找到了一个引用http的wsdl文件,一个configuration.svcinfo和一个使用http url而不是https的configuration91.svcinfo。

这是我的服务器端配置:

<?xml version="1.0"?>
<configuration>
  <system.web>
    <compilation debug="true" targetFramework="4.0" />
  </system.web>
  <system.serviceModel>
    <behaviors>
      <serviceBehaviors>
        <behavior>
          <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="true"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
  </system.serviceModel>
 <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
  </system.webServer>
</configuration>

.. 还有客户端的配置:

<system.serviceModel>
    <bindings>
      <basicHttpBinding>
        <binding name="BasicHttpBinding_IAvailabilityService" closeTimeout="00:01:00"
            openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
            allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
            maxBufferSize="65536" maxBufferPoolSize="524288" maxReceivedMessageSize="65536"
            messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered"
            useDefaultWebProxy="true">
          <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
              maxBytesPerRead="4096" maxNameTableCharCount="16384" />
          <security mode="Transport">
            <transport clientCredentialType="None" proxyCredentialType="None"
                realm="" />
            <message clientCredentialType="UserName" algorithmSuite="Default" />
          </security>
        </binding>
      </basicHttpBinding>
    </bindings>
    <client>
      <endpoint address="https://myservice/AvailabilityService.svc"
          binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IAvailabilityService"
          contract="PaymentAvailabilityService.IAvailabilityService"
          name="BasicHttpBinding_IAvailabilityService" />
    </client>
  </system.serviceModel>

也许我最好在代码中手动消费服务?

1个回答

37

您需要更改绑定以使用传输安全来使用HTTPS。

传输安全

您的服务器端绑定应该配置为HTTPS,而客户端也是如此:

<bindings>
  <basicHttpBinding>
    <binding name="httpsBinding" allowCookies="true" maxReceivedMessageSize="2147483647" maxBufferPoolSize="2147483647">
      <readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" />
      <security mode="Transport">
        <transport clientCredentialType="None" />
      </security>
    </binding>
  </basicHttpBinding>
</bindings>
<services>
  <service name="yourNamespace.YourService" behaviorConfiguration="yourBehaviors">
    <endpoint contract="yourNamespace.YourService" binding="basicHttpBinding" bindingConfiguration="httpsBinding" />
  </service>
</services>

谢谢,看起来这个方法解决了问题。我想我曾经错误地认为WCF服务应用程序在幕后处理了一些东西,而在WCF服务库中更加明确。 - Chris Klepeis
@ChrisKlepeis - 很高兴能帮忙!我认为在你的情况下,它在幕后执行了最基本的操作,即使用标准的 out-of-box 设置打开了一个 basichttpbinding。你可以通过 https 访问 MEX 端点,因为你在元数据行为中启用了它。 - Nick Nieslanik

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接