[英]Using Unity IOC Container in ASP.net solution
We are using Microsofts Unity IOC container in an ASP.Net solution. 我们在ASP.Net解决方案中使用Microsofts Unity IOC容器。
In one of the top layers we use the RegisterInstance method. 在其中一个顶层中,我们使用RegisterInstance方法。 We then get it back lower down with the Resolve method.
然后我们使用Resolve方法将其降低。
While looking at another problem we have seen that ASP.Net has thread agility, that is that a request can jump threads. 在查看另一个问题时,我们已经看到ASP.Net具有线程敏捷性,即请求可以跳转线程。
So the question is if we register an instance on one thread, and the request jumps thread before we resolve the instance, will we get back the correct instance? 所以问题是如果我们在一个线程上注册一个实例,并且在我们解析实例之前请求跳转线程,我们会回到正确的实例吗?
If you resolve your type on same container you've registered it in (or its child), you'll get correct instance. 如果你在同一个容器上解析你的类型,你已经在(或它的孩子)中注册了它,你就会获得正确的实例。
As far as I know there's no need to register types in container on every request, that's usually done somewhere on application start. 据我所知,没有必要在每个请求上注册容器中的类型,这通常是在应用程序启动时的某个地方完成的。
[...]
Resolve, ResolveAll, and BuildUp are all thread safe .
[...] Resolve,ResolveAll和BuildUp都是线程安全的 。 Looks like we missed it in the docs.
看起来我们在文档中错过了它。
None of the other methods on the container are thread safe, so if you're going to be configuring containers, you'll need to do locking.
容器上的其他任何方法都不是线程安全的,因此如果您要配置容器,则需要进行锁定。 Also, if you've got one thread configuring and another thread resolving, there will be thread issues.
此外,如果您有一个线程配置和另一个线程解析,则会出现线程问题。
AFAIK, your approach is correct and you shouldn't worry about getting incorrect instance in ASP.NET application. AFAIK,您的方法是正确的,您不必担心在ASP.NET应用程序中获取不正确的实例。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.