Working with dependency injection in mvc

XamDev

In one of our app I am already using the dependency injection of AppTenant class like follows

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>();
}

and in controller i am able to access it easily as follows

public AccountController(AppTenant tenant)
    {
        this.tenant = tenant;
    }

Now, I want to access the same AppTenant OR HttpContext in other project class in the same solution. So, I have tried like this

public SqlStringLocalizerFactory(
       AppTenant tenant)
    {
     _tenant = tenant;

    }

But it is coming null, so what I need to do, to get the AppTenant OR HttpContext in the other project class ?

For SqlStringLocalizerFactory class the services are written in ConfigureServices method like follows

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;
    }
}

I have even tried with IHttpContextAccessor, but still not getting any success.

Any help on this appreciated !

adem caglin

Edit-2

New Solution:

public SqlStringLocalizerFactory(IHttpContextAccessor _accessor)
{
   _accessor= accessor;
}
public void SomeMethod()
{
    var tenant = _accessor.HttpContext.RequestServices
            .GetRequiredService<AppTenant>();
}

Edit : IServiceProvider way doesn't work as i expect. See @Sock's solution

First, i assumes the problem occurs because of captive dependency as pointed by @qujck. To avoid captive dependency:

If lifetime of SqlStringLocalizerFactory must be singleton(some cases must be), in this case use IServiceProvider:

public SqlStringLocalizerFactory(IServiceProvider serviceProvider)
{
   _serviceProvider = serviceProvider;
}
public void SomeMethod()
{
    var tenant =  _serviceProvider.GetService<AppTenant>();
}

Otherwise using AddScoped seems reasonable for your case.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related