Get the App
SLTechnology News&Howtos  ›  Development  › 

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

Shulou Source: shulou.com Published: 2022-06-03 01:06:46 10月05日 Update

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!

Tags: Model lifetime problem thread code object learning instance processing generation more condition cache reuse help hosting utility and then that is Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno NVidia vpn Shulou Information MySQL Apple