0

I am using Entity Framework and trying to call a stored procedure, but ExecuteSqlCommandAsync is is throwing an unhandled exception. I have put try/catch but code is not going into the catch block. ExecuteSqlCommand method works fine.

The exception I am getting is

System.NullReferenceException' occurred in 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?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Irfan
  • 566
  • 7
  • 13
  • Do you still get it when you execute synchronously? – Caius Jard Oct 27 '17 at 05:06
  • It works fine synchronously. – Irfan Oct 27 '17 at 05:37
  • 1
    You're probably going to need to show more code - post the entire method this code is found in, plus the entire method that calls this method. I suspect that the way your program works, it is attempting to use the incomeFound value before the stored procedure has completed and provided any value for it – Caius Jard Oct 27 '17 at 06:22
  • Does your method containing this code return `void` by any chance? If so, this is probably a duplicate of [https://stackoverflow.com/questions/5383310/catch-an-exception-thrown-by-an-async-method](https://stackoverflow.com/questions/5383310/catch-an-exception-thrown-by-an-async-method) – Hintham Oct 27 '17 at 07:21
  • No I doesn't. I have realized one of the methods in the method call chain didn't have await. I added that and it works fine now. – Irfan Jan 12 '18 at 00:45

1 Answers1

1

By using ExecuteSqlCommandAsync(), the code is running in another thread, and Exceptions are out of your control. Unhandled exceptions can be globally handled in AppDomain.CurrentDomain.UnhandledException event handler. 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)
        {
        } 
    });
}
Showching Tong
  • 201
  • 2
  • 3