我试图让构造函数注入在 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() { }
...
请帮忙!!!!
谢谢你。