简体   繁体   中英

Azure Function startup's Configure not being called

I'm trying to create non-static functions in my Azure Function projet in .NET 5 (VS 2022) and the Startup Configure method is not being called.

Here's my start up class

[assembly: FunctionsStartup(typeof(AuthenticationGateway.Functions.Startup))]
namespace AuthenticationGateway.Functions
{
    class Startup : FunctionsStartup //public or not, still does not get called.
    {
        public override void Configure(IFunctionsHostBuilder builder)
        {
            
            //break point here never gets hit...
            
        }

        
    }


}

And here's the function in question:

namespace AuthenticationGateway.Functions
{
    public class CreationConnection
    {
        private AuthenticationGatewayContext Context { get; set; }

        public CreationConnection(AuthenticationGatewayContext context)
        {
            Context = context;
        }

        [Function("CreationConnection")]
        public HttpResponseData Run([HttpTrigger(AuthorizationLevel.Function, "get")] HttpRequestData req,
            FunctionContext executionContext)
        {            

            var response = req.CreateResponse(HttpStatusCode.OK);

            return response;
        }
    }
}

I've tried commenting all of the code in Configure just in case it was a problem with it, not working either. Also tried marking the startup class as public too, no go.

Here are the dependencies for the projet in question

在此处输入图像描述

They are not the default dependencies the projet has when creating an Azure Function projet but as I tried other solutions to fix the issue, it lead me to plug those in.

Here's what the console is saying when starting the project:

Azure Functions Core Tools Core Tools Version: 3.0.3904 Commit hash: c345f7140a8f968c5dbc621f8a8374d8e3234206 (64-bit) Function Runtime Version: 3.3.1.0

Anything I missed?

EDIT: I have reverted to the following dependencies as the previous ones made it so no functions would be found in the project.

在此处输入图像描述

On this page here it says those following dependencies have to be installed:

Microsoft.Azure.Functions.Extensions
Microsoft.NET.Sdk.Functions package version 1.0.28 or later
Microsoft.Extensions.DependencyInjection (currently, only version 3.x and earlier supported)

I have done so, except the last one because it is incompatible with .NET 5 it seems. Also, the project is now unbuildable:

error MSB4062: The "GenerateFunctionMetadata" task could not be loaded from the assembly

I have tried to reproduce the same issue which you got by following the below steps:

  1. Created the Azure Functions (Stack: .Net5-v3) in VS2022.
  2. Before adding Microsoft.Net.Sdk.functions to the project, it was built successfully.

在此处输入图像描述

  1. After adding Microsoft.Net.Sdk.functions to the project, it has run to the same issue MSB4062 error as below:

在此处输入图像描述

By referencing these SO Thread1 and Thread2 , removing Microsoft.Net.Sdk.functions will solve the compile issue.

Just in case you're running v4, Startup is not used.

Perform the dependency injection setup in Program.cs:

var host = new HostBuilder()
    .ConfigureFunctionsWorkerDefaults()
    .ConfigureServices(builder =>
    {
        builder.AddTransient<IUserService, UserService>();
        builder.AddTransient<ICompetitionService, CompetitionService>();
        builder.AddTransient<ICompetitionRepository, CompetitionRepository>();
    })
    .Build();

host.Run();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM