Ninject in MVC 和 WebAPI 来创建抽象工厂
本文关键字:创建 抽象 工厂 WebAPI in MVC Ninject | 更新日期: 2023-09-27 18:34:31
我在C# 4.5项目中使用Ninject作为IoC容器。我正在尝试使用 Ninject 注入到抽象工厂模式中,该模式在 MVC 中运行良好,但在 Web API 中不起作用。
我安装了以下内容:
Ninject 3.0.1.10
Ninject.Extensions.WCF 3.0.0.5
Ninject.MVC3 3.0.0.6
Ninject.Web.Common 3.0.0.7
SignalR.Ninject
我的工厂类如下所示:
public interface IBusinessEngineFactory
{
T GetBusinessEngine<T>() where T : IBusinessEngine;
}
public interface IBusinessEngine
{
}
public class BusinessEngineFactory : IBusinessEngineFactory
{
private IKernel Kernel { get; set; }
public BusinessEngineFactory(IKernel kernel)
{
Kernel = kernel;
}
T IBusinessEngineFactory.GetBusinessEngine<T>()
{
return Kernel.Get<T>();
}
}
我有标准的NinjectWebCommon:
公共静态类 NinjectWebCommon { 私有静态只读引导程序引导程序 = 新引导程序((;
/// <summary>
/// Starts the application
/// </summary>
public static void Start()
{
DynamicModuleUtility.RegisterModule(typeof(OnePerRequestHttpModule));
DynamicModuleUtility.RegisterModule(typeof(NinjectHttpModule));
bootstrapper.Initialize(CreateKernel);
}
/// <summary>
/// Stops the application.
/// </summary>
public static void Stop()
{
bootstrapper.ShutDown();
}
/// <summary>
/// Creates the kernel that will manage your application.
/// </summary>
/// <returns>The created kernel.</returns>
private static IKernel CreateKernel()
{
var kernel = new StandardKernel();
kernel.Bind<Func<IKernel>>().ToMethod(ctx => () => new Bootstrapper().Kernel);
kernel.Bind<IHttpModule>().To<HttpApplicationInitializationHttpModule>();
RegisterServices(kernel);
// Install our Ninject-based IDependencyResolver into the Web API config
GlobalConfiguration.Configuration.DependencyResolver = new NinjectResolver(kernel);
return kernel;
}
/// <summary>
/// Load your modules or register your services here!
/// </summary>
/// <param name="kernel">The kernel.</param>
private static void RegisterServices(IKernel kernel)
{
kernel.Bind<IUserIdentity>().To<UserIdentity>();
kernel.Bind<IUnitOfWork>().To<UnitOfWork>();
kernel.Bind<IAlarmEngine>().To<AlarmEngine>();
kernel.Bind<IBusinessEngineFactory>().To<BusinessEngineFactory>();
}
}
这是在控制器中设置的,因此
public class UserController : BaseController
{
public UserController(IBusinessEngineFactory businessEngineFactory, IUnitOfWork unitOfWork, UserIdentity userIdentity)
: base(businessEngineFactory, unitOfWork, userIdentity)
{
}
...
public ActionResult Edit(int id = 0)
{
...
var x = BusinessEngineFactory.GetBusinessEngine<IAlarmEngine>();
x.ProcessAlarmEventUpdate("test");
return View(x);
}
}
哪个工作正常。我遇到问题的地方是WebAPIController中的相同代码:
public class AlarmWebApiController : ApiController
{
public IBusinessEngineFactory BusinessEngineFactory { get; set; }
public IUnitOfWork UnitOfWork {get; set;}
public IUserIdentity UserIdentity { get; set; }
public AlarmWebApiController(IBusinessEngineFactory businessEngineFactory, IUnitOfWork unitOfWork, IUserIdentity userIdentity)
{
if (businessEngineFactory == null) throw new NullReferenceException("businessEngineFactory");
BusinessEngineFactory = businessEngineFactory;
if (unitOfWork == null) throw new NullReferenceException("unitOfWork");
UnitOfWork = unitOfWork;
if (userIdentity == null) throw new NullReferenceException("userIdentity");
UserIdentity = userIdentity;
}
}
当我运行它时,MVC 控制器处理正常,但 Web API 函数没有,并且我收到在调用 webapi 构造函数中的任何代码之前发生的模糊错误消息。
"加载 Ninject 组件 ICache 时出错
内核的组件容器中未注册任何组件">
我认为问题是MVC具有内核知识,但因为WebAPI使用它:
GlobalConfiguration.Configuration.DependencyResolver = new NinjectResolver(kernel);
所以它不知道内核。用于创建业务引擎的代码存在于与主MVC,Web API和ninject创建不同的项目中,如果我能提供帮助,我宁愿不将依赖项粘贴到System.Web.Http(对于GlobalConfiguration.Configuration.DependencyResolver对象(。有没有办法让它在MVC和WebAPI上工作?
提前致谢
我修复了这个问题,以便 IoC 注入抽象工厂并在 MVC 和 WebAPI 中返回正确的具体实现。最后,我的解决方案具有以下软件包:
<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.MVC3" version="3.0.0.6" targetFramework="net40" />
<package id="Ninject.Web.Common" version="3.0.0.7" targetFramework="net40" />
<package id="Ninject.Web.WebApi-RC" version="3.0.0.22" targetFramework="net45" />
已将业务引擎工厂修改为使用以下:
public class BusinessEngineFactory : IBusinessEngineFactory
{
private IResolutionRoot resolutionRoot { get; set; }
public BusinessEngineFactory(IResolutionRoot resolutionRoot)
{
this.resolutionRoot = resolutionRoot;
}
T IBusinessEngineFactory.GetBusinessEngine<T>()
{
return this.resolutionRoot.Get<T>();
}
}
我尝试使用Ninject.Web.WebApi软件包,该软件包不起作用,Ninject.Web.WeApi.Update阻止了我的解决方案运行,并在应用程序启动时显示"调用目标已引发异常"错误消息。回到旧版本是解决方案,所以现在一切正常。我什至检查了它在 WCF 中也有效!