Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to understand the problem of PerCallContextLifeTimeManager based on Custom Unity Lifetime Model

2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces "how to understand the problem of PerCallContextLifeTimeManager based on custom Unity lifetime model". In daily operation, I believe many people have doubts about how to understand the problem of PerCallContextLifeTimeManager based on custom Unity lifetime model. Xiaobian consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the question of "how to understand the problem based on custom Unity lifetime model PerCallContextLifeTimeManager"! Next, please follow the editor to study!

The problem with PerThreadLifetimeManager

When using Unity's built-in PerThreadLifetimeManager lifetime model, it is based on ThreadStatic's TLS (Thread Local Storage) design, that is, for each managed ManagedThreadId, it caches the generated object instance.

Because CLR maintains the thread pool, used threads are not immediately destroyed and will continue to be reused when needed. Under conditions like ASP.NET PerCall or WCF PerCall, Call2 occurs when Call1 is processed in thread ManagedThreadId1, and Call2 is most likely processed in thread ManagedThreadId1 as well. In this case, Call2 automatically reuses the object instances generated and cached when processing Call1.

If we want to generate a dedicated object instance for each call (PerCall), then PerThreadLifetimeManager is not appropriate in such a scenario.

There are two solutions:

1. Continue to use the PerThreadLifetimeManager model, not for ThreadPool, and manually create and destroy threads.

two。 Custom object lifetime model

PerCallContextLifeTimeManager

The copy code is as follows:

Public class PerCallContextLifeTimeManager: LifetimeManager

{

Private string _ key =

String.Format (CultureInfo.InvariantCulture

"PerCallContextLifeTimeManager_ {0}", Guid.NewGuid ()

Public override object GetValue ()

{

Return CallContext.GetData (_ key)

}

Public override void SetValue (object newValue)

{

CallContext.SetData (_ key, newValue)

}

Public override void RemoveValue ()

{

CallContext.FreeNamedDataSlot (_ key)

}

}

Use examples

The copy code is as follows:

Private static void TestPerCallContextLifeTimeManager ()

{

IExample example

Using (IUnityContainer container = new UnityContainer ())

{

Container.RegisterType (typeof (IExample), typeof (Example))

New PerCallContextLifeTimeManager ()

Container.Resolve () .SayHello ()

Container.Resolve () .SayHello ()

Action action = delegate (int sleep)

{

Container.Resolve () .SayHello ()

Thread.Sleep (sleep)

Container.Resolve () .SayHello ()

}

Thread thread1 = new Thread ((a) = > action.Invoke ((int) a))

Thread thread2 = new Thread ((a) = > action.Invoke ((int) a))

Thread1.Start (50)

Thread2.Start (55)

Thread1.Join ()

Thread2.Join ()

ThreadPool.QueueUserWorkItem ((a) = > action.Invoke ((int) a), 50)

ThreadPool.QueueUserWorkItem ((a) = > action.Invoke ((int) a), 55)

Thread.Sleep (100)

ThreadPool.QueueUserWorkItem ((a) = > action.Invoke ((int) a), 50)

ThreadPool.QueueUserWorkItem ((a) = > action.Invoke ((int) a), 55)

Thread.Sleep (100)

ThreadPool.QueueUserWorkItem ((a) = > action.Invoke ((int) a), 50)

ThreadPool.QueueUserWorkItem ((a) = > action.Invoke ((int) a), 55)

Thread.Sleep (100)

Example = container.Resolve ()

}

Example.SayHello ()

Console.ReadKey ()

}

At this point, the study on "how to understand the problem of PerCallContextLifeTimeManager based on custom Unity lifetime model" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report