Reading ServiceHost base address from configuration in WCF

Service Station, by Aaron Skonnard

Syndication

One of the most popular questions around ServiceHost is where do you store the "base address" value in the WCF configuration section. There answer is "you don't".
 
At first, I found this to be a strange omission, but after discussing it more with some folks on the WCF team I realized the issue went a bit deeper. While it might make sense to store the base address in config for self-hosting scenarios, it doesn't make sense for IIS/WAS scenarios. In the latter case, the base address is based on the application virtual directory and is automatically supplied by the WCF hosting goo. If you were to host WCF services in some other managed environment (say BizTalk for example), it would be up to BizTalk to know what base address to use for a particular ServiceHost. Hence, for now, the WCF folks have decided that it should be up to the developer to read the base address from configuration when desired. The team may provide some fancy helpers down the road to simplify this process but as of the Feb CTP, it's up to you.
 
Here's a simple example showing how to write a custom ServiceHost that reads the base address for each transport from the AppSettings:
 
public class CustomServiceHost : ServiceHost
{
    public CustomServiceHost(Type serviceType)
        : base(serviceType, GetBaseAddresses())
    {
    }
    // read base addresses from AppSettings in config
    private static Uri[] GetBaseAddresses()
    {
        List<Uri> addresses = new List<Uri>();
        AddBaseAddress(addresses, "baseHttpAddress");
        AddBaseAddress(addresses, "baseTcpAddress");
        AddBaseAddress(addresses, "baseNamedPipeAddress");
        return addresses.ToArray();
    }
    private static void AddBaseAddress(List<Uri> addresses, string key)
    {
        string address = ConfigurationManager.AppSettings[key];
        if (null != address)
            addresses.Add(new Uri(address));
    }
}
 
My collegue, Matt Milner, has written a more thorough sample where he defines a custom configuration section to use in the configuration file. You can find it here.
 

Posted Apr 19 2006, 02:53 PM by Aaron Skonnard
Filed under: ,

Comments

Kirk Allen Evans' Blog wrote Web Service Geek? Go Read Pluralsight's blogs
on 04-20-2006 6:11 PM
I like to write in my blog, but I hardly read other blogs anymore.&nbsp; In fact, I don't have an aggregator...
Service Station, by Aaron Skonnard wrote Configuring host base addresses in WCF
on 07-13-2006 11:05 AM
Rob Zelt : streams of thought wrote WCF: This collection already contains an address with scheme http (Update)
on 02-08-2007 7:56 PM
Joe's Blog wrote Hosting WCF Services in IIS
on 04-08-2007 3:06 AM
Testing services on your local machine is grand, though there is often a hiccup once you put it on your
Keyse wrote re: Reading ServiceHost base address from configuration in WCF
on 04-14-2008 7:41 AM
Thanks for the info. I was wondering about base addresses as well.

Add a Comment

(required)  
(optional)
(required)  
Remember Me?