Azure / azure-functions-host

The host/runtime that powers Azure Functions
https://functions.azure.com
MIT License
1.92k stars 442 forks source link

Can .NET5 isolated Azure Functions provide access to IWebhostEnvironment #7269

Open felinepc opened 3 years ago

felinepc commented 3 years ago

I have been using the .NET 5 Azure Functions with the help of the official doc. So DI in the .NET 5 Azure Functions behaves much like the regular ASP.NET Core's DI, which is nice because it allows me to share libraries with other ASP.NET Core web apps.

But one thing I'm trying to figure out is the access to IWebhostEnvironment, which is available by default in ASP.NET Core. I have libraries taking this as a dependency to define some behaviors based on checks such as IWebhostEnvironment.IsDevelopment.

What I found from my tests:

IWebhostEnvironment is not available for injection in Functions.

IHostEnvironment is available, but ignores the ASPNETCORE_ENVIRONMENT value set in local.settings.json, and therefore always returns Production.

Is there a way to make IWebhostEnvironment behave the same way in Azure Functions and ASP.NET Core? For now I have created my own IFunctionHostEnvironment interface that returns results based on the value of Environment.GetEnvironmentVariable("AZURE_FUNCTIONS_ENVIRONMENT"), which is automatically set to Development locally by Core Tools, and can be set manually in Azure portal.

v-anvari commented 3 years ago

@soninaren / @fabiocav , Please share your inputs here. Are there any alternative ways for the above scenario? can this be added as a feature request?

robntracts commented 3 years ago

Same issue except using the HostBuilderContext. context.HostingEnvironment is always Production. Used the same workaround.

varismii commented 1 year ago

I'm also missing this feature. Was trying to use code from our older Asp.Net Core based Functions and now I cannot inject IWebHostEnvironment to our custom telemetry class as in our codebase. Not happy to create a custom IFunctionHostEnvironment workaround, I'd prefer a built-in and supported way of accessing environment in .NET 6 based Functions V4. Please advice.