Kam*_*udi 7 autofac multi-tenant asp.net-core-mvc asp.net-core asp.net-core-1.0
我正在将我的多租户应用程序从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.因此,没有任何多租户服务得到解决.
查看样本,但找不到符合问题的任何内容.曾经有一个RequestParameterTenantIdentificationStrategy不再支持的Autofacv3.感谢任何帮助.
编辑已 修复代码问题并按要求添加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; }
}
Run Code Online (Sandbox Code Playgroud)
编辑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%" ]
}
}
Run Code Online (Sandbox Code Playgroud)
当前没有将事物注入到租户识别策略中的方法,因为该策略本身不会通过DI管道。
IHttpContextAccessor通常只支持HttpContextAccessor单例,并通过从异步/线程本地上下文中获取信息来执行操作。在启动时,您可以直接使用以下策略之一重新制定策略:
var strat = new MyStrategy(new HttpContextAccessor());
请注意,最初提出该问题时,多租户与ASP.NET Core IServiceProvider系统交互的方式存在问题,也就是说,事实并非如此。
从那时起,我们发布4.0.0-rc3-309了Autofac.Extensions.DependencyInjection可解决该问题的软件包。
更改是您需要更新ConfigureServices到return new AutofacServiceProvider(mtc);不再执行的操作return mtc.Resolve<IServiceProvider>();。
| 归档时间: |
|
| 查看次数: |
3463 次 |
| 最近记录: |