简体   繁体   中英

Using Unity IOC Container in ASP.net solution

We are using Microsofts Unity IOC container in an ASP.Net solution.

In one of the top layers we use the RegisterInstance method. We then get it back lower down with the Resolve method.

While looking at another problem we have seen that ASP.Net has thread agility, that is that a request can jump threads.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM