Guidance for integrating with Azure Functions 3.0
See original GitHub issueADMINISTRATOR’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:
- Created 3 years ago
- Reactions:1
- Comments:19
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.@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.