Skip to content
  • English Deutsch

benjamin-abt.com

Performance is a feature | Cloud - AI - Azure - .NET - Sustainability

  • Home
  • Blog
  • Portfolio
    • Overview
    • Workshops & Training
    • Publications
  • Contact
  • Home
  • Blog

Dependency Injection with Azure Functions and FunctionsStartup

Dependency Injection with Azure Functions and FunctionsStartup
February 4, 2020 2 min read BEN ABT
Azure Functions Serverless

This article was written in 2020. Versions, tools and recommendations may have changed since then. Newer posts are collected under Azure Functions.

Azure Functions provides a very lean way to execute code serverless. So lean, in fact, that there is no dependency injection by default.

With the help of NuGet and the Azure Function Extensions, however, Dependency Injection can be easily retrofitted, as the pattern itself is naturally supported.

Required NuGet Packages

First of all you need those NuGet Packages

  • Microsoft.Azure.Functions.Extensions
  • Microsoft.NET.Sdk.Functions

Code

All the magic is done by FunctionsStartup.

With the help of FunctionsStartup we can configure our services in a startup class, e.g. from ASP.NET Core.

In principle, it is even possible to outsource the entire Statup content and share it with other .NET Core environments. The signatures and the behavior is identical to all other startup configurations. It is fully compatible.

C#
 1[assembly: FunctionsStartup(typeof(MyCompany.MyApp.Startup))]
 2namespace MyCompany.MyApp
 3{
 4    public class Startup : FunctionsStartup
 5    {
 6        public override void Configure(IFunctionsHostBuilder builder)
 7        {
 8            // Http Client
 9            builder.Services.AddHttpClient();
10
11            // Analaytic Options
12            builder.Services.Configure<AnalyticsOptions>(options =>
13                {
14                    options.Url = Environment.GetEnvironmentVariable("ANALYTICS_SERVICE_API");
15                });
16            builder.Services.AddScoped<IAnalyticsService, NoiseAnalyticsService>();
17
18            // Notification
19            builder.Services.AddScoped<INotificationProvider, NotificationProvider>();
20
21            // Engine
22            builder.Services.AddMediatR(typeof(DeviceProcessingEngine));
23        }
24    }
25}

In the Azure Function Trigger, the respective instance of a service or another class can now simply be specified via the constructor as usual, so that it can be used in the class.

C#
 1public class IoTHubMessageProcessor
 2{
 3    private readonly IMediator _mediator;
 4
 5    public IoTHubMessageProcessor(IMediator mediator)
 6    {
 7        _mediator = mediator;
 8    }
 9
10    [FunctionName("iot-hub-message")]
11    public async Task Run(
12        [EventHubTrigger("device-sensor-events", 
13            Connection = "AZURE_EVENT_HUB_DTSENSOR_INGEST_CONNECTIONSTRING")] EventData[] eventData,
14        ILogger log)
15    {
16        if (eventData is null)
17        {
18            log.LogWarning($"Received sensor event but no messages were passed.");
19            return;
20        }

Related articles

Azure Functions with MediatR and Dependency Injection

Mar 4, 2020 - 4 min read

Azure Functions with MediatR and Dependency Injection

MediatR MediatR is one of the most popular libraries in the .NET environment and describes itself as “Simple, unambitious mediator …

Run Hono on Azure Static Web Apps: managed Functions APIs for static frontends

Jul 19, 2026 - 8 min read

Run Hono on Azure Static Web Apps: managed Functions APIs for static frontends

Azure Static Web Apps is primarily a host for static frontend assets, but it can also deploy a small HTTP API alongside the frontend. That …

Run Hono on Azure: Container Apps, Functions, App Service and AKS compared

Jul 12, 2026 - 14 min read

Run Hono on Azure: Container Apps, Functions, App Service and AKS compared

Hono is often associated with Cloudflare Workers and for good reason. Its small routing model, Web Standard API surface and direct fetch …


BEN ABT

BEN ABT

Ben is Chief Engineering Officer and Principal Software Engineer at Medialesson.de, with a strong focus on .NET and Microsoft Azure. In his professional role, he designs and builds highly scalable platforms for hybrid and cloud-based scenarios. He also advises companies and C-levels on migrating to the sovereign cloud.

He runs myCSharp.de, the largest and most active German-speaking C# community forum, actively contributes to both his own and external open-source projects and regularly shares his knowledge through his blog, at community events, conferences and as a writer for technical magazines.

Let's Work Together

Looking for an experienced Platform Architect or Engineer for your next project? Whether it's cloud migration, platform modernization or building new solutions from scratch - I'm here to help you succeed.

New Platforms

Modernization

Training & Consulting

Contact Me
X Facebook LinkedIn WhatsApp
Back Next Blog Post

Open for work!

Contact Me
BEN ABT BEN ABT Software Engineer · Microsoft MVP Passionate about .NET, Azure, performance optimization and sustainable software engineering.

Medialesson GmbH Logo

Microsoft MVP Logo

Recent posts

View all
Production Rate Limiting in ASP.NET Core

Production Rate Limiting in ASP.NET Core

Nov 3, 2026
The Big Decimal Bugfix with .NET 11

The Big Decimal Bugfix with .NET 11

Nov 2, 2026
Distroless, Chiseled or Alpine: Choosing a .NET Base Image

Distroless, Chiseled or Alpine: Choosing a .NET Base Image

Oct 27, 2026
Stop Using nginx as Your Default Static File Server in Docker

Stop Using nginx as Your Default Static File Server in …

Oct 24, 2026
Containerizing .NET in 2026 Without a Dockerfile

Containerizing .NET in 2026 Without a Dockerfile

Oct 20, 2026

Tags

.NET (195) Csharp (99) ASP.NET Core (49) Azure (41) DevOps (17) Docker (15) Microsoft (14) EF Core (11) Microsoft MVP (11) Security (11) AI (10) GitHub (10) GitHub Copilot (10) ASP.NET (9) Synology (9) .NET 11 (8) Architecture (8) Raspberry (8) Azure Functions (7) Cloudflare (7)
benjamin-abt.com

About BEN

Benjamin Abt is a Software Engineer, Platform Architect and Microsoft MVP specializing in .NET, Azure, cloud architecture, performance optimization and sustainable software development.

Connect

  • Follow me on GitHub
  • Follow me on LinkedIn
  • Follow me on Mastodon
  • Microsoft MVP profile
  • Follow SchwabenCode on GitHub
  • Follow me on X

Quick Links

  • Portfolio
  • Blog
  • Workshops & Training
  • Publications
  • Get in Touch
  • Sitemap
Open for Work

© Copyright Benjamin Abt & SchwabenCode | 2011 - 2026

Content licensed under CC BY 4.0 unless otherwise noted.

  • Impressum
  • Privacy
  • Contact