Many people may have questions about httpcontext. Current.
How did he implement it?
Why can different results be obtained when different requests access a static member current?
Obviously, this static object is related to the context of the current call.
Its internal implementation is similar to callcontext. setdata () and callcontext. getdata ()
The data saved through setdata is related to the call stack of the current method. For example, if you set data in the middle of method
Then method A calls Method B, so that method B can use getdata () to obtain data, which is also OK.
The data can be used to each other on this method stack.
For example:Code
Public Static Wcftransaction transaction
{
Get
{
Return Callcontext. logicalgetdata ( " Wcftransaction " ) As Wcftransaction;
}
Set
{
Callcontext. logicalsetdata ( " Wcftransaction " , Value );
}
}
PS: the main difference between logicalgetdata and getdata is that logicalgetdata can adapt to more situations.
For example, when cross-appdomian is used, when setdata is used in another appdomain, getdata cannot get the value, and logicalgetdata can continue to be used,
In addition, if the preceding wcftrancation inherits a specific interface, it can be automatically propagated to more places. However, it is difficult to control the lifecycle.
Callcontext is related to the call stack
In the next article, we will introduce the thread-related data... although these two have a large overlap in most cases ..