You did not run the asynchronous method, the target function is executed in the secondary application domain by the same thread. Thus, the principle does not change. It 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 start the same thread with a specific context, 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);
source share