How to setup DI in custom code with Azure Logic App?

Baskaran Samuel Ganesan 5 Reputation points
2024-11-22T03:02:34.7366667+00:00

https://stackoverflow.com/questions/78919971/use-dependency-injection-in-azure-logic-app-custom-code

only Logger is registered and ready to use.

This does not work for custom code with logic app

var host = new HostBuilder()
			.ConfigureFunctionsWorkerDefaults()
			.ConfigureServices((hostContext, services) =>
			{
				services.AddLogging();

                services.AddScoped<ISomeService, SomeService>();
                
			}).Build();
			
await host.RunAsync();
Azure Logic Apps
Azure Logic Apps
An Azure service that automates the access and use of data across clouds without writing code.
{count} votes

1 answer

Sort by: Most helpful
  1. Sonny Gillissen 3,996 Reputation points Volunteer Moderator
    2025-05-31T19:54:59.2733333+00:00

    Hi Baskaran Samuel Ganesan

    Thanks for reaching out on Microsoft Q&A!

    While you can use HostBuilder and register services like ILogger, custom services (e.g., ISomeService) are not automatically injected into your custom code actions in Logic Apps. This is because:

    • The Custom Code runtime in Logic Apps is designed for lightweight, inline logic, not full-blown ASP.NET Core-style DI setups.
    • The Program.cs and HostBuilder pattern is not fully supported in the context of Logic Apps' custom code execution

    If your logic requires full DI, consider moving the logic to an Azure Function (which supports DI fully), and call it from your Logic App via HTTP. If your logic is simple, you can also use the "Execute JavaScript Code" action in Logic Apps Standard to run inline code.

    Please click ‘Accept answer’ if you think my answer is helpful. Feel free to drop additional queries in the comments below!

    Kind regards,

    Sonny

    1 person found this answer helpful.
    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.