在 ASP.NET MVC 5 中使用 ControllerFactory 在控制器的构造函数中进行依赖注入
Dependency injection in constructor of controller with using a ControllerFactory in ASP.NET MVC 5
我正在开发 ASP.NET MVC 5 应用程序。我需要在控制器的构造函数中使用参数。 DefaultControllerFactory 无法解析它,我从它继承了我自己的 ControllerFactory:
public class ControllerFactoryProvider : DefaultControllerFactory
{
public IController CreateController(System.Web.Routing.RequestContext requestContext, string controllerName)
{
string controllerType = string.Empty;
IController controller = null;
// Read Controller Class & Assembly Name from Web.Config
controllerType = ConfigurationManager.AppSettings[controllerName];
if (controllerType == null)
throw new ConfigurationErrorsException("Assembly not configured for controller " + controllerName);
// Create Controller Instance
IDataTransmitter _dataTransmitter = new DataTransmitter();
controller = Activator.CreateInstance(Type.GetType(controllerType), _dataTransmitter) as IController;
return controller;
}
public void ReleaseController(IController controller)
{
//This is a sample implementation
//If pooling is used to write code to return the object to pool
if (controller is IDisposable)
{
(controller as IDisposable).Dispose();
}
controller = null;
}
}
我在Global.asax:
注册了
ControllerBuilder.Current.SetControllerFactory(new
ControllerFactoryProvider());
但是当我 运行 我的应用程序无论使用 DefaultControllerFactory 时都没有看到带参数的构造函数。
哪里会出错?
正如我在评论中所说,无需覆盖您的控制器工厂。你只需要插入你喜欢的依赖注入容器。
我还没有机会使用 .net 的每个依赖项注入容器,但我会尝试给出 objective 答案。
Ninject
在 asp.net Mvc 5
项目中设置 Ninject
非常简单。
正在安装 nuget 包
有一个非常方便的 nuget package
叫做 Ninject.MVC5
。
可以安装:
- 使用
manage nuget packages
对话,或
- 通过 运行
Install-Package Ninject.MVC5
在包管理器控制台中。
安装 Ninject.MVC5
后,您将在 App_Start/
的解决方案中看到一个名为 NinjectWebCommon.cs
的新文件。 Here 您可以看到该文件的内容最终会是什么。
连接你的依赖项
现在安装包后,您想要使用 ninject 的 api.
注册您的依赖项
假设您有一个 IFoo
接口及其实现 Foo
public interface IFoo
{
int Bar()
}
public class Foo : IFoo
{
public int Bar()
{
throw new NotImplementedException();
}
}
在您的 NinjectWebCommon
class 中,您将告诉 ninject
如何解析 IFoo
接口:
/// <summary>
/// Load your modules or register your services here!
/// </summary>
/// <param name="kernel">The kernel.</param>
private static void RegisterServices(IKernel kernel)
{
kernel.Bind<IFoo>().To<Foo>();
}
请记住,默认情况下 Ninject 有 implicit self binding of concrete types,这意味着
If the type you’re resolving is a concrete type (like Foo above), Ninject will automatically create a default association via a mechanism called implicit self binding. It’s as if there’s a registration like this:
Bind<Foo>().To<Foo>();
我正在开发 ASP.NET MVC 5 应用程序。我需要在控制器的构造函数中使用参数。 DefaultControllerFactory 无法解析它,我从它继承了我自己的 ControllerFactory:
public class ControllerFactoryProvider : DefaultControllerFactory
{
public IController CreateController(System.Web.Routing.RequestContext requestContext, string controllerName)
{
string controllerType = string.Empty;
IController controller = null;
// Read Controller Class & Assembly Name from Web.Config
controllerType = ConfigurationManager.AppSettings[controllerName];
if (controllerType == null)
throw new ConfigurationErrorsException("Assembly not configured for controller " + controllerName);
// Create Controller Instance
IDataTransmitter _dataTransmitter = new DataTransmitter();
controller = Activator.CreateInstance(Type.GetType(controllerType), _dataTransmitter) as IController;
return controller;
}
public void ReleaseController(IController controller)
{
//This is a sample implementation
//If pooling is used to write code to return the object to pool
if (controller is IDisposable)
{
(controller as IDisposable).Dispose();
}
controller = null;
}
} 我在Global.asax:
注册了ControllerBuilder.Current.SetControllerFactory(new ControllerFactoryProvider());
但是当我 运行 我的应用程序无论使用 DefaultControllerFactory 时都没有看到带参数的构造函数。
哪里会出错?
正如我在评论中所说,无需覆盖您的控制器工厂。你只需要插入你喜欢的依赖注入容器。
我还没有机会使用 .net 的每个依赖项注入容器,但我会尝试给出 objective 答案。
Ninject
在 asp.net Mvc 5
项目中设置 Ninject
非常简单。
正在安装 nuget 包
有一个非常方便的 nuget package
叫做 Ninject.MVC5
。
可以安装:
- 使用
manage nuget packages
对话,或 - 通过 运行
Install-Package Ninject.MVC5
在包管理器控制台中。
安装 Ninject.MVC5
后,您将在 App_Start/
的解决方案中看到一个名为 NinjectWebCommon.cs
的新文件。 Here 您可以看到该文件的内容最终会是什么。
连接你的依赖项
现在安装包后,您想要使用 ninject 的 api.
注册您的依赖项假设您有一个 IFoo
接口及其实现 Foo
public interface IFoo
{
int Bar()
}
public class Foo : IFoo
{
public int Bar()
{
throw new NotImplementedException();
}
}
在您的 NinjectWebCommon
class 中,您将告诉 ninject
如何解析 IFoo
接口:
/// <summary>
/// Load your modules or register your services here!
/// </summary>
/// <param name="kernel">The kernel.</param>
private static void RegisterServices(IKernel kernel)
{
kernel.Bind<IFoo>().To<Foo>();
}
请记住,默认情况下 Ninject 有 implicit self binding of concrete types,这意味着
If the type you’re resolving is a concrete type (like Foo above), Ninject will automatically create a default association via a mechanism called implicit self binding. It’s as if there’s a registration like this:
Bind<Foo>().To<Foo>();