404 BadRequest使用IIS主机标头通过外部IP公开WCF服务

时间:2022-11-21 03:30:11

We host a WCF webservice on Windows Server 2003. This server only has 2 internal IP's. We want to expose the service externally. This is done through the firewall that maps an external IP to the service.

我们在Windows Server 2003上托管WCF Web服务。该服务器只有2个内部IP。我们希望从外部公开服务。这是通过将外部IP映射到服务的防火墙完成的。

So, I would need to modify the service to display that external IP for the internal links. This is not an issue since it should only be used externally.

因此,我需要修改服务以显示内部链接的外部IP。这不是问题,因为它只应在外部使用。

Changing the Host Header value in IIS gives a 'Bad Request (Invalid Hostname)' response from IIS. I also added an 'address' value to the endpoint entry in the web.config ... but it sill just points to the internal machine name. Any ideas?

更改IIS中的Host Header值会从IIS中产生“错误请求(无效主机名)”响应。我还在web.config中的端点条目中添加了一个“地址”值...但它只是指向内部机器名称。有任何想法吗?

edit: I can verify that IIS7 has the exact same behaviour. Address didn't work. Different hostname gave Invalid Hostname error. Is there seriously no way to present a different (fictive) IP? :/

编辑:我可以验证IIS7具有完全相同的行为。地址不起作用。不同的主机名给出了无效的主机名错误。有没有办法提出不同的(虚构)IP? :/

edit2:

<system.serviceModel>
    <bindings>
        <basicHttpBinding>
            <binding name="BasicAnonymous">
                <security mode="None"/>
            </binding>
        </basicHttpBinding>
    </bindings>
    <behaviors>
        <serviceBehaviors>
            <behavior name="Extended">
                <serviceMetadata httpGetEnabled="true"/>
                <dataContractSerializer maxItemsInObjectGraph="2147483646"/>
            </behavior>
        </serviceBehaviors>
    </behaviors>
    <diagnostics>
        <messageLogging logEntireMessage="true" logMalformedMessages="false" logMessagesAtServiceLevel="false" logMessagesAtTransportLevel="true" maxMessagesToLog="3000"/>
    </diagnostics>
    <services>
        <service behaviorConfiguration="Extended" name="AnCWCFWebService.ProductInfoProvider">
            <endpoint address="" binding="basicHttpBinding" name="ASMX" bindingConfiguration="BasicAnonymous" contract="AnCWCFWebService.Interfaces.IProductInfoProvider"/>
        </service>
    </services>
</system.serviceModel>

3 个解决方案

#1


404 BadRequest Due to IIS Configuration...

If you are receiving a 404 BadRequest error from IIS after attempting to modify host headers in IIS, this is common, but there is a fix!

如果您在尝试修改IIS中的主机头后从IIS收到404 BadRequest错误,这很常见,但有一个修复!

Fix By Making IIS Configuration Changes

How can WCF support multiple IIS Binding specified per site?:
http://tinyurl.com/55q5hg

WCF如何支持每个站点指定的多个IIS绑定?:http://tinyurl.com/55q5hg

Fix By Making WCF Code Modifications

Alternatively, the following article explains how one developer solved this issue with a combination of configuration and code:

Well, that was fun! An adventure in WCF, SSL, and Host Headers
http://geekswithblogs.net/rakker/archive/2008/07/03/123562.aspx

或者,下面的文章解释了一个开发人员如何使用配置和代码的组合来解决这个问题:嗯,这很有趣! WCF,SSL和主机标题的冒险http://geekswithblogs.net/rakker/archive/2008/07/03/123562.aspx

The article references two important links...

文章引用了两个重要的链接......

The first one explains how to properly set the host headers in IIS:

第一个解释了如何在IIS中正确设置主机头:

After getting host headers working, you'll find that...

让主机头工作后,你会发现...

"you can't have more than one host header for IIS or wcf will break"

“你不能有多个主机头为IIS或wcf将打破”

The solution to working around this limitation is found within this article, and there is also a comment on the GeeksWithBlogs.net article above that provides an enhanced variation:

在本文中可以找到解决此限制的解决方案,并且还对上面的GeeksWithBlogs.net文章提供了一个评论,该文章提供了增强的变体:

  • WCF: This collection already contains an address with scheme http:
    http://tinyurl.com/62yssd
  • WCF:此集合已包含方案http:http://tinyurl.com/62yssd的地址

If you are still experiencing trouble, let us know in the comments below...

如果您仍遇到问题,请在下面的评论中告诉我们......


If the Service is Not Working...

My experience with WCF is that it is very tricky at times, especially with configuration. If one is following best practices and not doing anything non-standard, the great majority of deployment problems are the result of a botched configuration file.

我对WCF的经验是,它有时非常棘手,特别是在配置方面。如果一个人遵循最佳实践并且没有做任何非标准的事情,那么绝大多数部署问题都是由于配置文件拙劣造成的。

In theory (not so much in practice, due to architectural differences), setting up a WCF service on IIS should be no different than setting up a typical virtual directory and corresponding application for web application or for an ASMX web service.

理论上(在实践中并非如此,由于架构差异),在IIS上设置WCF服务应该与为Web应用程序或ASMX Web服务设置典型的虚拟目录和相应的应用程序没有什么不同。

Therefore, I recommend that if this is the first WCF service you are exposing to the Internet, follow the same simple approach you would take when exposing your first website. Basically, create a new sample "WCF Service Application" (this is available in the Add New Project dialog, under the Web section of C# or VB).

因此,我建议如果这是您正在向Internet公开的第一个WCF服务,请遵循在公开您的第一个网站时采用的相同简单方法。基本上,创建一个新的示例“WCF服务应用程序”(这可以在“添加新项目”对话框的C#或VB的Web部分下找到)。

Once you have it working, follow your deployment practices to move it into a production sandbox and test it locally. This sandbox would preferably already have some web sites or web services installed and known to be accessible from the Internet, in order to eliminate any doubt about the typical network configuration issues. If you have a sample ASMX web service that is already successfully exposed on the Internet from that server, that would be best.

使其工作后,请按照部署实践将其移至生产沙箱并在本地进行测试。该沙箱最好已经安装了一些网站或网络服务,并且已知可从因特网访问,以消除对典型网络配置问题的任何疑问。如果您有一个已从该服务器成功在Internet上公开的示例ASMX Web服务,那将是最好的。

Next, try testing the the ASMX and the WCF services from web browser, both locally on the server, internally on other desktops and then finally externally.

接下来,尝试从Web浏览器测试ASMX和WCF服务,包括服务器本地,内部其他桌面,最后是外部。

Testing URLs

We want to test accessing the standard SVC and ASMX file from the web browser in all the varieties of URL flavors that are available and relevant. The results should be similar, with summary page about he service rendering in the window. The difference will be that the ASMX web service's summary will likely allow you to execute the web methods on the service if that feature has not been disabled in the web.config file.

我们希望测试从Web浏览器访问标准SVC和ASMX文件,以及各种可用且相关的URL风格。结果应该类似,在窗口中有关于服务呈现的摘要页面。不同之处在于,如果未在web.config文件中禁用该功能,ASMX Web服务的摘要可能允许您在服务上执行Web方法。

Compare the results of browser fetches of the following styles of URLs...

比较以下样式的URL的浏览器提取结果...

  • http://localhost/WcfService1/Service1.svc
  • http://localhost/WcfService1/Service1.asmx
  • http://MachineName or MachineFQN/WcfService1/Service1.svc
  • http:// MachineName或MachineFQN / WcfService1 / Service1.svc

  • http://MachineName or MachineFQN/WcfService1/Service1.asmx
  • http:// MachineName或MachineFQN / WcfService1 / Service1.asmx

  • http://MachineLocalIP#1/WcfService1/Service1.svc
  • http://MachineLocalIP#1/WcfService1/Service1.asmx
  • http://MachineLocalIP#2/WcfService1/Service1.svc
  • http://MachineLocalIP#2/WcfService1/Service1.asmx
  • http://ExternalIP/WcfService1/Service1.svc
  • http://ExternalIP/WcfService1/Service1.asmx

All of these tests should return similar results.

所有这些测试都应该返回类似的结果。

Testing Service Methods

If you feel like it, go head and test some web methods on the ASMX web service for any of the tested URLs, from the web browser. You'll soon see that we can test ASMX web services a different way also...

如果您愿意,请从Web浏览器开始,在ASMX Web服务上测试一些Web方法,以获取任何测试过的URL。您很快就会看到我们可以用不同的方式测试ASMX Web服务......

Next we'll test web methods on both the WCF service and the ASMX web service by using the WcfTestClient.exe application that is found in the Visual Studio 2008 distribution (C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE).

接下来,我们将使用Visual Studio 2008发行版中的WcfTestClient.exe应用程序(C:\ Program Files \ Microsoft Visual Studio 9.0 \ Common7 \ IDE)测试WCF服务和ASMX Web服务上的Web方法。

You will need to add the services through the File->Add Service menu item, typing in the URL above for each service URL that you wish to test. Make sure to include the filename of the SVC and ASMX files. If all is well, the MEX endpoint that is enabled by the "httpGetEnabled" attribute of the <serviceMetadata/> element in the web.config file will return the data that is necessary for the utility to operate, thereby populating the tree with the inventory of our service methods like this:

您需要通过File-> Add Service菜单项添加服务,在上面输入您要测试的每个服务URL的URL。确保包含SVC和ASMX文件的文件名。如果一切正常,则由web.config文件中 元素的“httpGetEnabled”属性启用的MEX端点将返回该实用程序运行所必需的数据,从而使用库存填充树我们的服务方法如下:

404 BadRequest使用IIS主机标头通过外部IP公开WCF服务

From this point, it will be useful to refer to the following to resources:

从这一点来看,参考以下资源将是有用的:

Conclusion

If you make it this far, then I do not expect any other issues and you should now attempt to compare the setup of the samples to the WCF service that you are attempting to publish to the Internet, and hopefully the differences will be obvious.

如果你做到这一点,那么我不期望任何其他问题,你现在应该尝试将样本的设置与你试图发布到Internet的WCF服务进行比较,并希望差异是显而易见的。

Remember to treat the WCF service like an ASMX web service during you diagnostics, assuming that the web.config is known to be set up correctly.

请记住在诊断期间将WCF服务视为ASMX Web服务,假设已知web.config已正确设置。

If you are still unable to make things work, check this guide for further technical advice:

如果仍然无法使其工作,请查看本指南以获取进一步的技术建议:

Finally, if all else fails, just wrap your WCF service in an ASMX web service:

最后,如果所有其他方法都失败了,只需将您的WCF服务包装在ASMX Web服务中:

#2


You just need to configure your host header in the IIS so that the links in the WSDL references will use the donmain name rather local machine name.

您只需在IIS中配置主机头,以便WSDL引用中的链接将使用donmain名称而不是本地计算机名称。

Check out Steps to configure IIS host header so that WCF will use domain name in WSDL references.

查看配置IIS主机头的步骤,以便WCF在WSDL引用中使用域名。

#3


Why not just assign a new IP address to the server instead of messing around with hostnames? A Windows Server can have multiple IP addresses for the same NIC.

为什么不直接为服务器分配新的IP地址而不是乱用主机名? Windows Server可以为同一NIC提供多个IP地址。

Here is an article that talks about it.

这是一篇谈论它的文章。

#1


404 BadRequest Due to IIS Configuration...

If you are receiving a 404 BadRequest error from IIS after attempting to modify host headers in IIS, this is common, but there is a fix!

如果您在尝试修改IIS中的主机头后从IIS收到404 BadRequest错误,这很常见,但有一个修复!

Fix By Making IIS Configuration Changes

How can WCF support multiple IIS Binding specified per site?:
http://tinyurl.com/55q5hg

WCF如何支持每个站点指定的多个IIS绑定?:http://tinyurl.com/55q5hg

Fix By Making WCF Code Modifications

Alternatively, the following article explains how one developer solved this issue with a combination of configuration and code:

Well, that was fun! An adventure in WCF, SSL, and Host Headers
http://geekswithblogs.net/rakker/archive/2008/07/03/123562.aspx

或者,下面的文章解释了一个开发人员如何使用配置和代码的组合来解决这个问题:嗯,这很有趣! WCF,SSL和主机标题的冒险http://geekswithblogs.net/rakker/archive/2008/07/03/123562.aspx

The article references two important links...

文章引用了两个重要的链接......

The first one explains how to properly set the host headers in IIS:

第一个解释了如何在IIS中正确设置主机头:

After getting host headers working, you'll find that...

让主机头工作后,你会发现...

"you can't have more than one host header for IIS or wcf will break"

“你不能有多个主机头为IIS或wcf将打破”

The solution to working around this limitation is found within this article, and there is also a comment on the GeeksWithBlogs.net article above that provides an enhanced variation:

在本文中可以找到解决此限制的解决方案,并且还对上面的GeeksWithBlogs.net文章提供了一个评论,该文章提供了增强的变体:

  • WCF: This collection already contains an address with scheme http:
    http://tinyurl.com/62yssd
  • WCF:此集合已包含方案http:http://tinyurl.com/62yssd的地址

If you are still experiencing trouble, let us know in the comments below...

如果您仍遇到问题,请在下面的评论中告诉我们......


If the Service is Not Working...

My experience with WCF is that it is very tricky at times, especially with configuration. If one is following best practices and not doing anything non-standard, the great majority of deployment problems are the result of a botched configuration file.

我对WCF的经验是,它有时非常棘手,特别是在配置方面。如果一个人遵循最佳实践并且没有做任何非标准的事情,那么绝大多数部署问题都是由于配置文件拙劣造成的。

In theory (not so much in practice, due to architectural differences), setting up a WCF service on IIS should be no different than setting up a typical virtual directory and corresponding application for web application or for an ASMX web service.

理论上(在实践中并非如此,由于架构差异),在IIS上设置WCF服务应该与为Web应用程序或ASMX Web服务设置典型的虚拟目录和相应的应用程序没有什么不同。

Therefore, I recommend that if this is the first WCF service you are exposing to the Internet, follow the same simple approach you would take when exposing your first website. Basically, create a new sample "WCF Service Application" (this is available in the Add New Project dialog, under the Web section of C# or VB).

因此,我建议如果这是您正在向Internet公开的第一个WCF服务,请遵循在公开您的第一个网站时采用的相同简单方法。基本上,创建一个新的示例“WCF服务应用程序”(这可以在“添加新项目”对话框的C#或VB的Web部分下找到)。

Once you have it working, follow your deployment practices to move it into a production sandbox and test it locally. This sandbox would preferably already have some web sites or web services installed and known to be accessible from the Internet, in order to eliminate any doubt about the typical network configuration issues. If you have a sample ASMX web service that is already successfully exposed on the Internet from that server, that would be best.

使其工作后,请按照部署实践将其移至生产沙箱并在本地进行测试。该沙箱最好已经安装了一些网站或网络服务,并且已知可从因特网访问,以消除对典型网络配置问题的任何疑问。如果您有一个已从该服务器成功在Internet上公开的示例ASMX Web服务,那将是最好的。

Next, try testing the the ASMX and the WCF services from web browser, both locally on the server, internally on other desktops and then finally externally.

接下来,尝试从Web浏览器测试ASMX和WCF服务,包括服务器本地,内部其他桌面,最后是外部。

Testing URLs

We want to test accessing the standard SVC and ASMX file from the web browser in all the varieties of URL flavors that are available and relevant. The results should be similar, with summary page about he service rendering in the window. The difference will be that the ASMX web service's summary will likely allow you to execute the web methods on the service if that feature has not been disabled in the web.config file.

我们希望测试从Web浏览器访问标准SVC和ASMX文件,以及各种可用且相关的URL风格。结果应该类似,在窗口中有关于服务呈现的摘要页面。不同之处在于,如果未在web.config文件中禁用该功能,ASMX Web服务的摘要可能允许您在服务上执行Web方法。

Compare the results of browser fetches of the following styles of URLs...

比较以下样式的URL的浏览器提取结果...

  • http://localhost/WcfService1/Service1.svc
  • http://localhost/WcfService1/Service1.asmx
  • http://MachineName or MachineFQN/WcfService1/Service1.svc
  • http:// MachineName或MachineFQN / WcfService1 / Service1.svc

  • http://MachineName or MachineFQN/WcfService1/Service1.asmx
  • http:// MachineName或MachineFQN / WcfService1 / Service1.asmx

  • http://MachineLocalIP#1/WcfService1/Service1.svc
  • http://MachineLocalIP#1/WcfService1/Service1.asmx
  • http://MachineLocalIP#2/WcfService1/Service1.svc
  • http://MachineLocalIP#2/WcfService1/Service1.asmx
  • http://ExternalIP/WcfService1/Service1.svc
  • http://ExternalIP/WcfService1/Service1.asmx

All of these tests should return similar results.

所有这些测试都应该返回类似的结果。

Testing Service Methods

If you feel like it, go head and test some web methods on the ASMX web service for any of the tested URLs, from the web browser. You'll soon see that we can test ASMX web services a different way also...

如果您愿意,请从Web浏览器开始,在ASMX Web服务上测试一些Web方法,以获取任何测试过的URL。您很快就会看到我们可以用不同的方式测试ASMX Web服务......

Next we'll test web methods on both the WCF service and the ASMX web service by using the WcfTestClient.exe application that is found in the Visual Studio 2008 distribution (C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE).

接下来,我们将使用Visual Studio 2008发行版中的WcfTestClient.exe应用程序(C:\ Program Files \ Microsoft Visual Studio 9.0 \ Common7 \ IDE)测试WCF服务和ASMX Web服务上的Web方法。

You will need to add the services through the File->Add Service menu item, typing in the URL above for each service URL that you wish to test. Make sure to include the filename of the SVC and ASMX files. If all is well, the MEX endpoint that is enabled by the "httpGetEnabled" attribute of the <serviceMetadata/> element in the web.config file will return the data that is necessary for the utility to operate, thereby populating the tree with the inventory of our service methods like this:

您需要通过File-> Add Service菜单项添加服务,在上面输入您要测试的每个服务URL的URL。确保包含SVC和ASMX文件的文件名。如果一切正常,则由web.config文件中 元素的“httpGetEnabled”属性启用的MEX端点将返回该实用程序运行所必需的数据,从而使用库存填充树我们的服务方法如下:

404 BadRequest使用IIS主机标头通过外部IP公开WCF服务

From this point, it will be useful to refer to the following to resources:

从这一点来看,参考以下资源将是有用的:

Conclusion

If you make it this far, then I do not expect any other issues and you should now attempt to compare the setup of the samples to the WCF service that you are attempting to publish to the Internet, and hopefully the differences will be obvious.

如果你做到这一点,那么我不期望任何其他问题,你现在应该尝试将样本的设置与你试图发布到Internet的WCF服务进行比较,并希望差异是显而易见的。

Remember to treat the WCF service like an ASMX web service during you diagnostics, assuming that the web.config is known to be set up correctly.

请记住在诊断期间将WCF服务视为ASMX Web服务,假设已知web.config已正确设置。

If you are still unable to make things work, check this guide for further technical advice:

如果仍然无法使其工作,请查看本指南以获取进一步的技术建议:

Finally, if all else fails, just wrap your WCF service in an ASMX web service:

最后,如果所有其他方法都失败了,只需将您的WCF服务包装在ASMX Web服务中:

#2


You just need to configure your host header in the IIS so that the links in the WSDL references will use the donmain name rather local machine name.

您只需在IIS中配置主机头,以便WSDL引用中的链接将使用donmain名称而不是本地计算机名称。

Check out Steps to configure IIS host header so that WCF will use domain name in WSDL references.

查看配置IIS主机头的步骤,以便WCF在WSDL引用中使用域名。

#3


Why not just assign a new IP address to the server instead of messing around with hostnames? A Windows Server can have multiple IP addresses for the same NIC.

为什么不直接为服务器分配新的IP地址而不是乱用主机名? Windows Server可以为同一NIC提供多个IP地址。

Here is an article that talks about it.

这是一篇谈论它的文章。