.net WCF 服务启动错误“此集合已包含具有方案 http 的地址”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/561823/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
WCF service startup error "This collection already contains an address with scheme http"
提问by Jeremy
I built a web application containing a WCF service contract and a Silverlight control which makes calls to that WCF service. On my development and test servers it works great.
我构建了一个 Web 应用程序,其中包含一个 WCF 服务合同和一个调用该 WCF 服务的 Silverlight 控件。在我的开发和测试服务器上,它运行良好。
When I deploy to our live server and run the application I get an exception of type System.ServiceModel.ServiceActivationExceptionwhich states that the service can not be activated due to an exception during compilation. The exception is:
当我部署到我们的实时服务器并运行应用程序时,我收到一个类型的异常System.ServiceModel.ServiceActivationException,指出由于编译期间出现异常,无法激活该服务。例外是:
This collection already contains an address with scheme http. There can be at most one address per scheme in this collection.
这个集合已经包含一个带有 http 方案的地址。在此集合中,每个方案最多可以有一个地址。
I read that this exception may be thrown if the web site has more than one host header, which is true on our live server. Apparently WCF services hosted in IIS can have only one base address. How can I get around this issue?
我读到如果网站有多个主机标头,则可能会引发此异常,这在我们的实时服务器上是正确的。显然,IIS 中承载的 WCF 服务只能有一个基地址。我怎样才能解决这个问题?
采纳答案by Jeremy
Summary,
概括,
Code solution:Here
代码解决方案:这里
Configuration solutions:Here
配置解决方案:这里
With the help of Mike Chaliy, I found some solutions on how to do this through code. Because this issue is going to affect pretty much all projects we deploy to a live environment I held out for a purely configuration solution. I eventually found one which details how to do it in .net 3.0 and .net 3.5.
在Mike Chaliy的帮助下,我找到了一些有关如何通过代码执行此操作的解决方案。因为这个问题几乎会影响我们部署到实时环境的所有项目,所以我坚持纯粹的配置解决方案。我最终找到了一个详细说明如何在 .net 3.0 和 .net 3.5 中执行此操作的文章。
Taken from the site, below is an example of how to alter your applications web config:
取自该站点,以下是如何更改应用程序 Web 配置的示例:
<system.serviceModel>
<serviceHostingEnvironment>
<baseAddressPrefixFilters>
<add prefix="net.tcp://payroll.myorg.com:8000"/>
<add prefix="http://shipping.myorg.com:9000"/>
</baseAddressPrefixFilters>
</serviceHostingEnvironment>
</system.serviceModel>
In the above example, net.tcp://payroll.myorg.com:8000 and http://shipping.myorg.com:9000are the only base addresses, for their respective schemes, which will be allowed to be passed through. The baseAddressPrefixFilter does not support any wildcards .
The baseAddresses supplied by IIS may have addresses bound to other schemes not present in baseAddressPrefixFilter list. These addresses will not be filtered out.
在上面的例子中,net.tcp://payroll.myorg.com:8000 和 http://shipping.myorg.com:9000是它们各自方案的唯一基地址,它们将被允许通过。baseAddressPrefixFilter 不支持任何通配符。
IIS 提供的 baseAddresses 可能具有绑定到 baseAddressPrefixFilter 列表中不存在的其他方案的地址。这些地址不会被过滤掉。
Dns solution (untested):I think that if you created a new dns entry specific to your web application, added a new web site, and gave it a single host header matching the dns entry, you would mitigate this issue altogether, and would not have to write custom code or add prefixes to your web.config file.
Dns 解决方案(未经测试):我认为,如果您创建了一个特定于您的 Web 应用程序的新 dns 条目,添加了一个新网站,并为其提供了一个与 dns 条目匹配的单个主机标头,您将完全缓解这个问题,并且不会必须编写自定义代码或向 web.config 文件添加前缀。
回答by ericvg
In .Net 4, you can use the multipleSiteBindingsEnabledoption:
在 .Net 4 中,您可以使用以下multipleSiteBindingsEnabled选项:
<system.serviceModel>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true">
</serviceHostingEnvironment>
</system.serviceModel>
Then, you won't have to specify each address.
然后,您不必指定每个地址。
回答by Mike Chaliy
Did you see this - http://kb.discountasp.net/KB/a799/error-accessing-wcf-service-this-collection-already.aspx
你看到了吗 - http://kb.discountasp.net/KB/a799/error-accessing-wcf-service-this-collection-already.aspx
You can resolve this error by changing the web.config file.
With ASP.NET 4.0, add the following lines to your web.config:
您可以通过更改 web.config 文件来解决此错误。
使用 ASP.NET 4.0,将以下行添加到您的 web.config:
<system.serviceModel>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
</system.serviceModel>
With ASP.NET 2.0/3.0/3.5, add the following lines to your web.config:
使用 ASP.NET 2.0/3.0/3.5,将以下行添加到您的 web.config:
<system.serviceModel>
<serviceHostingEnvironment>
<baseAddressPrefixFilters>
<add prefix="http://www.YourHostedDomainName.com"/>
</baseAddressPrefixFilters>
</serviceHostingEnvironment>
</system.serviceModel>
回答by Amar
In my case root cause of this issue was multiple http bindings defined at parent web site i.e. InetMgr->Sites->Mysite->properties->EditBindings. I deleted one http binding which was not required and problem got resolved.
在我的例子中,这个问题的根本原因是在父网站上定义了多个 http 绑定,即 InetMgr->Sites->Mysite->properties->EditBindings。我删除了一个不需要的 http 绑定,问题得到了解决。
回答by bohdan_trotsenko
And in my case it was simple: I used 'Add WCF Service' wizard in Visual Studio, which automatically created corresponding sections in app.config. Then I went on reading How to: Host a WCF Service in a Managed Application. The problem was: I didn't need to specify the url to run the web service.
就我而言,这很简单:我在 Visual Studio 中使用了“添加 WCF 服务”向导,它会在 app.config 中自动创建相应的部分。然后我继续阅读如何:在托管应用程序中托管 WCF 服务。问题是:我不需要指定 url 来运行 Web 服务。
Replace:
代替:
using (ServiceHost host = new ServiceHost(typeof(HelloWorldService), baseAddress))
With:
和:
using (ServiceHost host = new ServiceHost(typeof(HelloWorldService))
And the error is gone.
错误消失了。
Generic idea: if you provide base address as a param andspecify it in config, you get this error. Most probably, that's not the only way to get the error, thou.
一般想法:如果您提供基地址作为参数并在配置中指定它,则会出现此错误。很可能,这不是获得错误的唯一方法,你。
回答by Eric
I had this problem, and the cause was rather silly. I was trying out Microsoft's demo regarding running a ServiceHost from w/in a Command Line executable. I followed the instructions, including where it says to add the appropriate Service (and interface). But I got the above error.
我遇到了这个问题,原因很愚蠢。我正在尝试 Microsoft 的关于从命令行可执行文件运行 ServiceHost 的演示。我按照说明进行操作,包括它所说的添加适当的服务(和接口)的位置。但我得到了上述错误。
Turns out when I added the service class, VS automatically added the configuration to the app.config. And the demo was trying to add that info too. Since it was already in the config, I removed the demo part, and it worked.
原来,当我添加服务类时,VS 会自动将配置添加到 app.config 中。演示也试图添加该信息。由于它已经在配置中,我删除了演示部分,并且它起作用了。
回答by jimas13
I came by the same error on an old 2010 Exchange Server. A service(Exchange mailbox replication service) was giving out the above error and the migration process could not be continued. Searching through the internet, i came by this linkwhich stated the below:
我在旧的 2010 Exchange Server 上遇到了同样的错误。一项服务(Exchange 邮箱复制服务)出现上述错误,无法继续迁移过程。通过互联网搜索,我来到这个链接,其中说明如下:
The Exchange GRE fails to open when installed for the first time or if any changes are made to the IIS server. It fails with snap-in error and when you try to open the snap-in page, the following content is displayed:
首次安装或对 IIS 服务器进行任何更改时,Exchange GRE 无法打开。它因管理单元错误而失败,当您尝试打开管理单元页面时,会显示以下内容:
This collection already contains an address with scheme http. There can be at most one address per scheme in this collection. If your service is being hosted in IIS you can fix the problem by setting 'system.serviceModel/serviceHostingEnvironment/multipleSiteBindingsEnabled' to true or specifying 'system.serviceModel/serviceHostingEnvironment/baseAddressPrefixFilters'."
Cause: This error occurs because http port number 443 is already in use by another application and the IIS server is not configured to handle multiple binding to the same port.
原因:发生此错误的原因是 http 端口号 443 已被另一个应用程序使用,并且 IIS 服务器未配置为处理到同一端口的多个绑定。
Solution: Configure IIS server to handle multiple port bindings. Contact the vendor (Microsoft) to configure it.
解决方案:配置 IIS 服务器以处理多个端口绑定。请联系供应商 (Microsoft) 进行配置。
Since these services were offered from an IIS Web Server, checking the Bindings on the Root Site fixed the problem. Someone had messed up the Site Bindings, defining rules that were overlapping themselves and messed up the services.
由于这些服务是从 IIS Web 服务器提供的,检查根站点上的绑定解决了问题。有人弄乱了站点绑定,定义了重叠的规则并弄乱了服务。
Fixing the correct Bindings resolved the problem, in my case, and i did not have to configure the Web.Config.
修复正确的绑定解决了这个问题,就我而言,我不必配置 Web.Config。

