如何将 IHttpContextAccessor 注入 Autofac TenantIdentificationStra

本文关键字:Autofac TenantIdentificationStra 注入 IHttpContextAccessor | 更新日期: 2023-09-27 17:53:39

我正在将我的多租户应用程序从Webapi迁移到aspnet核心。在webapi版本中,我使用的是根据HttpContext上的请求路径识别租户的TenantIdentificationStrategy

迁移到aspnet核心,我能够成功地连接autofac。我无法弄清楚如何连接租户策略。我尝试在ConfigureServices中注射IHttpContextAccessor

services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>(); 

我的策略看起来像这样

public class AssetClassIdentificationStrategy: ITenantIdentificationStrategy {
    private readonly IHttpContextAccessor _accessor;
    public AssetClassIdentificationStrategy(IHttpContextAccessor httpContextAccessor)
    {
        _accessor = httpContextAccessor;
    }
    public bool TryIdentifyTenant(out object tenantId) {
        tenantId = null;
        var context = _accessor.HttpContext;
        if (context != null && context.Request != null )){
            var matchRegex = new Regex(@"'/['d,'.,'w]*'/('w*)'/.*");
            var match = matchRegex.Match(context.Request.Path.ToString());
            if (match.Success) {
                tenantId = match.Groups[1].Value.ToLower();
            }
        }
        return tenantId != null;
    }
}

我所看到的是HttpContextAccessor被正确注入,其中HttpContext始终为空。因此,不会解析任何多租户服务。

四处寻找样品,但找不到任何适合问题的东西。Autofacv3 中曾经有一个不再受支持的RequestParameterTenantIdentificationStrategy。感谢对此的任何帮助。

编辑修复了代码和根据请求添加启动.cs的问题。

public class Startup
{
    public Startup(IHostingEnvironment env) {
        var builder = new ConfigurationBuilder()
            .SetBasePath(env.ContentRootPath)
            .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true);
        Configuration = builder.Build();
    }
    public IConfigurationRoot Configuration { get; }
    public IServiceProvider ConfigureServices(IServiceCollection services)
    {
        services.Configure<CacheConfig>(Configuration.GetSection("Caching"),false);
        services.AddMvc();
        services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
        services.AddTransient<ITenantIdentificationStrategy,AssetClassIdentificationStrategy>();
        var builder = new ContainerBuilder();
        builder.Populate(services);
        builder.RegisterType<TenantInfo>().WithProperty("TenantName", "unknown").As<ITenantInfo>();
        var container = builder.Build();
        ITenantIdentificationStrategy tenantIdentificationStrategy;
        bool isMultiTenant = container.TryResolve(out tenantIdentificationStrategy);
        var mtc = new MultitenantContainer(tenantIdentificationStrategy, container);
        mtc.ConfigureTenant("pesonalLoans", b => {
            b.RegisterType<TenantInfo>().WithProperty("TenantName","pesonalLoans") .As<ITenantInfo>();
        });
        mtc.ConfigureTenant("retirement", b => {
            b.RegisterType<TenantInfo>().WithProperty("TenantName", "retirement").As<ITenantInfo>();
        });
        return mtc.Resolve<IServiceProvider>();
    }
    public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
    {
        LoggingConfig.Register(Configuration, loggerFactory);
        app.UseMvc();
    }
}

public class ValuesController : Controller {
    private ITenantInfo _tenant;
    public ValuesController(ITenantInfo tenant) {
        _tenant = tenant;
    }
    [HttpGet]
    public string Get()
    {
        return  _tenant.TenantName;
    }
}

public interface ITenantInfo {
    string TenantName { get; set; }
}
public class TenantInfo: ITenantInfo
{
    public string TenantName { get; set; }
}

编辑 3 项目.json

{
  "dependencies": {
    "Microsoft.NETCore.App": {
      "version": "1.0.0-rc2-3002702",
      "type": "platform"
    },
    "Microsoft.AspNetCore.Mvc": "1.0.0-rc2-final",
    "Microsoft.AspNetCore.Server.IISIntegration": "1.0.0-rc2-final",
    "Microsoft.AspNetCore.Server.Kestrel": "1.0.0-rc2-final",
    "Microsoft.Extensions.Configuration.EnvironmentVariables": "1.0.0-rc2-final",
    "Microsoft.Extensions.Configuration.FileExtensions": "1.0.0-rc2-final",
    "Microsoft.Extensions.Configuration.Json": "1.0.0-rc2-final",
    "Microsoft.Extensions.Logging": "1.0.0-rc2-final",
    "Microsoft.Extensions.Logging.Console": "1.0.0-rc2-final",
    "Microsoft.Extensions.Logging.Debug": "1.0.0-rc2-final",
    "Autofac": "4.0.0-rc2-240",
    "Autofac.Multitenant": "4.0.0-beta8-219",
    "System.IdentityModel.Tokens.Jwt": "5.0.0-rc2-305061149",
    "Autofac.Extensions.DependencyInjection": "4.0.0-rc2-240",
    "System.Reflection": "4.1.0-rc2-24027",
    "System.Reflection.Primitives": "4.0.1-rc2-24027",
    "System.Reflection.Extensions": "4.0.1-rc2-24027",
    "System.Reflection.TypeExtensions": "4.1.0-rc2-24027",
    "System.Reflection.Emit": "4.0.1-rc2-24027",
    "System.Reflection.Context": "4.0.1-rc2-24027",
    "System.Reflection.DispatchProxy": "4.0.1-rc2-24027",
    "System.Reflection.Emit.ILGeneration": "4.0.1-rc2-24027",
    "Microsoft.AspNetCore.Diagnostics": "1.0.0-rc2-final",
    "Microsoft.AspNet.Mvc.Formatters.Xml": "6.0.0-rc1-final",
    "Microsoft.AspNet.Mvc.Formatters.Json": "6.0.0-rc1-final",
  },
  "tools": {
    "Microsoft.AspNetCore.Server.IISIntegration.Tools": {
      "version": "1.0.0-preview1-final",
      "imports": "portable-net45+win8+dnxcore50"
    }
  },
  "frameworks": {
    "netcoreapp1.0": {
      "imports": [
        "dotnet5.6",
        "dnxcore50",
        "portable-net45+win8"
      ]
    }
  },
  "buildOptions": {
    "emitEntryPoint": true,
    "preserveCompilationContext": true
  },
  "runtimeOptions": {
    "gcServer": true
  },
  "scripts": {
    "postpublish": [ "dotnet publish-iis --publish-folder %publish:OutputPath% --framework %publish:FullTargetFramework%" ]
  }
}

如何将 IHttpContextAccessor 注入 Autofac TenantIdentificationStra

目前没有办法将内容注入租户标识策略,因为策略本身不通过 DI 管道。

IHttpContextAccessor通常只支持HttpContextAccessor无论如何都是单例,并通过从异步/线程本地上下文获取信息来起作用。当您在启动时,您可以直接使用以下策略之一来更新您的策略:

var strat = new MyStrategy(new HttpContextAccessor());

请注意,在最初提出问题时,多租户与 ASP.NET 核心IServiceProvider系统的交互方式存在问题,也就是说,它没有。

从那以后,我们发布了Autofac.Extensions.DependencyInjection包的4.0.0-rc3-309,以解决此问题。

更改是您需要将ConfigureServices更新为 return new AutofacServiceProvider(mtc);,并且不再执行return mtc.Resolve<IServiceProvider>();

这成了渴望评论的渴望。

首先,你的类被命名为SampleIdentificationStrategy但你的构造函数引用AssetClassIdentificationStrategy。从这个问题开始,项目甚至不应该编译。

接下来(由于尚未提供启动文件(,请确保通过在 ConfigureServices 方法中调用以下代码来填充 AutoFac 中的已注册服务。

builder.Populate(services);
builder.Update(container);

注意:在将所有服务注册到IServiceCollection必须运行此方法

接下来,请确保不要混合框架版本。RC2 RC1beta-x等之间有很大的区别。这在 GitHub 问题日志中在此处和此处都有说明。

除此之外,我们需要查看您的startup.cs文件(特别是ConfigureServices方法的摘录,您的project.json文件(特别是frameworksdependency节点(。