我正在将多租户应用程序从Webapi迁移到aspnet核心。在webapi版本中,我使用的是TenantIdentificationStrategy
,它根据HttpContext
上的请求路径来标识租户。
转到aspnet core,我能够成功连接autofac。我无法弄清楚如何捆绑租户策略。我尝试在IHttpContextAccessor
中将ConfigureServices
注入为
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; }
}
编辑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%" ]
}
}
参考方案
当前还没有一种将内容注入租户识别策略的方法,因为该策略本身并不通过DI管道。
IHttpContextAccessor
通常仅以HttpContextAccessor
作为后盾,并且总是从异步/线程本地上下文中获取信息。在启动时,您可以直接使用以下策略之一来重新制定策略:
var strat = new MyStrategy(new HttpContextAccessor());
请注意,最初提出该问题时,多租户与ASP.NET Core IServiceProvider
系统进行交互的方式存在问题,也就是说没有。
从那时起,我们发布了4.0.0-rc3-309
for the Autofac.Extensions.DependencyInjection
package来解决此问题。
所做的更改是您需要将ConfigureServices
更新为return new AutofacServiceProvider(mtc);
,而不再需要执行return mtc.Resolve<IServiceProvider>();
。
我正在使用Retrofit来获取JSON答复。这是我实施的一部分-@GET("/api/report/list") Observable<Bills> listBill(@Query("employee_id") String employeeID); 而条例草案类是-public static class…
改造正在返回一个空的响应主体 - java我正在尝试使用Retrofit和Gson解析一些JSON。但是,我得到的响应机构是空的。当我尝试从对象中打印信息时,出现NullPointerException。我确保URL正确,并且我也确保POJO也正确。我正在使用jsonschema2pojo来帮助创建POJO类。这是我要解析的JSON{ "?xml": { "@versi…
.NET C#Webbrowser填充输入,不带ID或类名 - javascript我需要在网络浏览器中填写一个输入,但这不起作用。我认为必须使用name属性来完成,但是怎么做呢?foreach (HtmlElement login in webBrowser1.Document.GetElementsByTagName("input")) { if (login.GetAttribute("name"…
如何将POCO类传递给.NET Core配置 - c#我正在进行.NET Core应用程序的集成测试,并且想要使用一些测试配置。我的配置是一个POCO类,该类通过appsettings.json配置,然后通过IOptions<T>使用。在我的测试中,我想使用该类的实例。这是代码: var mySettings = GetTestSettings(); // factory method return…
Div单击与单选按钮相同吗? - php有没有一种方法可以使div上的click事件与表单环境中的单选按钮相同?我只希望下面的div提交值,单选按钮很丑代码输出如下:<input id="radio-2011-06-08" value="2011-06-08" type="radio" name="radio_date…