C#WinForms崩溃,没有异常 - c#

我有一个C#WinForms应用程序。

我正在使用Unity Container nuget包来注入我的服务类。如果我在Program.cs中获得服务类,并调用我的Web api以使用odata验证用户名和密码,则它成功运行。如果我调用Application.Run(myForm)(其中myForm是Telerik RadForm)并异步运行相同的调用,则应用程序将关闭,不会引发任何异常。

我正在使用Application.ThreadException并注册Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException),但这什么都没有。

导致应用程序崩溃的行是对System.Net.Http.HttpClient.PostAsync()的调用。此调用是从位于单独的.Net Standard 2.0 Class Library中的服务类内部进行的。主应用程序是.NET Framework 4.7.2 Windows Application,UI控件位于.NET Framework 4.7.2 Class Library中。同样,如果在Telerik RadForm之外调用,则此调用成功。

在Telerik RadForm中,我尝试使用以下命令执行调用:

private async void btnLogin_Click(object sender, System.EventArgs e)
var t = new Thread(() => Runner.DoWork(new Credentials(u, p, CLIENT_ID)))
不使用await异步调用静态方法

在服务类上调用LoginAsync(...).Result时,确实得到了不同的结果。这导致应用程序进入线程锁定状态,但没有使应用程序崩溃。

更新:

有两个RadForms。 DashboardFormLoginFormProgram.cs启动仪表板,该仪表板检查现有的Bearer令牌。如果令牌不存在,则仪表板将显示使用.ShowDialog()的登录名,以防止在经过身份验证之前使用仪表板。然后,登录名使用上述服务类。

如果不是使用Program.csApplication.Run()中启动仪表板,而是启动“登录名”,则用于身份验证的服务调用成功。

然后,我尝试在新线程中从仪表板启动登录,但这导致了上述相同的问题。

如果System.Net.Http.HttpClient.PostAsync()从另一个窗体显示的窗体中调用,为什么会导致应用程序崩溃(无异常)?

程序

static class Program
{
    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);

        WinInjectionHelper.Register(UnityConfig.RegisterComponents);

        Application.ThreadException += Application_ThreadException;
        Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);

        Application.Run(new DashboardForm());
    }

    private static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)
    {
        MessageBox.Show("Big error...");
    }

仪表板

public partial class DashboardForm : Telerik.WinControls.UI.RadForm, IDashboard
{
    private readonly IProductService _service;

    public DashboardForm()
    {
        this._service = WinInjectionHelper.Generate2<IProductService>();
        this.InitializeComponent();
    }

    private void DashboardForm_Load(object sender, EventArgs e)
    {
        if (this.NotAuthenticated())
        {
            var frm = new LoginForm();

            if (frm.ShowDialog() != DialogResult.OK)
            {
                this.Close();
            }
        }
        else
        {
            //TODO: display error to user
            this.Close();
        }
    }
}

登录

public partial class LoginForm : Telerik.WinControls.UI.RadForm
{
    private const string CLIENT_ID = "...";
    private readonly IAuthenticationService _service;

    public LoginForm()
    {
        this._service = WinInjectionHelper.Generate2<IAuthenticationService>();
        this.InitializeComponent();
    }

    private void btnCancel_Click(object sender, System.EventArgs e)
    {
        this.DialogResult = DialogResult.Cancel;
        this.Close();
    }

    private async void btnLogin_Click(object sender, System.EventArgs e)
    {
        var u = this.txtUsername.Text.Trim();
        var p = this.txtPassword.Text.Trim();

        if (string.IsNullOrWhiteSpace(u))
        {
            MessageBox.Show("Username is required!", "NOTICE", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            return;
        }

        if (string.IsNullOrWhiteSpace(p))
        {
            MessageBox.Show("Password is required!", "NOTICE", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            return;
        }

        try
        {
            var jwt = await this._service.LoginAsync(new Credentials(u, p, CLIENT_ID));

            var identity = new ClaimsIdentity(new[]
            {
                new Claim(ClaimTypes.Name, u),
                new Claim(ClaimTypes.Email, u),
                new Claim(ClaimTypes2.AccessToken, jwt.AccessToken),
            }, "JWT");

            ((GenericPrincipal)Thread.CurrentPrincipal).AddIdentity(identity);

            this.DialogResult = DialogResult.OK;

            this.Close();
        }
        catch
        {
            MessageBox.Show("An error occurred while processing your request.", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
    }
}

参考方案

您可以检查Windows事件查看器并在应用程序下查找以查找崩溃日志。我正在使用的应用程序也存在相同的问题,这是唯一包含任何信息的地方。它实际上帮助我解决了与定位特定.NET Framework版本有关的问题。单击左下方的“窗口”按钮,然后在其中键入“事件查看器”,然后单击显示的图标。

最简单的方法是运行该应用程序并使它崩溃,然后立即转到事件查看器,它将位于列表顶部。

当回复有时是一个对象有时是一个数组时,如何在使用改造时解析JSON回复? - java

我正在使用Retrofit来获取JSON答复。这是我实施的一部分-@GET("/api/report/list") Observable<Bills> listBill(@Query("employee_id") String employeeID); 而条例草案类是-public static class…

jQuery删除和$(this) - php

我在remove()方法上遇到问题。我无法删除$(this)对象。我的代码是:$(".submit_add_type").live("click", function() { var parent = $(this).parent(); var type_value = parent.children('.t…

改造正在返回一个空的响应主体 - java

我正在尝试使用Retrofit和Gson解析一些JSON。但是,我得到的响应机构是空的。当我尝试从对象中打印信息时,出现NullPointerException。我确保URL正确,并且我也确保POJO也正确。我正在使用jsonschema2pojo来帮助创建POJO类。这是我要解析的JSON{ "?xml": { "@versi…

json数组,其中in数组返回错误?坏字符串 - javascript

我将json字符串文件解析为python,并且始终返回error。我使用了在线json格式化程序和验证器,它们也返回错误,因此我需要帮助使我的json正确并告诉我错误 [{ "sentence_id": "TR.00001", "sentence": { "text": …

如何正确增加喜欢和不喜欢的人 - javascript

我在每个帖子上都有一个按钮1)点赞按钮如果用户已经喜欢了该帖子,则显示“与众不同”按钮如果用户不喜欢该帖子,则为HTML部分<a href="javascript:void(0);" id="liker" data-count="0" data-fpc="481" data…