跟踪静态构造函数的执行
我在这里遇到一个问题,其中一个类的静态构造函数在应该调用之前被调用。 (即,DI/IoC 未设置,并且它从服务定位器返回 null/异常)。
不幸的是,我对静态构造函数没有太多控制权,不要问我为什么它依赖于 DI/IoC 来设置,但确实如此。
在我的应用程序中,在我的 IoC 准备就绪之前,不应引用此类静态或其他任何内容,但静态构造函数无论如何都会执行。
有没有一种简单的方法来确定哪一行导致构造函数执行? 注意:我无法在静态构造函数
中设置断点,因为这一切都发生在 ASP.NET 远程调试器可以附加到 Web 服务器(在 Global.asax.cs 中)之前
I'm running into a problem here where a static constructor of one of my classes is being called before it should be. (I.e, DI/IoC isn't set up and it's getting null/exceptions back from the service locator).
I unfortunately don't have a lot of control over the static constructor, don't ask me why it's relying on DI/IoC to be set up, but it is.
In my app, nothing should be referencing this class static or otherwise before my IoC is ready to go, but the static constructor is executing anyway.
Is there an easy way to determine what line caused the constructor to execute? Note: I cannot breakpoint in the static constructor
because this is all happening before the remote debugger for ASP.NET can attach to the web server (in Global.asax.cs)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您无法控制静态构造函数何时执行。
将您正在执行的任何操作从构造函数移至静态 Initialize() 函数。
当你准备好时就打电话给那个人。
不依赖于静态构造函数何时执行。
检查此链接
You have no control as to when the static constructor is executed.
Move whatever you are doing from your constructor to a static Initialize() function.
Call that one whenever you are ready.
Do not depend on when the static constructor is executed.
Check this link
也许您应该跳过使用静态构造函数?是必然的吗?
Maybe you should skip using static constructor? Is it necessarily?
一如既往,使用:
As always, use:
这可以使用 Windbg 和 sosex 来完成。这是示例代码
以下是步骤
.load sosex
加载 sosex!mbm
*Code.Test..cctor*
静态构造函数,之后您可以发出
!mk
来获取callstack
的
!mk
的输出这是上述示例HTH
This could be done using Windbg and sosex. Here is the sample code
And here are the steps
.load sosex
!mbm
*Code.Test..cctor*
static constructor after which you could issue
!mk
to get thecallstack
Here is the output from
!mk
for the above sampleHTH
下面是我的静态构造函数调试经验,
当我尝试通过在静态字段引用的行上放置断点进行调试时,我没有获得对静态构造函数的调试控制。
我在静态构造函数入口处保留了断点,并从静态字段引用的行中删除了断点。现在调试控制开始进入静态构造函数代码。
此图显示了带有断点的编辑器的外观
Below is my static constructor debugging experience,
When I was trying to debug by placing a breakpoint on the line where the static field had referenced, I was not getting debug control on the static constructor.
I kept breakpoint on the static constructor entrance, removed the breakpoint from the line where the static field had referenced. Now Debug control started coming into the static constructor code.
This image shows how your editor with breakpoints would look like