asp.net-core – 如何将IHttpContextAccessor注入到Autofac Tena
我正在将我的多租户应用程序从Webapi迁移到aspnet核心.在webapi版本中,我使用TenantIdentificationStrategy根据HttpContext上的请求路径识别租户.
转到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总是为null.因此,没有任何多租户服务得到解决. 查看样本,但找不到符合问题的任何内容.过去在Autofacv3中有一个RequestParameterTenantIdentificationStrategy,不再受支持.感谢任何帮助. 编辑 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","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通常只支持 var strat = new MyStrategy(new HttpContextAccessor()); 请注意,在最初询问问题时,多租户与ASP.NET Core IServiceProvider系统交互的方式存在问题,也就是说,它没有. 从那以后,我们发布了 更改是您需要更新ConfigureServices以返回新的AutofacServiceProvider(mtc);并且不再返回mtc.Resolve< IServiceProvider>();. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- ASP.NET(AJAX+JSON)实现对象调用
- asp.net-mvc-3 – AZURE ACS – Windows Live ID – 如何获
- asp.net – TinyMCE在我第一次保存时不会返回任何值
- asp.net-mvc – 需要帮助来应用SOLID原则
- 经典ASP中的密码加密方法
- asp.net – 触发OWIN cookie中间件设置为被动身份验证模式的
- ASP.NET AJAX日历扩展程序日期格式
- MVC学习二:基础语法
- asp.net-mvc – 在使用Asp.Net MVC ActionLink,RedirectToA
- dependency-injection – 如何使用unity注入ApplicationUse
- asp.net – 何时使用HttpApplicationState而不是
- ASP.NET vs. Silverlight
- asp.net – 限制可以输入’textarea’的文本数量
- asp.net-mvc – Visual Studio 2017增加了缺失的
- 使用CodeMirror在浏览器中实现编辑器的代码高亮效
- 循环 – 如何检查我是否在ADO Recordset的最后一
- asp.net – 站点之间共享的IIS 7.0应用程序池 –
- asp.net-identity – 如何将承载令牌与MVC 6 API
- asp.net – this.Page和(页面)HttpContext.Curre
- 是否可以从ASP.NET Web服务方法手动将HTTP代码40