Octopus C# script step not finishing unawaited async Tasks

Search for a command to run...

No comments yet. Be the first to comment.
Polly is great library for easily configuring different kinds of policies around code execution, be it retry, circuit breaker, caching or lots of other things. It's probably most often used around HTTP requests. If you haven't heard about it you defi...

This is part 2 of my series on unit testing in .NET (C#) and will be about my conventions regarding naming unit tests, how to structure them and which parts to test. And lastly I will help you increase readability of assertions and potential error me...

I feel like unit testing is very important and very beneficial in a lot of ways. It helps with not breaking existing functionality and thereby also making you more confident in changing code without fear of breaking important stuff. Often it also mak...

I've been working with Swagger through NSwag a lot recently and also needed to get it to work when hidden behind a reverse proxy - i.e. another service forwarding a request to the service exposing the Swagger UI. Some of the issues I've had along the...

The other day I was writing a unit test to verify what was supposed to happen when a Task would throw an exception when awaited. It wasn't as straight forward as I first assumed, but I quickly found this Stack Overflow post that helped me: https://st...

You might find yourself wanting to start an async Task either through Task.Run(() => ...) or something like Task DoSomethingAsync() or Task.Delay() where you don't want to await the completion of the task. You just want the step to complete and the task to continue running in the background. But it wont work.
In my situation I wanted to hit a URL 10 minutes after the Octopus deployment was completed and I didn't want the deployment to "hang" for 10 minutes seemingly still deploying.
I tried several different methods, like mentioned in the beginning, but the tasks were never run to completion.
I'm pretty sure I know why this happens, although I haven't actually confirmed it. I'm assuming Octopus fires up a new ScriptCS process with our C# script as the input. When our script reaches the end, i.e. not awaiting any tasks etc., then the process exits and any unfinished tasks goes with it.
I ended up with sort of an alternative solution using Azure Functions and Queue Storage which is free (or close to) for most solutions that don't require a lot of throughput.
It's pretty simple and in short it consists of an HTTP endpoint which adds (delayed) a message to the queue, and then a function with a queue trigger which executes the message. In Octopus I then just call the HTTP endpoint to trigger a delayed action and that's that.
I'll leave it at that and leave the details of the Azure stuff to a future blog post.