Make the ConfigureServices method asynchronous in Startup.cs

I need to add a couple of pending functions to ConfigureServices in Startup.cs, and I ran into a problem.

System.InvalidOperationException Could not find the required services. Please add all the necessary services by calling "IServiceCollection.AddMvc ()" inside the call to "IApplicationBuilder.ConfigureServices (...)" or "IApplicationBuilder.UseMvc (...)" in the application’s startup code.

As you can see from the code below, AddMvc and UseMvc are in the right place, however, I still get this error.

public async void ConfigureServices(IServiceCollection services) { ... await manager.Initialize(); var data = await manager.GetData(); ... services.AddMvc(); } public void Configure(IApplicationBuilder app, IHostingEnvironment env,ILoggerFactory loggerFactory) { ... app.UseMvc(); .... } 

Can I configure the ConfigigureServices async function?

+22
c # asp.net-core async-await
source share
1 answer

No, you can’t. This will lead to racial status.

Instead, consider making your operation synchronous or using .Wait() / .Result (depending on whether the async method returns data or not) until the asynchronous task completes.

+27
source share

All Articles