#SharePointProblems | Koskila.net

Solutions are worthless unless shared! Antti K. Koskela's Personal Professional Blog
>

How to configure Azure Function's startup

koskila
Reading Time 3 min
Word Count 515 words
Comments 4 comments
Rating 4.3 (7 votes)
View

Azure Functions use dependency injection, and that allows us to define all kinds of prebuilt or custom-built services as being available for our functions really easily. This is a really easy way to initialize scoped resources to be used in your different functions... But how do you actually configure them in an Azure Functions App?

Problem

Okay, a step back - there's a bit of a problem that we need to resolve before we can proceed with the configuration. When you create a new Azure Functions project, there's a thing we're missing:

A new Azure Function in a Visual Studio project.

A new Azure Function in a Visual Studio project.

That's right! There's no StartUp.cs or similar file. Nothing like Program.cs either.

So... If we don't even get access to the file, how do we add our dependency injections in the Configure -class?

Luckily, once again, the solution is simple!

Solution

We'll simply need to copy-paste some code from the internet! In this case, just grab the snippets below, or you can try this GitHub repo where I committed a fully functional example project:

https://github.com/dodyg/practical-aspnetcore/tree/3.1-LTS/projects/azure-functions/StartupExample

If you don't want to browse the repo, you could just follow the steps below! What I'm doing is adding a new class, designating it as something that's run during the StartUp, and then adding the service configuration there.

Time needed: 10 minutes.

How to add a StartUp.cs file for your Azure Functions project?

  1. Add a new C# class file to your project.

    This file can be named pretty much anything, but I reckon a good convention is to place it at the root of your project and maybe even call it StartUp.cs

  2. Select the file and open it up

    It'll look somewhat like this:

  3. Add the following using -statements

    using Microsoft.Azure.WebJobs; using Microsoft.Azure.WebJobs.Hosting;
    In a matter of seconds, these will give you access to the attributes and interfaces that you will need!

  4. Add the following attribute to your namespace

    [assembly: WebJobsStartup(typeof(StartUp))]

    This defines your class to be run at the startup of your Azure Functions app!

  5. Add the following interface to your StartUp class

    StartUp : IWebJobsStartup

    That'll instruct the runtime to run the right thing, and gives you instructions on what to implement, such as...

  6. Implement Configure ()

    Somewhat like below:

    public void Configure(IWebJobsBuilder builder) { }

  7. Finally, add a reference to your own project

    In my case, it's like below:

    using StartUpExample;

    Below is an example of all of the things you need to add:

  8. Now you can configure your services just like you would in a .NET Core Web API project!

    ... well, almost. Adding services happen by accessing the IServiceCollection under builder - like so, for adding an Entity Framework Core database context with some configuration options presented:

    builder.Services.AddDbContext(options1 => { options1.UseLazyLoadingProxies(false); options1.UseSqlServer( config["ConnectionStrings:DefaultConnection"], builder => { builder.EnableRetryOnFailure(5, TimeSpan.FromSeconds(10), null); builder.CommandTimeout(10); } ); });


And you should be done! You can run something like below to verify that the Configure -method gets to run and your breakpoint actually does stick:

Hitting a breakpoint when debugging Azure Function Startup.cs

Hitting a breakpoint when debugging Azure Function Startup.cs

References

Probably the best resource (but a broader sample) is this blog post by Gunnar Peipman.

Comments

Interactive comments not implemented yet. Showing legacy comments migrated from WordPress.
Adem
2022-03-02 18:05:21)
Thank you so much Antti! You saved my day! I was trying to implement dependency injection in my Azure function and read a lot of blog posts. Most examples, including of Microsoft, use Microsoft.Azure.Functions.Extensions package. But that package has a dependency to Microsoft.Extensions.DependencyInjection >= 2.1.0. Therefore, it wasn't possible to use .Net 6 and Azure Function v4. Fortunately, it's working now with your implementation.
Antti K. Koskela
2022-03-04 21:46:02
Hi Adem, Thanks for your comment - I'm really happy to hear it was helpful :) Happy coding!
Arman G.
2022-03-07 00:02:27)
Thank you for this article! One question though, how did you get a reference to the 'config' instance in your code below?
config["ConnectionStrings:DefaultConnection"]
Antti K. Koskela
2022-03-16 13:12:42
Hi Arman, Thanks for your comment! you can instantiate it using ConfigurationBuilder. In practice, this should look something like below:
var config = new ConfigurationBuilder()
	.SetBasePath(context.FunctionAppDirectory)

	// This gives you access to your application settings 
	// in your local development environment
	.AddJsonFile("local.settings.json", optional: true, reloadOnChange: true)

	// This is what actually gets you the 
	// application settings in Azure
	.AddEnvironmentVariables()
	.Build();
I have described this in more details in this article: https://www.koskila.net/how-to-access-azure-function-apps-settings-from-c/ Hope this helps!