I have a WCF Service hosted in IIS. This solution is composed of 2 projects: Service and Data. Service depends on Data, like so:
Service -> Data
I've been trying to invert the dependency, like so:
Service <- Data
Which is quite a headache using WCF, since the WCF service constructor must be parameter-less (by default).
I hear it's possible to inject the dependency using Ninject and its WCF extension, so I tried to integrate it to my solution, but it's still not clear to me in which project should be the related files and references? What I did is :
- Download Ninject using NuGet
- Add Ninject to both my Data and Service projects (that created the NinjectWebCommon file in the App_Start folder of the Service Project
- Create a IDataProxy interface in my Service project
- Implement the interface in my Data project
- Add a IDataProxy argument to the WCF service constructor
- Added the factory configuration in the .svc file markup
Up to that point, I'm pretty sure I'm doing it right. Now the shaky part :
I created a DataInjectionModule in my data project with this code :
namespace Data { public class DataInjectionModule : NinjectModule { public override void Load() { Bind<IResolutionRoot>().ToConstant(Kernel); Bind<ServiceHost>().To<NinjectServiceHost>(); Bind<IDataProxy>().To<DataProxy>(); } } }
I finally tried to register the service in the NinjectWebCommon files (of both projects to be sure) like that :
/// <summary> /// Load your modules or register your services here! /// </summary> /// <param name="kernel">The kernel.</param> private static void RegisterServices(IKernel kernel) { kernel.Bind<IService>().To<Service>() .WithConstructorArgument("IDataProxy", context => context.Kernel.Get<IDataProxy>()); }
When I try to start my service, I still get this :
The service type provided could not be loaded as a service because it does not have a default (parameter-less) constructor. To fix the problem, add a default constructor to the type, or pass an instance of the type to the host.
I have a feeling that the problem resides in the fact that I did not bind my DataInjectionModule in the kernel, but if I try to do so, I must add a dependency from Service to Data, which is what I'm trying to avoid.
General expert advice would be greatly appreciated. Thanks.