来自全国各地的应用领域传播preventing Thread.CurrentPrincipal中 [英] Preventing Thread.CurrentPrincipal from propagating across application domains

查看:199
本文介绍了来自全国各地的应用领域传播preventing Thread.CurrentPrincipal中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有人,如果有可能从传播过应用程序域边界停止当前线程的IPrincipal的?我有超过已分配给该线程的IPrincipal无法控制,但我确实有过创建应用程序域控制。

(我想这样做的原因是为了prevent从存在的,如果主要对象类型的组件在其他域不可用的序列化错误。)

编辑: ExecutionContext.Sup pressFlow 看起来很有希望,但它似乎并没有实现这一目标。下面打印MyIdentity:

 静态无效的主要()
{
    ExecutionContext.Sup pressFlow();
    = Thread.CurrentPrincipal中新的GenericPrincipal(新GenericIdentity(MyIdentity),角色.Split());
    。AppDomain.CreateDomain(新域)DoCallBack(独立);
}

静态无效的隔离()
{
    Console.WriteLine(现任校长:+ Thread.CurrentPrincipal.Identity.Name); // MyIdentity
}
 

解决方案

您没有运行一个异步方法,目标函数是由同一个线程二级AppDomain中执行。这样主体不发生变化。这工作:

  VAR流量= ExecutionContext.Sup pressFlow();
        = Thread.CurrentPrincipal中新的GenericPrincipal(新GenericIdentity(MyIdentity),角色.Split());
        ThreadPool.QueueUserWorkItem((X)=> {
            。AppDomain.CreateDomain(新域)DoCallBack(独立);
        });
        flow.Undo();
 

或者,如果你只是想运行一个特定的背景下,同一个线程,那么你可以使用ExecutionContext.Run():

  VAR副本= ExecutionContext.Capture();
        = Thread.CurrentPrincipal中新的GenericPrincipal(新GenericIdentity(MyIdentity),角色.Split());
        ExecutionContext.Run(副本,新ContextCallback((X)=> {
            。AppDomain.CreateDomain(新域)DoCallBack(独立);
        }), 空值);
 

Does anyone if it's possible to stop the current thread's IPrincipal from propagating over an application domain boundary? I have no control over the IPrincipal that's assigned to the thread, but I do have control over creating the application domains.

(The reason I want to do this is to prevent a serialization error from occuring if the principal object type's assembly is unavailable in the other domain.)

Edit: ExecutionContext.SuppressFlow looks promising, but it doesn't appear to achieve the goal. The following prints "MyIdentity":

static void Main ()
{
    ExecutionContext.SuppressFlow ();
    Thread.CurrentPrincipal = new GenericPrincipal (new GenericIdentity ("MyIdentity"), "Role".Split ());
    AppDomain.CreateDomain ("New domain").DoCallBack (Isolated);
}

static void Isolated ()
{
    Console.WriteLine ("Current principal: " + Thread.CurrentPrincipal.Identity.Name);  // MyIdentity
}

解决方案

You didn't run an asynchronous method, the target function is executed in the secondary appdomain by the same thread. So the principal doesn't change. This works:

        var flow = ExecutionContext.SuppressFlow();
        Thread.CurrentPrincipal = new GenericPrincipal(new GenericIdentity("MyIdentity"), "Role".Split());
        ThreadPool.QueueUserWorkItem((x) => {
            AppDomain.CreateDomain("New domain").DoCallBack(Isolated);
        });
        flow.Undo();

Or if you just want to run the same thread with a specific context then you can use ExecutionContext.Run():

        var copy = ExecutionContext.Capture();
        Thread.CurrentPrincipal = new GenericPrincipal(new GenericIdentity("MyIdentity"), "Role".Split());
        ExecutionContext.Run(copy, new ContextCallback((x) => {
            AppDomain.CreateDomain("New domain").DoCallBack(Isolated);
        }), null);

这篇关于来自全国各地的应用领域传播preventing Thread.CurrentPrincipal中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆