问题
I was thinking of using StructureMap's profiles to facilitate offering up slight differences in behavior in my web app based on the authenticated user's type. My question is, if I do something like
ObjectFactory.Profile = Session["UserType"];
is it going to be thread-safe or will simultaneous requests potentially interfere with each other when resolving things based on the profile?
回答1:
The operations on the static ObjectFactory facade are all "thread safe". This means that you can safely call them on different threads without corrupting the internal state of the ObjectFactory.
However, what you are asking is whether each thread gets its own personal copy of the ObjectFactory, and the answer is no. Any change you make to ObjectFactory on any thread, will be reflected in all other threads within the AppDomain.
The Profiles feature is probably not the solution you are looking for. You probably want to use something like named instances:
ObjectFactory.GetInstance<ISomeService>( Session["UserType"] );
There are other potential solutions, depending on what you are trying to do. Consider posting a question about the problem you are trying to solve, ex: "how do I get different behavior based on the current user's UserType..."
来源:https://stackoverflow.com/questions/1494937/are-structuremap-profiles-thread-safe