Alternative WCF ServiceHost

I'm writing a test harness that will try out different WCF services and allow us to fire test data at them.  But I also want to be able to override some of the behaviour of the WCF services during the tests.  So I've built some code to look inside the assemblies in a particular folder and determine if they contain any classes that implement the WCF service interfaces.  Then I can create instances of those classes at runtime and host them as WCF services inside my test harness.  This means that I can switch between various new WCF classes from the UI of the test harness.  However, when swapping the service at runtime, I was getting this error: System.InvalidOperationException: Service 'TestHarness.ATestService' has zero application (non-infrastructure) endpoints.  That's because I was now using a differently named WCF service than the one named in the config file.  So, this was my solution:


public class NamedServiceHost : ServiceHost


    public static string configName { get; set; }


    public NamedServiceHost(object singletonInstance, params Uri[] baseAddresses)

        : base(singletonInstance, baseAddresses)

    { }


    protected override void ApplyConfiguration()


        if (!string.IsNullOrEmpty(configName))


            Description.ConfigurationName = configName;






I can use it instead of the normal ServiceHost class, like this:


NamedServiceHost.configName = "MyNamespace.MyService";

ServiceHost = new NamedServiceHost(singletonInstance);



Now I can tell the service host what name I'd like to use from the config file, rather than letting it pick based on the name of the WCF class.  Unfortunately the ApplyConfiguration() method is called from the constructor of the base class, so the easiest way to tell the class what configuration name you'd like to use is by setting a static property.