加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

ASP.NET Web API应用程序中的Autofac多租户IoC容器

发布时间:2020-12-16 04:05:16 所属栏目:asp.Net 来源:网络整理
导读:Autofac 3.0现在将获得 MultitenantIntegration支持和 its preview release is out.为了试用它,我使用以下配置创建了一个ASP.NET Web API应用程序: public class Global : System.Web.HttpApplication { protected void Application_Start(object sender,Ev
Autofac 3.0现在将获得 MultitenantIntegration支持和 its preview release is out.为了试用它,我使用以下配置创建了一个ASP.NET Web API应用程序:
public class Global : System.Web.HttpApplication {

    protected void Application_Start(object sender,EventArgs e) {

        var config = GlobalConfiguration.Configuration;
        config.Routes.MapHttpRoute("Default","api/{controller}");
        RegisterDependencies(config);
    }

    public void RegisterDependencies(HttpConfiguration config) {

        var builder = new ContainerBuilder();
        builder.RegisterApiControllers(Assembly.GetExecutingAssembly());

        // creates a logger instance per tenant
        builder.RegisterType<LoggerService>().As<ILoggerService>().InstancePerTenant();

        var mtc = new MultitenantContainer(
            new RequestParameterTenantIdentificationStrategy("tenant"),builder.Build());

        config.DependencyResolver = new AutofacWebApiDependencyResolver(mtc);
    }
}

它完成了工作,并为每个租户创建一个LoggerService实例作为ILoggerService.在这个阶段我有两个问题,我无法解决:

>我在这里提供了开箱即用的RequestParameterTenantIdentificationStrategy作为TenantIdentificationStrategy,仅用于此演示应用程序.我可以通过实现ITenantIdentificationStrategy接口来创建我的自定义TenantIdentificationStrategy.但是,ITenantIdentificationStrategy的TryIdentifyTenant方法使您依赖于静态实例,例如HttpContext.Current,这是我在ASP.NET Web API环境中不需要的东西,因为我希望我的API能够托管不可知(我知道我可以将此工作委托给托管层,但我宁愿不这样做.有没有其他方法可以实现这一点,我不会依赖静态实例?
>我也有机会注册租户特定实例如下:

mtc.ConfigureTenant("tenant1",cb => cb.RegisterType<Foo>()
                                    .As<IFoo>().InstancePerApiRequest());

但是,我的一个情况要求我通过构造函数参数传递租户名称,我希望有类似下面的内容:

mtc.ConfigureTenant((cb,tenantName) => cb.RegisterType<Foo>()
                                    .As<IFoo>()
                                    .WithParameter("tenantName",tenantName)
                                    .InstancePerApiRequest());

目前没有这样的API.有没有其他方法来实现这一点或这种要求没有任何意义?

解决方法

多租户支持已经有很长一段时间了,只是3.0是我们第一次使用NuGet包.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读