question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Guidance for integrating with Azure Functions 3.0

See original GitHub issue

ADMINISTRATOR’s NOTE: There’s an Azure Functions Integration page. This demonstrates how to integrate Simple Injector with Azure Functions v3.

A lot has happened since many of the Azure Functions related issues here. Azure Functions are no longer required to be static classes with static method(s) and DI can be customized (using configuration) by extending FunctionsStartup. Therefore I’m looking to integrate Simple Injector with MS.DI with cross wiring. My current use case is to get access to the current user (from HttpContext) in an application component deeper in the object graph (so propagating it down the stack as a method parameter is not very pretty). I have created an IUserService abstraction with an implementation like this:

public class UserService : IUserService
{
    private readonly IHttpContextAccessor httpContextAccessor;

    public UserService(IHttpContextAccessor httpContextAccessor)
    {
        this.httpContextAccessor = httpContextAccessor;
    }

    public User GetCurrentUser()
    {
        var claimsPrincipal = httpContextAccessor.HttpContext?.User;

        var userName = claimsPrincipal?.Claims.FirstOrDefault(x => x.Type == ClaimTypes.Upn)?.Value;
        var name = claimsPrincipal?.Claims.FirstOrDefault(x => x.Type == ClaimTypes.Name)?.Value;

        return new User(name, userName);
    }
}

In FunctionsStartup I’m adding HttpContextAccessor and SimpleInjector:

public override void Configure(IFunctionsHostBuilder builder)
{
    var configuration = builder.GetContext().Configuration;
    var serviceCollection = builder.Services;
    var container = new Container();
    
    serviceCollection
        .AddHttpContextAccessor()
        .AddSimpleInjector(container);
    serviceCollection
        .BuildServiceProvider(true)
        .UseSimpleInjector(container);
    
    // Handles registrations of application components
    Setup(container, configuration);
}

The issue is that when firing up the Azure Function locally I get the following error:

A host error has occurred during startup operation ‘69997a2d-8526-49a2-8a76-df5e0c0a5f45’. func: Invalid host services. Microsoft.Azure.WebJobs.Script.WebHost: The following service registrations did not match the expected services: [Invalid] ServiceType: Microsoft.Extensions.Hosting.IHostedService, Lifetime: Singleton, ImplementationFactory: System.Func`2[System.IServiceProvider,Microsoft.Extensions.Hosting.IHostedService]. Value cannot be null. (Parameter ‘provider’)

As far as I can see this error comes from the following code from SimpleInjector.Integration.ServiceCollection v.5.2.0:

private static void HookAspNetCoreHostHostedServiceServiceProviderInitialization(
      SimpleInjectorAddOptions options)
{
  options.Services.AddSingleton<IHostedService>(
    (Func<IServiceProvider, IHostedService>) (provider =>
  {
    options.SetServiceProviderIfNull(provider);
    return (IHostedService)new SimpleInjectorServiceCollectionExtensions
        .NullSimpleInjectorHostedService();
  }));
}

I’m not sure if the problem is that the provider I create in Configure is not the same provider as the one actually used by the rest of the Azure Functions runtime?

Anyway, I’m looking for a possibility of integrating Simple Injector with Azure Functions or updated best practices regarding this topic.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:1
  • Comments:19

github_iconTop GitHub Comments

2reactions
RyanMarcottecommented, Mar 14, 2021

Correct @CasperWSchmidt , NET 5 only provides the out-of-process option while NET 6 will (according to their roadmap) support both in-process and out-of-process options. It may be useful for the Azure Function integration page to also link to the HostBuilder integration page for setting up the integration for the out-of-process option.

1reaction
CasperWSchmidtcommented, Mar 14, 2021

@RyanMarcotte this is only true for the out-of-process version of Azure Functions. It is similar to pretty much any other .Net Core program using HostBuilder. For .Net 6 (which will be LTS) it will be supported (confirmed by MS somewhere) as in-process like .Net Core 3.1 (probably as AZ v4) and for that I believe we will still have to use what we have above.

For out-of-process Azure Functions (nice addition by the way) we can do what ever we like, it’s just a console application.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Guidance for developing Azure Functions
Learn the Azure Functions concepts and techniques that you need to develop functions in Azure, across all programming languages and ...
Read more >
Introduction to Azure Functions 3.0
Here's the introduction article to Azure Functions 3.0 which brought effective updates based on runtime version and support in migrating.
Read more >
Azure Functions v3 migration guide from .NET Core 3.1 to ...
This guide will help you migrate from a project with Azure Functions v3 on .NET Core 3.1 to the new Azure Functions Isolated...
Read more >
Getting Started with Azure Functions Tutorial [Example ...
If you're been meaning to learn Azure Functions, you're in luck. Learn how to get started in this Azure Functions tutorial.
Read more >
Azure Functions Integration
WARNING: This integration guide is an evolving document. It gets updated any time we find better ways to integrate Simple Injector with Azure...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found