我有兴趣在.NET 4.5上运行的ASP.NET 4.5(MVC5)项目中使用一些ASP.NET 5程序包,如Logging和Dependency Injection。我已成功安装了来自nuget的两个软件包,并且似乎能够正常使用它们。我有什么理由不这样做(除了他们当然处于测试阶段的事实)?
答案 0 :(得分:0)
不在ASP.NET MVC中使用ASP.NET 5依赖注入的一个原因是它没有提供ASP.NET 4.6所需的许多功能来使其工作。例如。
像Autofac这样的东西会为你做这件事,并且设计用于ASP.NET 4.6。这是一个使用Autofac开始的示例类:
/// <summary>
/// Register types into the Autofac Inversion of Control (IOC) container. Autofac makes it easy to register common
/// MVC types like the <see cref="UrlHelper"/> using the <see cref="AutofacWebTypesModule"/>. Feel free to change
/// this to another IoC container of your choice but ensure that common MVC types like <see cref="UrlHelper"/> are
/// registered. See http://autofac.readthedocs.org/en/latest/integration/aspnet.html.
/// </summary>
public partial class Startup
{
public static void ConfigureContainer(IAppBuilder app)
{
IContainer container = CreateContainer();
app.UseAutofacMiddleware(container);
// Register MVC Types
app.UseAutofacMvc();
}
private static IContainer CreateContainer()
{
ContainerBuilder builder = new ContainerBuilder();
builder.RegisterType<MyService>().As<IMyService>().InstancePerRequest();
// Register Common MVC Types
builder.RegisterModule<AutofacWebTypesModule>();
// Register MVC Filters
builder.RegisterFilterProvider();
// Register MVC Controllers
builder.RegisterControllers(Assembly.GetExecutingAssembly());
IContainer container = builder.Build();
DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
return container;
}
}