我试图让构造函数注入在WCF应用程序中工作,但它调用的是无参数构造函数而不是具有[inject]作为参数的构造函数。我通过Nuget安装了Ninject WCF,它为我安装了依赖项。
这是我的包裹:
<packages>
<package id="Microsoft.Web.Infrastructure" version="1.0.0.0" targetFramework="net40" />
<package id="Ninject" version="3.0.1.10" targetFramework="net40" />
<package id="Ninject.Extensions.Wcf" version="3.0.0.5" targetFramework="net40" />
<package id="Ninject.Web.Common" version="3.0.0.7" targetFramework="net40" />
<package id="WebActivator" version="1.5.1" targetFramework="net40" />
</packages>
它为我创建了一个NinjectWebCommon.cs类并将其放在App_Start中。下面是我从该文件的内核绑定。注意:我正在使用实体框架pocos,并且需要上下文来实例化repo。
private static void RegisterServices(IKernel kernel)
{
var camContext = new STCAMx0Entities();
var iseaContext = new iseaEntities();
kernel.Bind<IRepository<EnrgyProfl>>().To<EnrgyProflRepository>().InRequestScope().WithConstructorArgument("context", camContext);
kernel.Bind<IRepository<GenUnitCost>>().To<GenUnitCostRepository>().InRequestScope().WithConstructorArgument("context", camContext);
kernel.Bind<IRepository<LglGenUnit>>().To<LglGenUnitRepository>().InRequestScope().WithConstructorArgument("context", camContext);
kernel.Bind<IRepository<dly_sch>>().To<DlySchRepository>().InRequestScope().WithConstructorArgument("context", iseaContext);
kernel.Bind<IRepository<DealModel>>().To<WtDealRepository>().InRequestScope().WithConstructorArgument("context", iseaContext);
kernel.Bind<IRepository<EnrgyProfl>>().To<WtEnergyProfileRepository>().InRequestScope().WithConstructorArgument("context", iseaContext);
}
在我的服务中,我将此作为我的构造函数:
[Inject]
public CamService(IRepository<dly_sch> dlySchRepo, IRepository<dly_unit> dlyUnitRepo, IRepository<LglGenUnit> lglGenRepo, IRepository<EnrgyProfl> enrgyProflRepo, IRepository<DealModel> wtDealRepo)
{
_dlySchRepo = dlySchRepo;
_dlyUnitRepo = dlyUnitRepo;
_lglGenRepo = lglGenRepo;
_enrgyProflRepo = enrgyProflRepo;
_wtDealRepo = wtDealRepo;
}
以下是我的一个存储库的示例:
public class DlySchRepository : IRepository<dly_sch>
{
private static readonly CommonLogger Log = LogFactory.Singleton.Create(typeof (DlySchRepository));
private iseaEntities _context = new iseaEntities();
public DlySchRepository(iseaEntities context) { _context = context; }
public DlySchRepository() { }
...
请帮助!!!!
谢谢。