简体   繁体   English

在ASP.net解决方案中使用Unity IOC容器

[英]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? 所以问题是如果我们在一个线程上注册一个实例,并且在我们解析实例之前请求跳转线程,我们会回到正确的实例吗?

  1. If you resolve your type on same container you've registered it in (or its child), you'll get correct instance. 如果你在同一个容器上解析你的类型,你已经在(或它的孩子)中注册了它,你就会获得正确的实例。

  2. 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. 据我所知,没有必要在每个请求上注册容器中的类型,这通常是在应用程序启动时的某个地方完成的。

Based on this 基于

[...] 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.

相关问题 ASP.Net MVC 3 Unity IoC处置 - ASP.Net MVC 3 Unity IoC Disposal 为什么我应该在ASP.Net应用程序中使用IoC Container(Autofac,Ninject,Unity等)进行依赖注入? - Why should I use IoC Container (Autofac, Ninject, Unity etc) for Dependency Injection in ASP.Net Applications? 使用Asp.Net MVC 5框架和IoC容器时,如何构造视图模型的实例? - How to construct an instance of a view-model when using Asp.Net MVC 5 framework and IoC container? 使用 ASP.NET Core 内置 IoC 容器注入多个客户端而不是一个 - Injecting multiple clients instead of just one using ASP.NET Core built-in IoC container 如何在ASP.NET中使用IoC容器配置单元测试? - How to configure unit tests with an IoC container in ASP.NET? ASP.NET Core 3.x - 访问 IoC 容器 - ASP.NET Core 3.x - Access IoC Container 标准化存储库,UnitOfWork,IOC容器Asp.Net MVC - Standardize Repository, UnitOfWork, IOC Container Asp.Net MVC ASP.NET Core与现有的IoC容器和环境? - ASP.NET Core with existing IoC container & environment? 具有IoC容器并发症的C#ASP.NET依赖注入 - C# ASP.NET Dependency Injection with IoC Container Complications 如何使用asp.net MVC和Unity在application_beginrequest中访问统一容器? - how to access unity container at application_beginrequest using asp.net mvc and unity?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM