Constructor WCF / Ninject / Default (no parameter)

I am trying to add Ninject to a WCF service using the WCF Ninject extension.

I get an error message:

The provided service type cannot be loaded as a service because it does not have a default constructor (without a parameter). To fix the problem, add a default constructor to the type or pass an instance of this type to the host.

The service has a Ninject factory service host:

<%@ ServiceHost Language="C#" Debug="true" CodeBehind="SchedulingSvc.svc.cs" Service="Scheduling.SchedulingSvc" Factory="Ninject.Extensions.Wcf.NinjectWebServiceHostFactory" %> 

The global.asax file is inherited from NinjectHttpApplication, and CreateKernel returns the new kernel using the NinjectModule:

 public class Global : NinjectHttpApplication { protected override IKernel CreateKernel() { return new StandardKernel(new NinjectServiceModule()); } } 

NinjectModule:

 public class NinjectServiceModule : NinjectModule { public override void Load() { this.Bind<ISchedulingService>().To<SchedulingSvc>(); this.Bind<ISchedulingBusiness>().To<SchedulingBusiness>(); } } 

Service with constructor installation:

 [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)] public class SchedulingSvc : ISchedulingService { private ISchedulingBusiness _SchedulingBusiness = null; public SchedulingSvc(ISchedulingBusiness business) { _SchedulingBusiness = business; } public CalendarEvent[] GetCalendarEvents() { var calendarEvents = _SchedulingBusiness.GetCalendarEvents(); return calendarEvents; } ... } 

Service with the introduction of properties:

 [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)] public class SchedulingSvc : ISchedulingService { [Inject] public ISchedulingBusiness _SchedulingBusiness { get; set; } public SchedulingSvc() { } public CalendarEvent[] GetCalendarEvents() { var calendarEvents = _SchedulingBusiness.GetCalendarEvents(); return calendarEvents; } ... } 

If I use the constructor, I get the error indicated at the top of the message. If I try to use property injection, _ScheduleBusiness is always null.

What am I missing?

+4
source share
3 answers

The only time I encounter this error message is an attempt to use Interceptor (using the Ninject.Extensions.Interceptor library and / or Castle DynamicProxy.) This is the part that the constructors with parameters dislike.

Otherwise, it should work fine. It seems you are not using any interceptors, so I may ask, what is the purpose of this ?:

 this.Bind<ServiceHost>().To<NinjectServiceHost>(); 

I assume that you are using some kind of custom service node here, but this is not necessary for what you are trying to do. All you need to get the code above:

1: Factory attribute in service markup (you have this) 2: related constructor dependency in your kernel (you have this)

I have this fine-tuning working right now, so I think something in your NinjectServiceHost is causing this problem and trying to hook some kind of interceptor.

+1
source

Well, I think this should do it. Your Ninject installation may be disabled. This used to happen before, as there are several β€œmethods” for setting it up, but some of them do not always work.

I see that you are using AspNet compatibility mode and hosting this through a web project. You are using Ninject.Web, right? In fact, you do not need to have your global inheritance from Ninject. Remove all Ninject references and codes from Global and dispose of your NinjectServiceModule. If you installed Ninject.Web from NuGet, it should have created the file " NinjectWebCommon.cs " in the App_Start folder. Here you configure your bindings, for example:

  private static IKernel CreateKernel() { var settings = new NinjectSettings { InjectNonPublic = true }; var kernel = new StandardKernel(settings); kernel.Bind<Func<IKernel>>().ToMethod(ctx => () => new Bootstrapper().Kernel); kernel.Bind<IHttpModule>().To<HttpApplicationInitializationHttpModule>(); RegisterServices(kernel); return kernel; } /// <summary> /// Load your modules or register your services here! /// </summary> /// <param name="kernel">The kernel.</param> private static void RegisterServices(IKernel kernel) { kernel.Bind<AdminHelper>().ToSelf().InRequestScope(); kernel.Bind<AuditHelper>().ToSelf().InRequestScope(); kernel.Bind<ErrorHelper>().ToSelf().InRequestScope(); kernel.Bind<GoalHelper>().ToSelf().InRequestScope(); } 

This should already have these methods, and most of them are already populated, so you only need to populate RegisterServices with your bindings.

The App_Start folder should also have NinjectWeb.cs . You do not need to make changes to it; just make sure it exists.

You may also need to change the Factory attribute in your service markup back to "Ninject.Extensions.Wcf.NinjectServiceHostFactory" after that. Or try both of them and see what works.

These 2 files are all that Ninject needs to work. Try to go down this route; I have a suspicion that this will fix.

+1
source

I think that WCF service (or any website) needs a constructor with a lower value to instantiate the service. Since you provided a parameterized constructor, Ninject may not have been able to initialize your service.

Pls. see also this link http://social.msdn.microsoft.com/Forums/vstudio/en-US/550d82eb-a2bc-4c08-b846-e3fb1b966457/no-parameterless-constructor-error-in-wcf

0
source

Source: https://habr.com/ru/post/1487056/


All Articles