You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

.NET 5 WPF应用上线后频发TypeInitializationException问题求助

.NET 5.0 WPF应用上线后频繁触发System.TypeInitializationException问题

我开发的一款用于分离容器计算的.NET 5.0.17 WPF应用已迭代两年半,上月正式上线。应用以UserControls为核心组件,通过ViewModel调用「计算运行器」异步执行长耗时计算。

上线后几乎每日都会触发System.TypeInitializationException,但开发阶段在4台设备上从未出现该问题。异常触发无规律且涉及多个类,即便备份崩溃前的项目也无法复现。异常发生在Task.Run代码块内,抛出TypeInitializationException,内部异常为NullReferenceException,但无具体空引用类信息。尝试在Task.Run内添加try-catch也无法捕获,推测异常源于Task.Run内部。

异常详情

外层异常

The type initializer for '<Module>' threw an exception.
   at MySep.Studio.DesignCalculationRunner.Run(Int32 vesselIndex, Boolean openProjectFile)
   at System.Runtime.CompilerServices.AsyncMethodBuilderCore.Start[TStateMachine](TStateMachine& stateMachine)
   at MySep.Studio.DesignCalculationRunner.Run(Int32 vesselIndex, Boolean openProjectFile)
   at MySep.Studio.DesignCalculationRunner.<>c__DisplayClass0_0.<<RunAsync>b__1>d.MoveNext()
--- End of stack trace from previous location ---
   at MySep.Studio.DesignCalculationRunner.RunAsync(Int32 vesselIndex, Action chainMethod, Boolean openProjectFile)
   at System.Threading.Tasks.Task.<>c.<ThrowAsync>b__140_0(Object state)
   at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
   at System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source, Delegate callback, Object args, Int32 numArgs, Delegate catchHandler)
MySep.Studio

内部异常

System.NullReferenceException: Object reference not set to an instance of an object.
(without any further information on where the exception happens)

相关代码

internal static async void RunAsync(int vesselIndex, Action chainMethod = null, bool openProjectFile=false)
{
    //ParamName is undefined now, should be vessel tag or so, "Vessel" is fine for now
    DockHelper.ToggleSpinner(true, "Running design calculations...");

    //Await project
    Project proj = await Task.Run(async () =>
    {
        return await Run(vesselIndex, openProjectFile);
    });

    //Refresh application
    DockHelper.RefreshAllControls(vesselIndex);

    //After calc method
    if (chainMethod != null) chainMethod();

    //Event bus after calc
    EventBus.Publish(Events.PerformanceCalculation);

    //Wait for spinner
    await Task.Run(async () =>
    {
        await Task.Delay(400);
    });

    //Toggle spinner
    DockHelper.ToggleSpinner(false);
}

internal static async Task<Project> Run(int vesselIndex, bool openProjectFile=false)
{
    //Create param
    MySepParam mySepParam = new MySepParam();
    mySepParam.LiquidLevelSetIndex = 0;
    mySepParam.SectionName = "Vessel";
    mySepParam.VesselIndex = vesselIndex;
    mySepParam.BEAccessCode = Global.BEAccessCode;
    mySepParam.OpenProjectFile = openProjectFile;

    //Set globals
    Global.ProjectName = Global.Project.Name;
    Global.VesselIndex = vesselIndex;

    //Write sentry breadcrumb
    SentryHelper.AddBreadcrumb($"Calculation - Design");

    //Run
    var allResults = Global.Project.VesselsResults;
    var vesselResult = DesignCalculationHelper.DesignCalculation(Global.Project.Vessels[vesselIndex], mySepParam).VesselResults;
    if (allResults.Count <= vesselIndex)
        allResults.Add(vesselResult);
    else
        allResults[vesselIndex] = vesselResult;

    await Task.Delay(1);
    return Global.Project;
}

已尝试的无效方案

  • 为所有代码添加try-catch块
  • 在Jira中抛出InnerException而非外层异常
  • 排查可能存在空值的类

补充说明

有观点认为该问题与其他类似问题重复,但实际情况不同:其他问题是因静态变量缺失导致应用无法运行,而我的异常仅提示「The type initializer for '' threw an exception」,无具体异常位置。

此问题更接近一个12年前的.NET 2.0问题,但该问题内部异常为「C++模块初始化失败」,解决方案是修复系统后重装.NET,而我认为并非.NET版本不稳定,因为大量用户均遇到过该问题。

内容的提问来源于stack exchange,提问作者Sep Vrij

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.02 16:24:55