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?
source share