在 MVC 6 中访问依赖注入服务
Accessing dependency injected services in MVC 6
我在 vs2015 rc 中使用 mvc6。阅读 Using IConfiguration globally in mvc6 后,我的代码现在看起来像这样:
startup.cs:
public void ConfigureServices(IServiceCollection services)
{
...
IConfiguration configuration = new Configuration().AddJsonFile("config.json");
services.Configure<Settings>(configuration);
}
我的控制器:
private Settings options;
public MyController(IOptions<Settings> config)
{
options = config.Options;
}
这对控制器非常有用。
但是我如何从代码中的其他地方访问我的设置对象,例如从我的格式化程序(实现 IInputFormatter,因此具有固定签名)或任何其他随机 class?
一般来说,只要您有权访问 HttpContext
,就可以使用名为 RequestServices
的 属性 来访问 DI 中的服务。例如,要从 ObjectResult
.
中访问 ILogger
服务
public override async Task ExecuteResultAsync(ActionContext context)
{
var logger = context.HttpContext.RequestServices.GetRequiredService<ILogger<ObjectResult>>();
上面的用法与你在上面的控制器示例中提到的有点不同,因为 MVC 中的控制器是 type activated
...也就是说,构建控制器的责任是通过 DI 管理的,它将查看构造函数的参数并从 DI 中的注册服务填充它们...这也称为 constructor injection
...
要了解 DI 类型激活的一般工作原理(DI 独立于 MVC,甚至可以在控制台应用程序中使用),请查看以下代码片段(摘自 http://docs.asp.net/en/latest/security/data-protection/using-data-protection.html)
using System;
using Microsoft.AspNet.DataProtection;
using Microsoft.Framework.DependencyInjection;
public class Program
{
public static void Main(string[] args)
{
// add data protection services
var serviceCollection = new ServiceCollection();
serviceCollection.AddDataProtection();
var services = serviceCollection.BuildServiceProvider();
// create an instance of MyClass using the service provider
var instance = ActivatorUtilities.CreateInstance<MyClass>(services);
instance.RunSample();
}
public class MyClass
{
IDataProtector _protector;
// the 'provider' parameter is provided by DI
public MyClass(IDataProtectionProvider provider)
{
_protector = provider.CreateProtector("Contoso.MyClass.v1");
}
public void RunSample()
{
Console.Write("Enter input: ");
string input = Console.ReadLine();
// protect the payload
string protectedPayload = _protector.Protect(input);
Console.WriteLine($"Protect returned: {protectedPayload}");
// unprotect the payload
string unprotectedPayload = _protector.Unprotect(protectedPayload);
Console.WriteLine($"Unprotect returned: {unprotectedPayload}");
}
}
}
在上面的示例中,类型 MyClass
被激活。
关于您关于 InputFormatter
的具体示例,它们是 未激活的 类型,因此您不能对其使用构造函数注入,但您可以访问 HttpContext
所以你可以按照前面提到的 post.
也看看这篇文章:http://blogs.msdn.com/b/webdev/archive/2014/06/17/dependency-injection-in-asp-net-vnext.aspx
我在 vs2015 rc 中使用 mvc6。阅读 Using IConfiguration globally in mvc6 后,我的代码现在看起来像这样:
startup.cs:
public void ConfigureServices(IServiceCollection services)
{
...
IConfiguration configuration = new Configuration().AddJsonFile("config.json");
services.Configure<Settings>(configuration);
}
我的控制器:
private Settings options;
public MyController(IOptions<Settings> config)
{
options = config.Options;
}
这对控制器非常有用。
但是我如何从代码中的其他地方访问我的设置对象,例如从我的格式化程序(实现 IInputFormatter,因此具有固定签名)或任何其他随机 class?
一般来说,只要您有权访问 HttpContext
,就可以使用名为 RequestServices
的 属性 来访问 DI 中的服务。例如,要从 ObjectResult
.
ILogger
服务
public override async Task ExecuteResultAsync(ActionContext context)
{
var logger = context.HttpContext.RequestServices.GetRequiredService<ILogger<ObjectResult>>();
上面的用法与你在上面的控制器示例中提到的有点不同,因为 MVC 中的控制器是 type activated
...也就是说,构建控制器的责任是通过 DI 管理的,它将查看构造函数的参数并从 DI 中的注册服务填充它们...这也称为 constructor injection
...
要了解 DI 类型激活的一般工作原理(DI 独立于 MVC,甚至可以在控制台应用程序中使用),请查看以下代码片段(摘自 http://docs.asp.net/en/latest/security/data-protection/using-data-protection.html)
using System;
using Microsoft.AspNet.DataProtection;
using Microsoft.Framework.DependencyInjection;
public class Program
{
public static void Main(string[] args)
{
// add data protection services
var serviceCollection = new ServiceCollection();
serviceCollection.AddDataProtection();
var services = serviceCollection.BuildServiceProvider();
// create an instance of MyClass using the service provider
var instance = ActivatorUtilities.CreateInstance<MyClass>(services);
instance.RunSample();
}
public class MyClass
{
IDataProtector _protector;
// the 'provider' parameter is provided by DI
public MyClass(IDataProtectionProvider provider)
{
_protector = provider.CreateProtector("Contoso.MyClass.v1");
}
public void RunSample()
{
Console.Write("Enter input: ");
string input = Console.ReadLine();
// protect the payload
string protectedPayload = _protector.Protect(input);
Console.WriteLine($"Protect returned: {protectedPayload}");
// unprotect the payload
string unprotectedPayload = _protector.Unprotect(protectedPayload);
Console.WriteLine($"Unprotect returned: {unprotectedPayload}");
}
}
}
在上面的示例中,类型 MyClass
被激活。
关于您关于 InputFormatter
的具体示例,它们是 未激活的 类型,因此您不能对其使用构造函数注入,但您可以访问 HttpContext
所以你可以按照前面提到的 post.
也看看这篇文章:http://blogs.msdn.com/b/webdev/archive/2014/06/17/dependency-injection-in-asp-net-vnext.aspx