我想添加Ninject到使用WCF Ninject擴展的WCF服務。WCF/Ninject /默認(無參數)構造函數
,我發現了錯誤:
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.
服務有Ninject服務主機工廠:
<%@ ServiceHost Language="C#" Debug="true" CodeBehind="SchedulingSvc.svc.cs"
Service="Scheduling.SchedulingSvc"
Factory="Ninject.Extensions.Wcf.NinjectWebServiceHostFactory" %>
Global.asax文件從NinjectHttpApplication繼承和CreateKernel返回一個新內核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>();
}
}
與構造器注入的服務:
[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;
}
...
}
財產注射服務:
[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;
}
...
}
如果我使用構造函數注入,我會在後頂部提到的錯誤。如果我嘗試使用屬性注入,_ScheduleBusiness始終爲空。
我錯過了什麼?
您是否註冊過'SchedulingBusiness'的所有必需的依賴關係? – mipe34
@ mipe34是的。當我將業務插入Ninject的控制檯應用程序時,所有工作都按預期進行。 –