chinh nguyen van
chinh nguyen van

Reputation: 889

Dependency Injection for AzureFunction ServiceBus based on message information

I am working on AzureFunction ServiceBus v3 which handle multi-tenant message. In message I will have TenantId and I will need to register DependencyInjection per message base on this TenantId. What I tried so far:

  1. At StartUp, I stored the IServiceCollection as static variable

  2. Retrieve the TenantId from the serialized message in Function'd Run method

  3. Update IServiceCollection based on above TenantId and retrieve the Service

    _serviceCollection.AddTransient<ITenantIdResolver>(ctx => { return new CompanyResolver{TenantId=tenantId}; }); var service = _serviceCollection.BuildServiceProvider().GetService<T>();

But it throw exception: Unable to resolve service for type 'Microsoft.Azure.WebJobs.Script.IEnvironment' while attempting to activate 'Microsoft.Azure.WebJobs.Script.Configuration.ScriptHostOptionsSetup' I do some research and look like it was because I used IHttpClientFactory. How can I fix this?

Or even better if there is a way to retrieve the message in StartUp, so I can inject the tenantId properly? Like serviceCollection.AddTransient<ITenantIdResolver>(ctx => { var tenantId = GetServicebusMessage().TenantId; return new CompanyResolver { TenantId=tenantId }; }

Upvotes: 1

Views: 1373

Answers (2)

Machiel Visser
Machiel Visser

Reputation: 925

I think that would be the wrong order of things. Dependency injection should be setup before the message is being processed.

A resolver could be a solution. Register the resolver with the dependency injection container, and let the function be dependent on the resolver. Based on the message you get the right instance from the resolver. In this article it is explained better under "IoC Container Registration #3 – Resolver + Factory Method Pattern": https://techcommunity.microsoft.com/t5/apps-on-azure/dependency-injection-on-azure-functions-5-ways-selecting/ba-p/1502394

Upvotes: 2

Bassam Gamal
Bassam Gamal

Reputation: 701

According to Dependency injection for azure functions, it's not possible to use the services at an early stage.

My suggestion is to change the architecture to "Durable Orchestrations" so you call an ActivityTrigger function from the orchestrator which gets back list of tenants then you fire other ActivityTriggers that will handle them.

Upvotes: 1

Related Questions