I just read this great answer, how to inject dependencies into WCF service. The solution is pretty neat, but I have a problem.
Solution in this answer uses self-hosting (console application), so it's possible to implement own ServiceHost and ErrorHandler and do:
var logger = new DummyLogger();
var errorHandler = new TestErrorHandler(logger);
ServiceHost host = new TestServiceHost(errorHandler, typeof(TestService), new Uri("net.tcp://localhost:8002"));
host.Open();
My problem is, how can I do this in ASP.NET Web application host? I'm trying to inject something into Service while hosting via IIS. Any help would be great!
Castle Windsor's WCF Integration Facility does this.
Add the nuget package to your WCF project. In your application startup (global.asax or some bootstrap class you create):
Then add an installer class.
Container.Install(FromAssembly.This())will execute any installers in your service.Finally, edit the markup for your YourService.svc to specify that Windsor should create service instances:
Now you can have ILogger as an argument in your service's constructor. Windsor will create each instance of the service and will provide that argument for the constructor.
I use this for just about every WCF I create.
Your comment mentioned that you use Ninject. I found this documentation which shows how to wire up Ninject with WCF.
It's nearly identical. The markup for a service looks like this:
Then the Ninject equivalent of a Windsor installer:
Then in
global.asaxThe only thing that seems off is that you have to inherit from
NinjectWcfApplication. inglobal.asax. That seems a little intrusive.