首页 > 解决方案 > 在 Visual Studio 中向 WCF 服务添加服务引用失败

问题描述

我有最简单的 WCF 服务(Visual Studio 模板之一),它在 http 上运行良好,但在 https 上运行失败。下面是小细节。

这是它在 IIS 中的样子。

在此处输入图像描述

这是web.config

<system.serviceModel>
<services>
  <service name="WcfService1.Service1">
    <endpoint address="" binding="wsHttpBinding" contract="WcfService1.IService1" />
    <!--<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>-->
  </service>
</services>
<behaviors>
  <serviceBehaviors>
    <behavior>
      <!-- To avoid disclosing metadata information, set the values below to false before deployment -->
      <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
      <!-- To receive exception details in faults for debugging purposes, set the value below to true.  Set to false before deployment to avoid disclosing exception information -->
      <serviceDebug includeExceptionDetailInFaults="false"/>
    </behavior>
  </serviceBehaviors>
</behaviors>
<protocolMapping>
  <add binding="basicHttpsBinding" scheme="https"/>
</protocolMapping>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true"/>

这是我从 ip ping 时的浏览器

在此处输入图像描述

这是我从域名 ping 时的浏览器。

在此处输入图像描述

在这一点上一切都很好。但是,当我尝试在 Visual Studio 中添加服务引用时,它使用 https 失败,但使用 http 成功。

这是 IP 之一(http)

在此处输入图像描述

这是域名一(https)

在此处输入图像描述

详细的错误信息是

在此处输入图像描述

我在这里遇到的问题是,为什么 Visual Studio 无法获取https://tar.tennis.com.au/testwcf/service1.svc并且可以使用http://10.21.100.129/testwcf/service1.svc

肯定一个是http,另一个是https,但是为什么浏览器没有问题呢?

标签: visual-studiowcfhttps

解决方案


我们需要添加额外的服务端点来支持 https 绑定。有两种配置方式,请参考我的示例。
1.简化配置

  <system.serviceModel>
    <behaviors>
      <serviceBehaviors>
        <behavior>
          <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="false" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <protocolMapping>
      <add binding="basicHttpBinding" scheme="http" />
      <add binding="basicHttpsBinding" scheme="https" />
    </protocolMapping>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
  </system.serviceModel>

2.添加使用传输安全的https服务端点。

<system.serviceModel>
    <services >
      <service name="WcfService1.Service1">
        <endpoint address="" binding="basicHttpBinding" contract="WcfService1.IService1"></endpoint>
        <endpoint address="" binding="basicHttpBinding" contract="WcfService1.IService1" bindingConfiguration="https"></endpoint>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior>
          <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="false" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <bindings>
      <basicHttpBinding>
        <binding name="https">
          <security mode="Transport">
            <transport clientCredentialType="None"></transport>
          </security>
        </binding>
      </basicHttpBinding>
    </bindings>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
  </system.serviceModel>

如果有什么我可以帮忙的,请随时告诉我。


推荐阅读