我正在将我的多租户应用程序从Webapi迁移到aspnet核心.在webapi版本中,我TenantIdentificationStrategy根据请求路径使用了已识别的租户HttpContext.
转到aspnet核心,我能够成功连接autofac.我无法弄清楚如何连接租户策略.我试着注入IHttpContextAccessor在ConfigureServices如
services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
Run Code Online (Sandbox Code Playgroud)
我的策略看起来像这样
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;
}
}
Run Code Online (Sandbox Code Playgroud)
我所看到的是HttpContextAccessor正确注入,其中HttpContext始终为null.因此,没有任何多租户服务得到解决. …
autofac multi-tenant asp.net-core-mvc asp.net-core asp.net-core-1.0