Note I'm tagging each level of sub-web scope as "SubWeb" - that will allow you to have "instance per sub web" sort of registrations in both container-level and sub-web-level registrations.// Register a "singleton" per sub-web:builder.RegisterType<Foo>() .As<IFoo>() .InstancePerMatchingLifetimeScope("SubWeb");现在,很明显,这是一个概念性的东西-您实际上无法使用这样的语句来包装所有内容.您需要对创建和处置进行不同的管理,因为创建将在与处置不同的地方进行.Now, obviously, that's a conceptual thing there - you won't actually be able to wrap everything in using statements like that. You'll need to manage your creation and disposal differently because creation will happen in a different place than disposal.您可以同时查看ASP.NET和多租户源,以获取有关如何执行此操作的想法.通用算法为:You can look at both the ASP.NET and multitenant source to get ideas on how to do that. The general algorithm will be:在应用程序启动时,构建根容器.在子网站启动时,产生一个为子网站命名的嵌套生命周期范围.如果子站点需要注册特定组件,请在调用BeginLifetimeScope 时进行操作如果在每个子网站级别都需要上下文",则可以将为该子网站创建的范围传递给它,而不是创建一个单独的容器.At application startup, build the root container.As sub webs start up, spawn a nested lifetime scope named for the sub web.If a sub web needs a specific component registered, do that during the call to BeginLifetimeScopeIf you need the "context" at each sub web level, you'd pass it the scope created for that sub web rather than creating a whole separate container.现在,您可以采取另一步骤,将子Web ID的根级字典保留在范围内,这样就根本不需要每个级别的上下文"对象.它更像是一种DependencyResolver.Current.GetService<T>模式.如果您查看Autofac多租户支持中的MultitenantContainer的工作方式,您会看到类似的租户ID范围字典.Now, you could take it another step by keeping a root-level dictionary of sub web ID to scope so that you'd not need per-level "context" objects at all. It'd be more like a DependencyResolver.Current.GetService<T> kind of pattern. If you look at how the MultitenantContainer in the Autofac multitenant support works, you'll see a similar sort of tenant-ID-to-scope dictionary.事实上,多租户支持将是一个很好的模式,特别是如果您还希望具有按Web请求的作用域时. Autofac ASP.NET支持要求您传入父ILifetimeScope,将从其生成子Web请求生存期范围.多租户支持在其中添加了一些动态方面,因此当ASP.NET支持调用BeginLifetimeScope时,事物的多租户部分会自动确定(通过租户标识)哪个租户应该是当前请求的父级.您可以对子站点的层次结构执行相同的操作.但是,再次,多租户支持是一个平面结构,而您的子网站是一个层次结构,因此多租户支持将不起作用 .In fact, that multitenant support will be a good pattern to look at, especially if you also want to have per-web-request scopes. The Autofac ASP.NET support requires you pass in a parent ILifetimeScope from which child web request lifetime scopes will be spawned. The multitenant support adds some dynamic aspect in there so when the ASP.NET support calls BeginLifetimeScope the multitenant portion of things automatically figures out (through tenant identification) which tenant should be the parent of the current request. You could do the same thing with your hierarchy of sub-webs. However, again, the multitenant support is a flat structure while your sub webs are a hierarchy, so the multitenant support won't just work.这很长的话可以说您在这里有一个有趣的用例,但是您会变得很脏.This is all a long way of saying you have an interesting use case here, but you're going to be getting your hands pretty dirty. 这篇关于需要有关Autofac自定义生命周期范围与多租户的指南的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 1403页,肝出来的..
09-06 15:46