Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ninject 3, WCF Service and parameterized constructor

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 :

  1. Download Ninject using NuGet
  2. Add Ninject to both my Data and Service projects (that created the NinjectWebCommon file in the App_Start folder of the Service Project
  3. Create a IDataProxy interface in my Service project
  4. Implement the interface in my Data project
  5. Add a IDataProxy argument to the WCF service constructor
  6. 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 :

  1. 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>();
            }
        }
    }
    
  2. 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.

like image 928
Philippe Desjardins Avatar asked Oct 21 '22 08:10

Philippe Desjardins


1 Answers

Please check your point 6: "Added the factory configuration in the .svc file markup."

Have you done it properly?

The *.svc file should have this code:

Factory="Ninject.Extensions.Wcf.NinjectServiceHostFactory"
like image 109
Daniel Heo Avatar answered Oct 24 '22 09:10

Daniel Heo