Dependency Injection in Azure Functions
...and how to use HttpClient correctly!

Copenhagen based cloud developer. Love to code and to make complex solutions simple!
Search for a command to run...
...and how to use HttpClient correctly!

Copenhagen based cloud developer. Love to code and to make complex solutions simple!
No comments yet. Be the first to comment.
This series will cover lots of practical and hard learned experiences developing Azure Functions. Read about how to design your microservice setup, how to implement the functions using triggers.
Are you using HttpClient in your .net projects as well as Azure Functions as general .net projects? That is awesome, however, in this post I am going to talk about the IHttpClientFactory, what benefits you gain from using it, how to use it and why I ...
And examples for .NET projects also

...and why Windows is not going to sleep when left idle

TL;DR Keep trying the firmware update - after around 30 attempts doing the same thing, it just worked 🤯🎉🪅 Awesome Ultrawide Monitor, not only for gaming For once I'm going to write an article which is not directly related to programming, but if yo...

When using GitHub actions it's pretty neat that you can use the hosted GitHub runners. However, this can get costly if you use a lot of runners and they are not always that fast at running the actions. Luckily you can also host your own runners and w...

...and some SonarCloud code analysis!

A common scenario when using Azure Functions is to be able to do HTTP request. The HttpClient is the preferred tool for that, so how do we use HttpClient correctly?
By 'correctly' I refer to the fact that for years many .net developers (my self included) has been disposing the HttpClientafter each use. The reason for this is because HttpClient implements IDisposable and as a good developer, you have learned always to dispose the resources you are not using, well, what if you intend to use it again shortly after? There is a great Visual Studio Magazine article about that.
So, since we don't want to dispose the HttpClientafter each use, we are going to reuse, but how do we do that in a stateless environment as our Functions are running in?
Simple enough, we let the runtime handle that by using dependency injection. In fact there are built in library support for DI in the official runtime.
We have 2 options:
HttpClient for injectionHttpClientFactory and get a reference when we need itIn this article I will go with the first, for simplicity, later I will publish an article about the HttpClientFactory because that has some really nice features.
We need some Nugets to help us out:
Install-Package Microsoft.Extensions.Http -Version 3.1.14
Install-Package Microsoft.Extensions.DependencyInjection -Version 3.1.14
Install-Package Microsoft.Azure.Functions.Extensions -Version 1.1.0
Install-Package Microsoft.NET.Sdk.Functions -Version 3.0.11
Note if you want to deploy to Azure then stick with the version 3.x of the 2 first packages, as there is not full natively support for .net 5 yet.
Startup.csWe need to add a new class to our Function project, name it Startup.cs and define it like this:
using Microsoft.Azure.Functions.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection;
[assembly: FunctionsStartup(typeof(MyFunction.Startup))]
namespace MyFunction
{
public class Startup : FunctionsStartup
{
public override void Configure(IFunctionsHostBuilder builder)
{
builder.Services.AddHttpClient();
}
}
}
Now can use our injected dependency in our functions, all we need to do is remove the static things from our class and method definitions, then we need to add a constructor that takes the HttpClient as a parameter, like this:
public class Function1
{
private readonly HttpClient httpClient;
public Function1(HttpClient httpClient)
{
this.httpClient = httpClient;
}
[FunctionName("Function1")]
public async Task Run([TimerTrigger("0 */5 * * * *")]TimerInfo myTimer, ILogger log)
{
log.LogInformation($"C# Timer trigger function executed at: {DateTime.Now}");
await httpClient.GetAsync("blog.jepsen.ninja");
}
}
That is really all there is needed to use DI and a HttpClient in an Azure Function. Stay tuned for my coming post about how to use the HttpClientFactory, and please let me know if you have any thoughts, comments, feedback or something else. Thanks for reading!