So, I have implemented a stateful Wcf Service self-hosted inside a windows service.
I use this service as a remote hardware driver (kind of).
As the service has to force serialed access to the underlying hardware, I set the service as a unique single instance with the attribute:
[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]
It works correctly but now i want to have Castle manage dependencies for me, and here troubles start.
I found out that dependency injection and Single Instance Mode do not work well together (here is a reference, but you can find many more googling)...
Please note that I cannot use standard singletons provided by Castle as I need a different instance for every device installed in the system (there can be multiple at the same time). So using Castle default lifetime is not an option for me.
EDIT
As requested, I added the code I use to register and resolve the service, even if I think it's quite standard and there is nothing to underline.
This is the code I use to register the service:
container.Register(Component.For<IMyService>().ImplementedBy<MyService>().LifeStyle.Transient);
And this is the code I use to resolve the service:
public ServiceHostBase Build<T>(SelfHostConfiguration configuration)
{
// Service configuration is done by code, not by App.config
Uri[] uris = configuration.Select(e => e.Uri).ToArray();
var serviceName = typeof(T).AssemblyQualifiedName;
DefaultServiceHostFactory factory = new DefaultServiceHostFactory();
var serviceHost = factory.CreateServiceHost(serviceName, uris);
return serviceHost;
}
Nothing special, really, still maybe it'll be useful.
The question: is there anybody that was able to make Castle and InstanceContextMode.Single work together? Is it even possible?
Can you share your solution/ideas?