Cancel or Stop an Azure Function Instance

Singh, Rahul 16 Reputation points
2022-06-22T16:46:55.943+00:00

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?

Azure Functions
Azure Functions
An Azure service that provides an event-driven serverless compute platform.
Developer technologies | C#
Developer technologies | C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Bruce (SqlWork.com) 81,971 Reputation points Volunteer Moderator
    2022-06-22T18:40:20.15+00:00

    what triggers the cancel?

    public async Task AddTestMessage(string name)  
    {  
          // create token with timeout  
          var tokenSource = CancellationTokenSource(TIMEOUT);   
          var token = tokenSource.Token:  
            
          // optional: run watcher code to cancel early before timeout  
          Task.Run((token)=>Watcher(token), token);  
             
          // use as normal        
            
      
    

  2. MughundhanRaveendran-MSFT 12,511 Reputation points
    2022-06-23T09:19:00.66+00:00

    Hi @Singh, Rahul ,

    Unfortunately, there is no other way of terminating the function instance apart from using durable functions.

    https://learn.microsoft.com/en-us/azure/azure-functions/durable/durable-functions-instance-management?tabs=csharp#terminate-instances

    Apologies for the inconvenience caused!


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.