190

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.

When I deploy to our live server and run the application I get an exception of type System.ServiceModel.ServiceActivationException which states that the service can not be activated due to an exception during compilation. The exception is:

This collection already contains an address with scheme http. There can be at most one address per scheme in this collection.

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?

Jeroen
  • 60,696
  • 40
  • 206
  • 339
Jeremy
  • 44,950
  • 68
  • 206
  • 332
  • 1
    "I read that this exception may be thrown if the web site has more than one host header" BINGO for me. Noting for future reference. – Scuzzlebutt Apr 14 '23 at 17:41

7 Answers7

175

In .Net 4, you can use the multipleSiteBindingsEnabled option:

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

Then, you won't have to specify each address.

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

ericvg
  • 3,907
  • 1
  • 30
  • 35
148

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.

Taken from the site, below is an example of how to alter your applications web config:

<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:9000 are 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.

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.

Community
  • 1
  • 1
Jeremy
  • 44,950
  • 68
  • 206
  • 332
  • 2
    Adding the base address prefix filter to the web.config worked perfectly. Thanks Jeremy! – Mike737 Jul 07 '09 at 07:32
  • 2
    I can't think of any reason why one would want such a restriction, much less the default setting... – pbz Nov 19 '09 at 00:49
  • 42
    I'm starting to think badly about WCF in combination with ASP.net and web services accessed through JavaScript. I had much less problems with plain old ASMX services... – Juri May 10 '10 at 09:34
  • Ok what if you have a site with a mix of .net 4 and .net 2 applications running under it. The Base of the application is .net4 and there are several applications under it that require .net2. Do you use in all the .net4 files and the prefix in the .net 2 applications? – Travis Nov 05 '15 at 17:09
  • Be careful, make sure you are not trying to add the same base address in your config file as well as the code. In my situation, this was exactly the case. – Ashok Garg Nov 14 '21 at 17:46
66

Did you see this - 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:

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

With ASP.NET 2.0/3.0/3.5, add the following lines to your web.config:

<system.serviceModel> 
     <serviceHostingEnvironment> 
          <baseAddressPrefixFilters> 
               <add prefix="http://www.YourHostedDomainName.com"/> 
          </baseAddressPrefixFilters> 
     </serviceHostingEnvironment> 
</system.serviceModel> 
Community
  • 1
  • 1
Mike Chaliy
  • 25,801
  • 18
  • 67
  • 105
  • Thanks. I'm going to continue searching to see if there is a not code solution. Something that can be done in configuration because this is going to affect any project we do I'm hoping to not have to write custom code. – Jeremy Feb 18 '09 at 16:59
19

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.

Amar
  • 191
  • 1
  • 2
  • 1
    Yes Amar this was very helpful - in my case it was *ANOTHER* website with multiple bindings that broke it. Externally available on the same machine (but with a different Hostname). Equally it could be fixed by adding the multipleSiteBindingsEnabled setting but then the web.config would be different from all other environments. – The Coder Jan 30 '14 at 21:48
  • 2
    It is a shame this is at the bottom. In our case this fixed it for us. – brendonparker Apr 02 '14 at 21:34
  • It helped me to replicate the error in development environment. I can't edit website bindings in neither certification nor live environments. I've changed my hosts file to simulate a domain and added bindings to local IIS and bam! – MFedatto May 05 '16 at 11:34
12

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.

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 and specify it in config, you get this error. Most probably, that's not the only way to get the error, thou.

bohdan_trotsenko
  • 5,167
  • 3
  • 43
  • 70
2

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.

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.

Eric
  • 2,207
  • 2
  • 16
  • 16
0

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 link which stated the below:

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:

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.

Solution: Configure IIS server to handle multiple port bindings. Contact the vendor (Microsoft) to configure it.

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.

Fixing the correct Bindings resolved the problem, in my case, and i did not have to configure the Web.Config.

Community
  • 1
  • 1
jimas13
  • 597
  • 5
  • 19