[英]Working with dependency injection in mvc
在我們的一個應用程序中,我已經在使用AppTenant
類的依賴注入,如下所示
public void ConfigureServices(IServiceCollection services)
{
services.AddMultitenancy<AppTenant, CachingAppTenantResolver>();
services.Configure<MultitenancyOptions>(Configuration.GetSection("Multitenancy"));
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
app.UseMultitenancy<AppTenant>();
}
在控制器中,我可以按以下方式輕松訪問它
public AccountController(AppTenant tenant)
{
this.tenant = tenant;
}
現在,我想在相同解決方案的其他項目類中訪問相同的AppTenant
或HttpContext
。 所以,我嘗試過這樣
public SqlStringLocalizerFactory(
AppTenant tenant)
{
_tenant = tenant;
}
但是它即將為null,所以我需要做些什么才能在另一個項目類中獲取AppTenant
或HttpContext
?
對於SqlStringLocalizerFactory
類,服務使用ConfigureServices
方法編寫,如下所示
public static class SqlLocalizationServiceCollectionExtensions
{
public static IServiceCollection AddSqlLocalization(this IServiceCollection services)
{
if (services == null)
{
throw new ArgumentNullException(nameof(services));
}
return AddSqlLocalization(services, setupAction: null);
}
public static IServiceCollection AddSqlLocalization(
this IServiceCollection services,
Action<SqlLocalizationOptions> setupAction)
{
if (services == null)
{
throw new ArgumentNullException(nameof(services));
}
services.TryAdd(new ServiceDescriptor(
typeof(IStringExtendedLocalizerFactory),
typeof(SqlStringLocalizerFactory),
ServiceLifetime.Singleton));
services.TryAdd(new ServiceDescriptor(
typeof(IStringLocalizerFactory),
typeof(SqlStringLocalizerFactory),
ServiceLifetime.Singleton));
services.TryAdd(new ServiceDescriptor(
typeof(IStringLocalizer),
typeof(SqlStringLocalizer),
ServiceLifetime.Singleton));
if (setupAction != null)
{
services.Configure(setupAction);
}
return services;
}
}
我什至嘗試使用IHttpContextAccessor
,但仍然沒有成功。
任何幫助對此表示贊賞!
編輯2
新解決方案:
public SqlStringLocalizerFactory(IHttpContextAccessor _accessor)
{
_accessor= accessor;
}
public void SomeMethod()
{
var tenant = _accessor.HttpContext.RequestServices
.GetRequiredService<AppTenant>();
}
編輯 : IServiceProvider
方式無法正常工作。 請參閱@Sock的解決方案
首先,我假設問題是由於@qujck所指出的依賴關系而發生的。 為了避免圈養依賴性:
如果SqlStringLocalizerFactory
生存期必須為單身(某些情況下必須為單身),則在這種情況下,請使用IServiceProvider
:
public SqlStringLocalizerFactory(IServiceProvider serviceProvider)
{
_serviceProvider = serviceProvider;
}
public void SomeMethod()
{
var tenant = _serviceProvider.GetService<AppTenant>();
}
否則,對於您的情況,使用AddScoped
似乎是合理的。
您有兩個選擇,最好的選擇,如果SqlStringLocalizerFactory
可以是作用域的依賴項(每個請求都有一個新實例),則可以將其注冊為作用域的依賴項:
services.TryAdd(new ServiceDescriptor(
typeof(IStringLocalizerFactory),
typeof(SqlStringLocalizerFactory),
ServiceLifetime.Scoped));
如果SqlStringLocalizerFactory
必須是一個Singleton依賴項,則需要確保通過使用ServiceSope
為租戶解決有范圍的依賴項:
public class SqlStringLocalizerFactory
{
private readonly IServiceProvider _serviceProvider;
public SqlStringLocalizerFactory(IServiceProvider serviceProvider)
{
_serviceProvider = serviceProvider;
}
public void SomeMethod()
{
using (var serviceScope = _serviceProvider
.GetRequiredService<IServiceScopeFactory>().CreateScope())
{
var tenant = serviceScope.ServiceProvider.GetService<AppTenant>();
// do something with tenant...
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.