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