I have a WCF Service Library containing a custom ServiceHostFactory derived from DefaultServiceHostFactory. I can't get the test client to use this factory. I just get the "no parameterless constructor was found" error.
Here's my hosting enviroment configuration:
<serviceHostingEnvironment>
<serviceActivations>
<add service="TestService.WcfLibrary.TestService"
relativeAddress="/TestService.svc"
factory="TestService.WcfLibrary.TestServiceHostFactory, TestService.WcfLibrary, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"/>
</serviceActivations>
</serviceHostingEnvironment>
Note that I don't have a .svc file actually. I am trying to use fileless activation.
Here's my custom ServiceHostFactory:
public class TestServiceHostFactory : DefaultServiceHostFactory
{
public TestServiceHostFactory() : base(CreateKernel()) { }
private static IKernel CreateKernel()
{
WindsorContainer container = new WindsorContainer();
container.AddFacility<WcfFacility>();
container.Register(Component.For<TestService>());
container.Register(Component.For<IServiceManager>().ImplementedBy<ServiceManager>());
return container.Kernel;
}
}
It looks like this path is never executed. How can I get WCF Test Client to use my custom implementation?
OK, this is possible, but it aint pretty...
First of all we need a way to hook in when the assembly is loaded since in this scenario their is no "main" or "application start" or anything. There is an interesting event on AppDomain called AssemblyLoaded that looks like it may do the trick, hmm, but how to hook into it, a way I thought to do this is to define a custom app domain manager so...
Create a brand new assembly and inside it define an interface, which can be implemented by some client and an AppDomainManager like so:
Make sure the assembly is signed and then add it to the GAC. Let's say I called the assembly AssemblyInitCustomDomainManager I can add it like so (and I get back the details about it straight away because I will need them):
Now edit the WcfServiceHost.exe.config (typically located at: C:\Program Files\Microsoft Visual Studio 10.0\Common7\IDE or the x86 version on 64 bit systems) and add the following inside the runtime element (see here for info about this settings):
NOTE: You will need to change at least one of (and possibly all depending on what you called things above): type name, namespace, assembly name, public key, version number. I guess you should be able to figure out what they need to be in your case.
OK, that was easy enough, now we will create a new "WCF Service Library" project in visual studio and it will create an app.config and a service for us (this is the project type you are wanting to test right, oh I hope so!).
First of all, remove the system.servicemodel part from the app.config since we do not want the service host application to read this in and then delete the Service1.cs and IService1.cs (as I am going to make my own a bit later). Make sure that you reference the app domain manager assembly you created earlier since you need to implement that interface.
Now, create a new file and stick the following code in (we are just having a simple service with a dependency hosted by the Castle WCF Facility):
Click run and you will get an error message that says:
Don't worry, just click no.
The test client starts up, but sadly it does not have your service in it. No worries, just right-click add service... and put in the URL of the service (it is in the installer in the code - http://localhost:9777/TestWcfServiceLibrary/Service1).
And there you go - WCF service hosted inside the WCF Test Client. Don't believe me - test it out, invoke the GetData operation and you should see a result.
There you go. Now if you asked if this is a good idea...I do not know but it works and I think it is what you asked for...