简体   繁体   English

ExecuteSqlCommandAsync 抛出的异常

[英]Exception thrown by ExecuteSqlCommandAsync

I am using Entity Framework and trying to call a stored procedure, but ExecuteSqlCommandAsync is is throwing an unhandled exception.我正在使用实体框架并尝试调用存储过程,但ExecuteSqlCommandAsync正在引发未处理的异常。 I have put try/catch but code is not going into the catch block.我已经放置了 try/catch 但代码没有进入 catch 块。 ExecuteSqlCommand method works fine. ExecuteSqlCommand方法工作正常。

The exception I am getting is我得到的例外是

System.NullReferenceException' occurred in mscorlib.dll System.NullReferenceException' 发生在 mscorlib.dll 中

My code:我的代码:

try
{
    var inboundsParam = new SqlParameter();
    inboundsParam.ParameterName = "@Inbounds";
    inboundsParam.SqlDbType = SqlDbType.Xml;
    inboundsParam.Direction = ParameterDirection.Input;
    inboundsParam.Value = inboundsXml;

    var incomeFoundOutParam = new SqlParameter();
    incomeFoundOutParam.ParameterName = "@IncomeFound";
    incomeFoundOutParam.SqlDbType = SqlDbType.Bit;
    incomeFoundOutParam.Direction = ParameterDirection.Output;

    var output = await dbContext.Database.ExecuteSqlCommandAsync("EXEC dbo.CalculateIncome @Inbounds, @IncomeFound OUTPUT", inboundsParam, incomeFoundOutParam);
    var incomeFound = (bool)incomeFoundOutParam.Value;
}
catch(System.Exception ex)
{
} 

Does anyone know what could be wrong with the code?有谁知道代码可能有什么问题?

By using ExecuteSqlCommandAsync(), the code is running in another thread, and Exceptions are out of your control.通过使用 ExecuteSqlCommandAsync(),代码在另一个线程中运行,并且异常不受您的控制。 Unhandled exceptions can be globally handled in AppDomain.CurrentDomain.UnhandledException event handler.未处理的异常可以在 AppDomain.CurrentDomain.UnhandledException 事件处理程序中全局处理。 This exception handler just keep your application from crash, but should not be used to solve your problem.此异常处理程序只是防止您的应用程序崩溃,但不应用于解决您的问题。 This is just for your information.这只是为了您的信息。

To solve your problem, use an alternative way, create your own async tasks, so that you have the full control of exceptions.要解决您的问题,请使用替代方法,创建您自己的异步任务,以便您可以完全控制异常。 And replace your async call with the corresponding sync call.并用相应的同步调用替换您的异步调用。

public async void YourMethodAsync()
{
    // NOTE HERE TO CREATE YOUR OWN TASK, SO THAT YOU HAVE CONTROL TO THE EXCEPTION
    Task.Run(()=>{
        try
        {
            var inboundsParam = new SqlParameter();
            inboundsParam.ParameterName = "@Inbounds";
            inboundsParam.SqlDbType = SqlDbType.Xml;
            inboundsParam.Direction = ParameterDirection.Input;
            inboundsParam.Value = inboundsXml;

            var incomeFoundOutParam = new SqlParameter();
            incomeFoundOutParam.ParameterName = "@IncomeFound";
            incomeFoundOutParam.SqlDbType = SqlDbType.Bit;
            incomeFoundOutParam.Direction = ParameterDirection.Output;

            // NOTE HERE, USE SYNC METHOD CALL.
            var output = dbContext.Database.ExecuteSqlCommand("EXEC dbo.CalculateIncome @Inbounds, @IncomeFound OUTPUT", inboundsParam, incomeFoundOutParam);
            var incomeFound = (bool)incomeFoundOutParam.Value;
        }
        catch(System.Exception ex)
        {
        } 
    });
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM