Getting "Collection Already Contains an Address" Exception in WCF

In this article, I will tell you how to eliminate the "collection already contains an address Exception" in WCF.

To understand this in depth you need to create a website that contains a WCF service.

Step 1: Create a ASP.NET website named "WCF_Website".

Create a Asp.Net website

Step 2: Add a WCF service named "MyService" to it.

Add a WCF service

Step 3: Add an "operation contract" into the interface of the WCF service.

[OperationContractint Add(int a , int b);
[OperationContract]
 int Add(int a , int b);

operation contract

Step 4: Implement the entire interface in the "MyService" class and return the addition of both input values.

public int Add(int a, int b)
{
   
return a + b;
}

Implement the whole interface

Step 5: Publish the website in a local folder

Publish the website in a local folder

Then publish it on the main server using FTP client or you directly publish it on the server.

FTP client

Problem: When you access your WCF service named "MyService.svc", it will generate an exception "collection already contains an address".

collection already contains an address

Solution
 
This is the current default settings in the web.config file.

config file

Add the following code to the <system.serviceModel> </system.serviceModel> section of the web.config file.

  1. If you have the 3.0/3.5 Framework on the server:

    <serviceHostingEnvironment>
    <
    baseAddressPrefixFilters>
    <
    add prefix="http://xyz.com"/>
    </
    baseAddressPrefixFilters>
    </serviceHostingEnvironment>

    Note: Here my server domain name is "xyz".

    server domain
     

  2. If you have version 4.0 or above of the Framework on the server:

    <serviceHostingEnvironment multipleSiteBindingsEnabled="True">    
    </serviceHostingEnvironment> 

    Framework

Up Next
    Ebook Download
    View all
    Learn
    View all