我在排队后台工作项时试图保留调用主体.为此,我试图:
Thread.CurrentPrincipal = callingPrincipal;
但是当我这样做时,我得到一个ObjectdisposedException:
System.ObjectdisposedException: Safe handle has been closed
如何将当前主体保留在后台工作项中?
我可以以某种方式复制校长吗?
public voID Run<T>(Action<T> action){ _logger.DeBUG("@R_733_4403@ background work item"); var callingPrincipal = Thread.CurrentPrincipal; HostingEnvironment.QueueBackgrounDWorkItem(token => { try { // UNCOMMENT - THROWS EXCEPTION // Thread.CurrentPrincipal = callingPrincipal; _logger.DeBUG("Executing queued background work item"); using (var scope = DependencyResolver.BeginlifetimeScope()) { var service = scope.Resolve<T>(); action(service); } } catch (Exception ex) { _logger.Fatal(ex); } finally { _logger.DeBUG("Completed queued background work item"); } });}解决方法 事实证明,ClaimsPrincipal现在有一个复制构造函数.
var principal = new ClaimsPrincipal(Thread.CurrentPrincipal);
这似乎可以在保留所有身份和声明信息的同时解决问题.完整的功能如下:
public voID Run<T>(Action<T> action){ _logger.DeBUG("@R_733_4403@ background work item"); var principal = new ClaimsPrincipal(Thread.CurrentPrincipal); HostingEnvironment.QueueBackgrounDWorkItem(token => { try { Thread.CurrentPrincipal = principal; _logger.DeBUG("Executing queued background work item"); using (var scope = DependencyResolver.BeginlifetimeScope()) { var service = scope.Resolve<T>(); action(service); } } catch (Exception ex) { _logger.Fatal(ex); } finally { _logger.DeBUG("Completed queued background work item"); } });}总结
以上是内存溢出为你收集整理的c# – 在排队的后台工作项中保留主体全部内容,希望文章能够帮你解决c# – 在排队的后台工作项中保留主体所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)