baseAddressPrefixFilters enables IIS hosted WCF service on multi addresses

by Lixin 15. December 2008 10:43
WCF came up in .NET Framework 3.0 and it had a well known limitation on service instance hosted on IIS with a parked domain name. If you host a plain WCF service on IIS and did nothing special in your code and configuration file, you will find that you can't access that WCF service by the domain name provided and normally the service page returns an error message such as

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

For example, you have a website which has a domain name called "mysite.com". Then normally there could be two addresses with which you can access your service,

http://mysite.com/myservice.svc      or      http://www.mysite.com/myservice.svc

In that case IIS doesn't know which base address, mysite.com or www.mysite.com should be bound to your WCF service and it just throws out an exception to tell you that you need to make it clear.

A solution to this problem is create your own custom ServiceHostFactory. Create a new class called MyServiceHostFactory

public class MyServiceHostFactory : ServiceHostFactory
{
    protected override ServiceHost CreateServiceHost
        (Type serviceType, Uri[] baseAddresses)
    {
       return base.CreateServiceHost(serviceType, new Uri("mysite.com"));
    }    
}

You can see I have put "mysite.com" into the above code and get IIS respond to requests which nominate addresses starting with "mysite.com". Namely you can see the WCF service page at http://mysite.com/myservice.svc. as for http://www.mysite.com/myservice.svc, it just won't work.

Then set custom service host factory in your svc file,

<%@ ServiceHost Language=”C#”  Service=”MyService”
                Factory=”MyServiceHostFactory“
                CodeBehind=”~/App_Code/MyService.cs” %>


If you keep your service code in a separate class library, the svc file could be like this,

<%@ ServiceHost Language=”C#”  Service=”MyNameSpace.MyService”
                Factory=”MyNameSpace.MyServiceHostFactory“ %>


.NET Framework 3.5 has introduced a better solution for this issue, which is called the "baseAddressPrefixFilters" entry in the configuration file.

In your web.config file, go to <system.serviceModel><serviceHostingEnvironment> , add a new section called <baseAddressPrefixFilters> into the file.

<system.serviceModel>

  <serviceHostingEnvironment aspNetCompatibilityEnabled="true">

    <baseAddressPrefixFilters>

      <clear/>

      <add prefix="http://mysite.com"/>

    </baseAddressPrefixFilters>

  </serviceHostingEnvironment>

</system.serviceModel>

No doubt it is a much more decent solution but you need to install .NET Framework 3.5 for this feature.

 

Currently rated 5.0 by 2 people

  • Currently 5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags: , , , , ,

Communication

Comments

Comments are closed

Powered by BlogEngine.NET 1.4.5.0
Original themed by Mads Kristensen and customised by Lixin