I am making a number of REST est calls as follows:
using (var scope = new TransactionScope())
{
try
{
HttpResponseMessage response = MakeRestCall();
if (response.IsSuccessStatusCode)
{
// do stuff;
}
else
{
throw new Exception("API call not successful");
}
response = MakeRestCall2();
if (response.IsSuccessStatusCode)
{
// do stuff;
}
else
{
throw new Exception("API call not successful");
}
MakeDatabaseconnection()
scope.Complete();
}
catch (Exception ex)
{
// Handle any exceptions that occurred during the API call
Console.WriteLine("Exception occurred: " + ex.Message);
scope.Dispose();
}
}
private void MakeDatabaseconnection()
{
string connectionStringTables = "<conn string value>";
using (SqlConnection connection = new SqlConnection(connectionStringTables))
{
connection.Open(); // I get an error here - null reference
}
}
If I look at connection, it is not null, but the code throws an exception.
How can I make the TransactionScope commit only if all the API calls and the database actions are successful?
Does the procedure MakeDatabaseconnection have to have a dependent scope? If so how can I do that?