Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/301.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在ASP.NET后台线程上使用ThreadStatic数据并设置Thread.CurrentPrincipal.Identity是否安全?_C#_Asp.net - Fatal编程技术网

C# 在ASP.NET后台线程上使用ThreadStatic数据并设置Thread.CurrentPrincipal.Identity是否安全?

C# 在ASP.NET后台线程上使用ThreadStatic数据并设置Thread.CurrentPrincipal.Identity是否安全?,c#,asp.net,C#,Asp.net,如前所述,您应该使用HttpContext.Current.Items而不是ThreadStatic变量来存储ASP.NET请求的数据,因为您的请求可能由多个线程处理 如果你想在后台线程上做点什么呢?使用ThreadStatic数据和设置Thread.CurrentPrincipal.Identity以避免通过所有方法推送“全局”数据是否安全?我想没问题,但我想确定一下。例如,我没有在后台线程上执行的代码中显式使用任何wait,但我调用的库可能是;当此类异步代码处于非活动状态时,ASP.NET是

如前所述,您应该使用
HttpContext.Current.Items
而不是
ThreadStatic
变量来存储ASP.NET请求的数据,因为您的请求可能由多个线程处理

如果你想在后台线程上做点什么呢?使用
ThreadStatic
数据和设置
Thread.CurrentPrincipal.Identity
以避免通过所有方法推送“全局”数据是否安全?我想没问题,但我想确定一下。例如,我没有在后台线程上执行的代码中显式使用任何
wait
,但我调用的库可能是;当此类异步代码处于非活动状态时,ASP.NET是否可以使用我的线程执行其他操作?如果是这样,那么在运行线程之前,在线程上设置一些“线程静态”数据和用户信息的安全方法是什么

我很清楚在ASP.NET的后台线程上运行代码。但有时你必须做你必须做的事。在我的例子中,我们还没有在.NET4.5.2上,所以不能使用,所以我们使用来运行后台任务。这在内部使用
Task.Run()

例如,我会这样做:

public class SomeController
{    
    public void AControllerMethod()
    {
        // Get the data set earlier on the HttpContext
        var theData = HttpContext.Current.Items["importantKey"];
        var thePrincipal = HttpContext.Current.User;
        // Pass it to the background thread method
        BackgroundTaskManager.Run(() => DoStuffInBackground(theData, thePrincipal));        
    }

    private void DoStuffInBackground(string someData, IPrincipal user)
    {
        // Store the data & principal for use by logic classes later
        Thread.CurrentPrincipal = user;
        MyContextService.BackgroundData["importantKey"] = someData;

        // do some things, some of which would call:
        aLogicClass.DoSomethingDeepInLogicCode();
    }    
}

public class ExampleLogicClass
{    
    private void DoSomethingDeepInLogicCode()
    {
        // get the context info
        var user = Thread.CurrentPrincipal;
        var theData = MyContextService.GetContextData("importantKey");

        // .. do stuff with user & data....        
    }    
}

public static class MyContextService
{     
    [ThreadStatic]
    public static ConcurrentDictionary<string,object> BackgroundData = new ConcurrentDictionary<string,object>();

    // Gets data from HttpContext or from BackgroundData 
    public object GetContextData(string key)
    {
        if (HttpContext.Current != null)
        { 
            var data = HttpContext.Current.Items[key]; 
            if (data != null) return data;
        } 
        if (BackgroundData.ContainsKey(key))
        {
            return BackgroundData[key];
        }
        return null;
    }    
} 
公共类控制器
{    
公共无效控制方法()
{
//在HttpContext上更早地获取数据集
var theData=HttpContext.Current.Items[“importantKey”];
var thePrincipal=HttpContext.Current.User;
//将其传递给后台线程方法
BackgroundTaskManager.Run(()=>DoStuffInBackground(theData,thePrincipal));
}
私有void DoStuffInBackground(字符串someData,IPrincipal用户)
{
//存储数据&principal,供逻辑类以后使用
Thread.CurrentPrincipal=用户;
MyContextService.BackgroundData[“importantKey”]=someData;
//做一些事情,其中一些会叫:
aLogicClass.DoSomethingDeepInLogicCode();
}    
}
公共类示例逻辑类
{    
私有void DoSomethingDeepInLogicCode()
{
//获取上下文信息
var user=Thread.CurrentPrincipal;
var theData=MyContextService.GetContextData(“importantKey”);
//…处理用户和数据。。。。
}    
}
公共静态类MyContextService
{     
[线程静态]
公共静态ConcurrentDictionary BackgroundData=新ConcurrentDictionary();
//从HttpContext或BackgroundData获取数据
公共对象GetContextData(字符串键)
{
if(HttpContext.Current!=null)
{ 
var data=HttpContext.Current.Items[key];
如果(数据!=null)返回数据;
} 
if(背景数据。容器(关键))
{
返回背景数据[键];
}
返回null;
}    
} 
更新:

从a开始,我想我应该使用和LogicalGetData()来设置/获取与ThreadLocal/ThreadStatic数据等效的数据。(或在.net 4.6中)

如果有人能正确地了解这件事,再多澄清一下,那就太好了