在 .NET 中,System.Threading.Thread.CurrentPrincipal 未更新
在调试一些 .NET 代码时,我在这里遗漏了一些基本的东西:
public static void CreateServiceSessionStore()
{
ServiceSessionStore serviceSessionStore = new ServiceSessionStore();
serviceSessionStore.SetIdentity(System.Threading.Thread.CurrentPrincipal.Identity);
System.Threading.Thread.CurrentPrincipal = serviceSessionStore;
// Here, CurrentPrincipal still isn't a serviceSessionStore!
}
在这段代码中,一切似乎都在愉快地进行。但是......当我调试并在最后一行之前时,我正在查看 System.Threading.Thread.CurrentPrincipal。该值是一个 WebSessionStore 对象,这正是我所期望的,我认为最后一行应该将其更改为 ServiceSessionStore 对象。但事实并非如此。我可以查看 serviceSessionStore,它包含一个 ServiceSessionStore 对象,但是在该行运行之后,CurrentPrincipal 仍然包含一个 WebSessionStore 对象。不会抛出任何错误。
现在,除了这些对象的实际用途之外,有人可以提供一下为什么它似乎拒绝更新 CurrentPrincipal 的想法吗?
I'm missing something elementary here when debugging some .NET code:
public static void CreateServiceSessionStore()
{
ServiceSessionStore serviceSessionStore = new ServiceSessionStore();
serviceSessionStore.SetIdentity(System.Threading.Thread.CurrentPrincipal.Identity);
System.Threading.Thread.CurrentPrincipal = serviceSessionStore;
// Here, CurrentPrincipal still isn't a serviceSessionStore!
}
In this code, everything seems to chug merrily along. However...when I debug and am just before the last line, I'm looking at System.Threading.Thread.CurrentPrincipal. The value is a WebSessionStore object, which is what I expect, and what I am thinking the last line should change it to a ServiceSessionStore object. But it doesn't. I can look at serviceSessionStore, and it's holding a ServiceSessionStore object, but after the line runs CurrentPrincipal still contains a WebSessionStore object. No error is thrown.
Now, aside from what these objects actually do, can someone offer an idea about why it seems to be refusing to update CurrentPrincipal?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是一个调试器工件。调试表达式在专用调试器线程上计算。 CurrentPrincipal 是线程执行上下文的属性。这也是它可以成为静态属性的原因。不同的线程将具有不同的主体,因此调试器线程的主体也不相同。
你没有真正的问题。
This is a debugger artifact. Debug expressions are evaluated on a dedicated debugger thread. CurrentPrincipal is a property of the thread's execution context. Also the reason it can be a static property. Different threads will have different principals and the debugger thread's principal is therefore not the same.
You don't have a real problem.