简体   繁体   中英

Why is the startup.cs file not being used when I start my Azure Function?

I am trying to run an azure function app project locally. I have breakpoints placed around my startup.cs file and I've noticed that they are never hit. As a result, my dependency injection does not work. I have been trying various combinations of nuget package versions, all to no avail. Can anyone provide me with advice as to how I can get the startup.cs file to be used?

This is a Class Library project that targets .Net Standard 2.0

I'm using Azure Functions Core Tools 3.0.2912 with Function Runtime version 3.0.14287

Here is my solution explorer

在此处输入图像描述

Here is the start of my startup function

[![enter image description here][2]][2]

There is an existing bug where the debugger attaches late, and early breakpoints, such as those in Startup.cs aren't able to be hit. Have a look here https://github.com/microsoft/vscode-azurefunctions/issues/1971

To test if this is your situation, add a thread sleep of ten seconds into your startup.cs, then place a breakpoint after that. Is it it?

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