如何将IHttpContextAccessor注入Autofac的TenantIdentificationStrategy中

8

我正在将我的多租户应用程序从Webapi迁移到aspnet core。在Webapi版本中,我使用了TenantIdentificationStrategy,该策略基于HttpContext上的请求路径来识别租户。

转移到aspnet core后,我成功地连接了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 始终为 null。因此,无法解析任何多租户服务。
寻找示例,但找不到适用于该问题的内容。在 Autofacv3 中曾经有一个 RequestParameterTenantIdentificationStrategy,但现在不再支持。感激任何关于此的帮助。
编辑:
已修复代码问题,并添加了 Startup.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; }
}

Edit 3 project.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%" ]
  }
}

1
你能发一下你的启动文件吗? - Nick Acosta
我已经附上了启动文件。 - Kamalakar Nellipudi
2个回答

6

目前没有办法将内容注入到租户识别策略中,因为该策略本身不会经过DI管道。

IHttpContextAccessor通常只是由HttpContextAccessor支持的单例,并通过获取异步/线程本地上下文信息来执行。当您在启动时,可以直接使用其中之一来新建策略:

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

请注意,在最初提出问题时,多租户与ASP.NET Core IServiceProvider系统的交互存在问题,也就是说,两者并不兼容。

自那以后,我们发布了Autofac.Extensions.DependencyInjection包的4.0.0-rc3-309版本来解决这个问题。

更改是您需要更新ConfigureServicesreturn new AutofacServiceProvider(mtc); ,不再使用return mtc.Resolve<IServiceProvider>();


我现在尝试了这种方法,发现HttpContextAccessor中的HttoContext为空。另外一个问题是,如果我在策略的TryIdentifyTenant中放置断点,对于每个请求的页面,断点都不会触发。有什么想法我做错了什么吗? - Kamalakar Nellipudi
1
我个人还没有尝试过新的带有多租户支持的ASP.NET Core功能。我不确定那边怎么样了,但很有可能当前的Core集成正在绕过多租户功能。我得去查一下。不过上下文访问器应该是可以工作的。 - Travis Illig
1
我已经在Autofac的总体待办事项列表中添加了一条评论,关于Core的问题(https://github.com/autofac/Autofac/issues/594#issuecomment-229820297)。我们会看一下的。 - Travis Illig
已在最新版本中修复。我已相应地更新了我的答案。 - Travis Illig

1
这段内容翻译如下:

这篇评论已经太长了。

首先,你的类名为SampleIdentificationStrategy,但构造函数引用的是AssetClassIdentificationStrategy。由此问题,该项目甚至不能编译。

接下来(因为你没有提供启动文件),请确保在ConfigureServices方法中调用以下代码以填充AutoFac中注册的服务。

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

请注意,此方法必须在您将所有服务注册到IServiceCollection之后运行。
接下来,请确保不要混淆框架版本。 RC2,RC1,beta-x等之间有很大的区别。这在GitHub问题日志herehere中已经指出。
除此之外,我们需要查看您的startup.cs文件(特别是从ConfigureServices方法中提取的内容),以及project.json文件(特别是frameworks和dependency节点)。

我已经添加了启动文件和project.json,请看一下。 - Kamalakar Nellipudi

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接