In my Azure function, I am executing a stored procedure. Now, based on some event, I want to cancel the execution of this particular instance. Sample code below (very simplified for example purpose):-
public async Task AddTestMessage(string name, CancellationToken token)
{
try
{
using (SqlConnection connection = new SqlConnection(CONNECTION_STRING))
{
using (SqlCommand command = new SqlCommand("uspTest", connection))
{
command.CommandType = CommandType.StoredProcedure;
SqlParameter[] sqlParameters = new SqlParameter[]
{
new SqlParameter("name", name),
};
command.Parameters.AddRange(sqlParameters);
token.Register(() => command.Cancel());
connection.Open();
await command.ExecuteNonQueryAsync();
}
}
}
catch (SqlException ex)
{
Console.WriteLine($"Cancellation Requested: {ex.Message}");
if (token.IsCancellationRequested)
{
token.ThrowIfCancellationRequested();
}
}
}
Now, in any other application it's very easy to set the CancellationTokenSource passed to this method, but in Azure functions since we can't have multiple trigger, I can think of no way if it's possible to cancel the SQL Server stored procedure.
I know it's possible with a durable function but just want to know if there is any way to achieve this using normal function?