Thanks to those of you that have sent me comments on my CustomHostFactory post. I wanted to post publicly question that I received from a couple of people.
While specifying a hard coded BaseAddress array works, it's not really practice for deploying applications in multiple environments and updating configuration changes in the future. I set out to find a better approach to this, and found a blog post by Aaron Skonnard here that add a simple configuration capability by reading the following section of a web.config file.
<appSettings>
<add key ="baseHttpAddress" value=http://localhost/ServiceTest/service.svc/>
</appSettings>
My updated CustomHostFactory code is as follows
using System;
using System.Collections.Generic;
using System.Text;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.Configuration;
class CustomHostFactory : ServiceHostFactory
{
protected override ServiceHost CreateServiceHost(Type serviceType, Uri[] baseAddresses)
{
CustomHost customServiceHost =
new TradeServiceCustomHost(serviceType, baseAddresses);
return customServiceHost;
}
}
class CustomHost : ServiceHost
{
public CustomHost(Type serviceType, params Uri[] baseAddresses)
: base(serviceType, GetBaseAddresses())
{ }
protected override void ApplyConfiguration()
{
base.ApplyConfiguration();
}
// read base addresses from AppSettings in config
// Added from sample at http://pluralsight.com/blogs/aaron/archive/2006/04/19/22106.aspx
private static Uri[] GetBaseAddresses()
{
List<Uri> addresses = new List<Uri>();
AddBaseAddress(addresses, "baseHttpAddress");
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));
}
}
It would be really great if there was a way to remove base address through the web.config similar to how asp.net lets you remove providers and other settings with something to the effect of
<BaseAddresses>
<Remove All />
<Add BaseAddress="http;//..." />
</BaseAddresses>
Posted in wcf |