来自全国各地的应用领域传播preventing Thread.CurrentPrincipal中

编程入门 行业动态 更新时间:2024-10-25 02:27:56
本文介绍了来自全国各地的应用领域传播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中

本文发布于:2023-11-12 23:45:36,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1582840.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:全国各地   应用领域   preventing   Thread   CurrentPrincipal

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!