Azure / azure-functions-dotnet-worker

Azure Functions out-of-process .NET language worker
MIT License
418 stars 181 forks source link

Trigger not working when ConfigureFunctionsWorkerDefaults() is registered in Asp.Net Core Integration app #1947

Open surgupta-msft opened 11 months ago

surgupta-msft commented 11 months ago

Trigger not working as expected when ConfigureFunctionsWorkerDefaults() is registered in Asp.Net Core Integration app. Also, evaluate if we need analyzer for this scenario.

Repro steps and behavior seen

  1. Register ConfigureFunctionsWorkerDefaults() in Asp.NetCore app. Sample for reference
var host = new HostBuilder()
    .ConfigureFunctionsWorkerDefaults()
    .ConfigureFunctionsWebApplication()
    .Build();
  1. On invoking SimpletHttpTrigger a few times, noticed either of below behaviors -
    • Trigger worked without any error but did not show the output log line: Welcome to Azure Functions!
    • Trigger did not work and failed with Timeout exception.

Expected behavior

Trigger should work successfully emitting the expected output log: Welcome to Azure Functions!

Additional notes from offline discussion

ktriple commented 7 months ago

I also encounter issues with this while running .NET 8 isolated. After some testing I found:

Case 1: Endless loop until timeout

 var host = new HostBuilder()
     .ConfigureFunctionsWebApplication()
     .ConfigureFunctionsWorkerDefaults()

Case 2: 200 OK, but without response body

 var host = new HostBuilder()
     .ConfigureFunctionsWorkerDefaults()
     .ConfigureFunctionsWebApplication() 

Using either of them solo fixes it and therefor I'm not sure if they are supposed to be used together.. but it's a scenario we currently have.

XHunter74 commented 7 months ago

Also, I have the same behavior.

LaurentBonnot commented 7 months ago

Question, is it possible to benefits of the ASP.NET Core integration and also to be able to inject custom middleware? For example builder.UseMiddleware<CustomMiddleware>(). is there another alternative?

XHunter74 commented 7 months ago

Yes, it is possible, as it turns out, you just need to:

.ConfigureFunctionsWebApplication(workerApplication => { workerApplication.UseWhen< CorsHttpHeaderMiddleware >(context => { return context.FunctionDefinition.InputBindings.Values .First(a => a.Type.EndsWith("Trigger")).Type == "httpTrigger"; }); })

wouter-b commented 6 months ago

Because ConfigureFunctionsWebApplication allready calls ConfigureFunctionsWorkerDefaults

https://github.com/Azure/azure-functions-dotnet-worker/blob/main/extensions/Worker.Extensions.Http.AspNetCore/src/FunctionsHostBuilderExtensions.cs#L52

Rujith commented 6 months ago

I also encounter issues with this while running .NET 8 isolated. After some testing I found:

Case 1: Endless loop until timeout

 var host = new HostBuilder()
     .ConfigureFunctionsWebApplication()
     .ConfigureFunctionsWorkerDefaults()

Case 2: 200 OK, but without response body

 var host = new HostBuilder()
     .ConfigureFunctionsWorkerDefaults()
     .ConfigureFunctionsWebApplication() 

Using either of them solo fixes it and therefor I'm not sure if they are supposed to be used together.. but it's a scenario we currently have.

I think you are only supposed to use either one of them, not both. Have a look the docs here

mattchenderson commented 6 months ago

Hi all - yes, confirming that we generally expect only one of these to be used, and I'd default to using ConfigureFunctionsWebApplication to get the performance benefits that come with ASP.NET Core integration, unless you are targeting .NET Framework 4.8.

christianroll commented 2 weeks ago

Any news on this issue?? I realized that if I have a http trigger and a servicebus trigger in the same function app it get conflicts by just using .ConfigureFunctionsWebApplication(), only the http trigger works and for the servicebus trigger I get an exception of: An exception was thrown by the invocation. [2024-09-03T12:44:27.345Z] Exception: System.InvalidOperationException: Unable to resolve service for type 'Microsoft.Azure.ServiceBus.Grpc.Settlement+SettlementClient' while attempting to activate 'Microsoft.Azure.Functions.Worker.ServiceBusMessageActionsConverter'

If I try to use both together the servicebus trigger works but for the http trigger I get same result of ktriple comment:

I also encounter issues with this while running .NET 8 isolated. After some testing I found:

Case 1: Endless loop until timeout

 var host = new HostBuilder()
     .ConfigureFunctionsWebApplication()
     .ConfigureFunctionsWorkerDefaults()

Case 2: 200 OK, but without response body

 var host = new HostBuilder()
     .ConfigureFunctionsWorkerDefaults()
     .ConfigureFunctionsWebApplication() 

Using either of them solo fixes it and therefor I'm not sure if they are supposed to be used together.. but it's a scenario we currently have.