WCF服务启动错误“这个集合已经包含一个地址与schemehttp”

我构build了一个包含WCF服务契约和一个调用该WCF服务的Silverlight控件的Web应用程序。 在我的开发和testing服务器上效果很好。

当我部署到我们的实时服务器并运行应用程序时,我得到一个types为System.ServiceModel.ServiceActivationException的exception,它指出由于编译期间发生exception而无法激活服务。 例外是:

这个集合已经包含一个地址与schemehttp。 这个集合中每个scheme最多可以有一个地址。

我读到,如果网站有多个主机头,这个exception可能会被抛出,这是真实的服务器上。 显然,IIS中托pipe的WCF服务只能有一个基地址。 我怎样才能解决这个问题?

在.Net 4中,您可以使用multipleSiteBindingsEnabled选项:

 <system.serviceModel> <serviceHostingEnvironment multipleSiteBindingsEnabled="true"> </serviceHostingEnvironment> </system.serviceModel> 

那么,你将不必指定每个地址。

http://msdn.microsoft.com/en-us/library/system.servicemodel.servicehostingenvironment.multiplesitebindingsenabled.aspx

概要,

代码解决scheme: 这里

configuration解决scheme: 这里

在Mike Chaliy的帮助下,我find了一些如何通过代码来实现的解决scheme。 因为这个问题几乎影响到我们部署到现场环境中的所有项目,所以我提出了一个纯粹的configuration解决scheme。 我最终发现了一个详细介绍如何在.net 3.0和.net 3.5中执行的操作。

从网站采取,下面是如何改变你的应用程序的webconfiguration的例子:

 <system.serviceModel> <serviceHostingEnvironment> <baseAddressPrefixFilters> <add prefix="net.tcp://payroll.myorg.com:8000"/> <add prefix="http://shipping.myorg.com:9000"/> </baseAddressPrefixFilters> </serviceHostingEnvironment> </system.serviceModel> 

在上面的例子中,net.tcp://payroll.myorg.com:8000和http://shipping.myorg.com:9000是唯一的基地址,对于它们各自的scheme,将被允许通过。; baseAddressPrefixFilter不支持任何通配符。

IIS提供的baseAddresses可能具有绑定到其他scheme(不存在于baseAddressPrefixFilter列表中的地址)的地址。 这些地址不会被过滤掉。

Dns解决scheme(未经testing):我认为如果你为你的web应用程序创build了一个新的dns条目,添加了一个新的web站点,并且给它一个与dns条目相匹配的主机标题,你就可以完全缓解这个问题,必须编写自定义代码或将前缀添加到您的web.config文件。

你有没有看到这个 – http://kb.discountasp.net/KB/a799/error-accessing-wcf-service-this-collection-already.aspx

您可以通过更改web.config文件来解决此错误。

使用ASP.NET 4.0,将以下行添加到您的web.config中:

 <system.serviceModel> <serviceHostingEnvironment multipleSiteBindingsEnabled="true" /> </system.serviceModel> 

使用ASP.NET 2.0 / 3.0 / 3.5,将以下行添加到您的web.config中:

 <system.serviceModel> <serviceHostingEnvironment> <baseAddressPrefixFilters> <add prefix="http://www.YourHostedDomainName.com"/> </baseAddressPrefixFilters> </serviceHostingEnvironment> </system.serviceModel> 

在我的情况下,这个问题的根本原因是在父网站定义的多个http绑定,即InetMgr-> Sites-> Mysite-> properties-> EditBindings。 我删除了一个不需要的http绑定,问题得到解决。

在我的情况下,这很简单:我在Visual Studio中使用“添加WCF服务”向导,该向导自动在app.config中创build相应的部分。 然后我继续阅读如何:在托pipe应用程序中托pipeWCF服务 。 问题是:我不需要指定URL来运行Web服务。

更换:

 using (ServiceHost host = new ServiceHost(typeof(HelloWorldService), baseAddress)) 

附:

 using (ServiceHost host = new ServiceHost(typeof(HelloWorldService)) 

错误消失了。

一般的想法:如果你提供基地址作为参数并在configuration中指定它,你会得到这个错误。 很可能,这不是唯一的方法来得到错误,你。

我有这个问题,原因很愚蠢。 我正在尝试微软的演示,关于从命令行可执行文件中运行ServiceHost。 我遵循指示,包括它说什么添加适当的服务(和接口)。 但是我得到了上面的错误。

当我添加服务类时,VS自动将configuration添加到app.config。 演示试图添加该信息。 由于它已经在configuration中,我删除了演示部分,它的工作。