Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/263.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# ExecuteSqlCommandAsync引发异常_C#_.net_Sql Server_Entity Framework - Fatal编程技术网

C# ExecuteSqlCommandAsync引发异常

C# ExecuteSqlCommandAsync引发异常,c#,.net,sql-server,entity-framework,C#,.net,Sql Server,Entity Framework,我正在使用实体框架并尝试调用存储过程,但ExecuteSqlCommandAsync正在引发未处理的异常。我已经输入了try/catch,但是代码没有进入catch块ExecuteSqlCommand方法工作正常 我得到的例外是 mscorlib.dll中发生System.NullReferenceException“” 我的代码: try { var inboundsParam = new SqlParameter(); inboundsParam.ParameterName

我正在使用实体框架并尝试调用存储过程,但
ExecuteSqlCommandAsync
正在引发未处理的异常。我已经输入了try/catch,但是代码没有进入catch块
ExecuteSqlCommand
方法工作正常

我得到的例外是

mscorlib.dll中发生System.NullReferenceException“”

我的代码:

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)
{
} 

有人知道代码可能出了什么问题吗?

通过使用ExecuteSqlCommandAsync(),代码正在另一个线程中运行,异常不受您的控制。未处理的异常可以在AppDomain.CurrentDomain.UnhandledException事件处理程序中全局处理。此异常处理程序只是防止应用程序崩溃,但不应用于解决问题。这只是供你参考

要解决您的问题,请使用另一种方法,创建您自己的异步任务,以便完全控制异常。并用相应的同步调用替换异步调用

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)
        {
        } 
    });
}

当你同步执行的时候,你仍然能得到它吗?它同步运行的很好。你可能需要显示更多的代码-发布这个代码所在的整个方法,以及调用这个方法的整个方法。我怀疑您的程序的工作方式是,它试图在存储过程完成并为它提供任何值之前使用incomefind值。您包含此代码的方法是否会返回
void
?如果是这样的话,这可能是“不,我没有”的复制品。我意识到方法调用链中的一个方法没有等待。我加了这个,现在效果很好。