标签:c wcf ninject ninject-extensions
我正在尝试使用WCF Ninject扩展将Ninject添加到WCF服务.
我收到错误:
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 Service Host工厂:
<%@ 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始终为null.
我错过了什么?
解决方法:
我遇到该错误消息的唯一一次是在尝试使用拦截器时(通过使用Ninject.Extensions.Interceptor库和/或Castle DynamicProxy.)这是不喜欢带参数的构造函数的部分.
否则,这应该工作正常.你似乎没有使用任何拦截器,所以我可以问这个目的是什么?:
this.Bind<ServiceHost>().To<NinjectServiceHost>();
我假设你在这里使用某种自定义服务主机,但它不应该是你想要做的事情.使您的上述代码工作所需的只是:
1:服务标记中的Factory属性(你有这个)
2:内核中的构造函数依赖项绑定(你有这个)
我现在有这个确切的设置工作,所以我认为你的NinjectServiceHost中的某些东西导致了这个问题并试图附加某种类型的拦截器.
标签:c,wcf,ninject,ninject-extensions 来源: https://codeday.me/bug/20190703/1369316.html
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。