ASP 具有 System.Web.Http API 控制器的 WebForms - 控制器中的构造函数依赖注入不起作用
ASP WebForms with System.Web.Http API Controllers- Constructor dependency Injection in controllers not working
我正在处理旧版 ASP.NET WebForms 系统,必须实施依赖注入。
我已经使用 Microsoft.Extensions.DependencyInjection 和自定义 ServiceProvider 为页面 类 设置了 DI,效果很好。
服务提供商
public class ServiceProvider : IServiceProvider
{
private readonly IServiceProvider _serviceProvider;
public ServiceProvider(IServiceProvider serviceProvider)
{
_serviceProvider = serviceProvider;
}
public object GetService(Type serviceType)
{
try
{
IServiceScope lifetimeScope;
var currentHttpContext = HttpContext.Current;
if (currentHttpContext != null)
{
lifetimeScope = (IServiceScope)currentHttpContext.Items[typeof(IServiceScope)];
if (lifetimeScope == null)
{
void CleanScope(object sender, EventArgs args)
{
if (sender is HttpApplication application)
{
application.RequestCompleted -= CleanScope;
lifetimeScope.Dispose();
}
}
lifetimeScope = _serviceProvider.CreateScope();
currentHttpContext.Items.Add(typeof(IServiceScope), lifetimeScope);
currentHttpContext.ApplicationInstance.RequestCompleted += CleanScope;
}
}
else
{
lifetimeScope = _serviceProvider.CreateScope();
}
return ActivatorUtilities.GetServiceOrCreateInstance(lifetimeScope.ServiceProvider, serviceType);
}
catch (InvalidOperationException)
{
//No public ctor available, revert to a private/internal one
return Activator.CreateInstance(serviceType, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.CreateInstance, null, null, null);
}
}
}
并在 Global.asax
IServiceCollection services = this.ConfigureServices(new ServiceCollection());
var provider = new Infrastructure.DI.ServiceProvider(services.BuildServiceProvider());
HttpRuntime.WebObjectActivator = provider;
在这个应用程序的客户端有一段 JS 必须通过 ajax 调用服务器。由于此遗留应用程序的设计,无法在相应的 .aspx 页面上调用 [WebMethod]
,因此引入了 System.Web.Http API 控制器来处理此调用。
我希望通过在 Global.asax
中像这样设置 DI,我可以在控制器构造函数中注入我需要的服务,但这不起作用并且 ControllerContext returns 是一个空的 HttpConfiguration。
如何使用我当前的 DI 并将其提供给我的控制器?
// This fixes controllers injection.
GlobalConfiguration.Configuration.DependencyResolver = new MyDependencyResolver();
我正在处理旧版 ASP.NET WebForms 系统,必须实施依赖注入。 我已经使用 Microsoft.Extensions.DependencyInjection 和自定义 ServiceProvider 为页面 类 设置了 DI,效果很好。
服务提供商
public class ServiceProvider : IServiceProvider
{
private readonly IServiceProvider _serviceProvider;
public ServiceProvider(IServiceProvider serviceProvider)
{
_serviceProvider = serviceProvider;
}
public object GetService(Type serviceType)
{
try
{
IServiceScope lifetimeScope;
var currentHttpContext = HttpContext.Current;
if (currentHttpContext != null)
{
lifetimeScope = (IServiceScope)currentHttpContext.Items[typeof(IServiceScope)];
if (lifetimeScope == null)
{
void CleanScope(object sender, EventArgs args)
{
if (sender is HttpApplication application)
{
application.RequestCompleted -= CleanScope;
lifetimeScope.Dispose();
}
}
lifetimeScope = _serviceProvider.CreateScope();
currentHttpContext.Items.Add(typeof(IServiceScope), lifetimeScope);
currentHttpContext.ApplicationInstance.RequestCompleted += CleanScope;
}
}
else
{
lifetimeScope = _serviceProvider.CreateScope();
}
return ActivatorUtilities.GetServiceOrCreateInstance(lifetimeScope.ServiceProvider, serviceType);
}
catch (InvalidOperationException)
{
//No public ctor available, revert to a private/internal one
return Activator.CreateInstance(serviceType, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.CreateInstance, null, null, null);
}
}
}
并在 Global.asax
IServiceCollection services = this.ConfigureServices(new ServiceCollection());
var provider = new Infrastructure.DI.ServiceProvider(services.BuildServiceProvider());
HttpRuntime.WebObjectActivator = provider;
在这个应用程序的客户端有一段 JS 必须通过 ajax 调用服务器。由于此遗留应用程序的设计,无法在相应的 .aspx 页面上调用 [WebMethod]
,因此引入了 System.Web.Http API 控制器来处理此调用。
我希望通过在 Global.asax
中像这样设置 DI,我可以在控制器构造函数中注入我需要的服务,但这不起作用并且 ControllerContext returns 是一个空的 HttpConfiguration。
如何使用我当前的 DI 并将其提供给我的控制器?
// This fixes controllers injection.
GlobalConfiguration.Configuration.DependencyResolver = new MyDependencyResolver();