如何修复:"Metadata contains a reference that cannot be resolved:'http://<host>:<port><path>?wsdl'." for "Configure WCF Web Service Reference" in vs

How to fix: "Metadata contains a reference that cannot be resolved:'http://<host>:<port><path>?wsdl'." for "Configure WCF Web Service Reference" in vs

我一直在尝试使用非标准 HTTPS 端口在我的 IIS 服务器上发布 WCF Web 服务。只有在身份验证后才能访问服务器(通过基本身份验证)。我为测试目的创建的 Web 服务是您在 Visual Studio 中创建 WCF Service Application 时默认获得的基础项目。我所做的唯一修改是在 web.config 文件中:

<?xml version="1.0" encoding="utf-8"?>
<configuration>

  <appSettings>
    <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
  </appSettings>
  <system.web>
    <compilation debug="true" targetFramework="4.6.1" />
    <httpRuntime targetFramework="4.6.1"/>
  </system.web>
  <system.serviceModel>
    <behaviors>
      <serviceBehaviors>
        <behavior>
          <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
          <serviceDebug includeExceptionDetailInFaults="false"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
    <bindings>
      <basicHttpBinding>
        <binding name="secureHttpBinding">
          <security mode="Transport">
            <transport clientCredentialType="Basic"/>
          </security>
        </binding>
      </basicHttpBinding>
    </bindings>
    <services>
      <service name="SoapApi.Service1">
        <endpoint address=""
                  binding="basicHttpBinding"
                  bindingConfiguration="secureHttpBinding"
                  contract="SoapApi.IService1"/>

        <endpoint address="mex"
                  binding="mexHttpsBinding"
                  contract="IMetadataExchange" />
      </service>
    </services>
  </system.serviceModel>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
    <directoryBrowse enabled="true"/>
  </system.webServer>

</configuration>

在本地文件系统上发布服务并配置 IIS 之后。从本地 PC 上的浏览​​器进行身份验证后,我能够访问服务器上的 WSDL 文件。但是,如果我尝试使用来自 Visual Studio 的 Configure WCF Web Service Reference 向导将服务添加到简单的客户端应用程序,我会收到以下错误消息:

Metadata contains a reference that cannot be resolved:'http://<host>:<port><path>?wsdl'.

以及完整的错误信息:

An error occurred while attempting to find services at 'http://<host>:<port><path>?wsdl'. The remote server returned an error: (403) Forbidden.

由于这个错误表明我没有访问权限,所以我想为什么不关闭身份验证并尝试它是否有效。我在 IIS 中激活了对网站的匿名访问并停用了基本身份验证。 此外,我更改了 web.config 文件中的以下段落:

<basicHttpBinding>
    <binding name="secureHttpBinding">
      <security mode="Transport">
        <transport clientCredentialType="None"/>
      </security>
    </binding>
 </basicHttpBinding>

我仍然可以从我的浏览器访问 WSDL 文件,但是在向客户端添加服务引用时我仍然遇到同样的错误。如果我通过 dotnet-svcutil http://<host>:<port><path>?wsdl

测试它,也会发生同样的情况

如果我尝试在本地添加服务引用,一切正常,没有任何问题。

附加信息:

关于为什么会发生这种情况以及我可以做些什么来解决这个问题有什么想法吗?

如果您使用传输安全模式,为什么不使用 https 地址? https/http 基地址应该在 IIS 站点绑定模块中配置。
另外,由于您使用的是基本身份验证,请在IIS身份验证模块中开启匿名身份验证和基本身份验证。
在我这边,我可以使用 Core-based 控制台应用程序正确添加服务引用。
此外,请为 WCF 启用以下 windows 功能。
如果有什么我可以帮忙的,请随时告诉我。