【问题标题】:Webservice is not accessible publicly when using URL rewrite in IIS在 IIS 中使用 URL 重写时无法公开访问 Web 服务
【发布时间】:2026-02-09 23:30:01
【问题描述】:

我在私人服务器上托管 web 服务。然后我在公共服务器(互联网服务器)中的 IIS 中创建了 URL 重写规则。

每次我访问 URL 时,它都会在浏览器的 URL 中明确显示私有服务器的 ip/计算机名称,而不是公共 url/IP。当我点击查看脚本的 URL 时,它显示“Example.svc 不存在”错误。

当我尝试通过在其中传递 URL 将其添加为 Visual Studio 中的服务引用时,它也不起作用。它会引发以下错误。

The request failed with HTTP status 400: Bad request
Metadata contains a reference that cannot be resolved: <SERVER URL>
The remote server turned an unexpected response: (405) Method not allowed.

我应该怎么做才能公开访问该 URL 并能够将其添加为 Visual Studio 中的参考?

【问题讨论】:

标签: web-services http url-rewriting iis-8


【解决方案1】:

您似乎正在 IIS 中部署 WCF 服务。那是对的吗?如果出现“example.svc does not exists” 错误,请确保您已启用某些 Windows 功能以便在 IIS 中托管 WCF 服务。

我们无法将其添加为Service Reference 以便调用服务的原因是我们尚未发布元数据服务端点。这取决于您的 WCF 服务类型。通常,我们要么添加 Mex 服务端点,要么通过添加元数据服务行为来发布服务 WSDL。

  <services>
    <service name="WcfService1.Service1">
      <endpoint address="" binding="basicHttpBinding" bindingConfiguration="mybinding" contract="WcfService1.IService1">
      </endpoint>
      <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"></endpoint>
    </service>
  </services>

  <behaviors>
        <serviceBehaviors>
          <behavior>
            <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />

每次访问 URL 时,它都会在浏览器的 URL 中明确显示私有服务器的 ip/计算机名称,而不是公共 url/IP。

为了使用主机名/域名URL访问服务,我们应该确保域名指向服务器的公共IP。这是一个与网络相关的问题。

如果问题仍然存在,请随时告诉我。

【讨论】: