4
votes

I am creating a webapi project in .netcore 3.1 and this is my code in program.cs

public class Program
{
    public static void Main(string[] args)
    {
        CreateHostBuilder(args).Build().Run();
    }

    public static IHostBuilder CreateHostBuilder(string[] args) =>
        Host.CreateDefaultBuilder(args)
            .ConfigureWebHostDefaults(webBuilder =>
            {
                webBuilder.UseStartup<Startup>();
            });
}

and this is my startup.cs:

public class Startup
{
    public Startup(IConfiguration configuration)
    {
        Configuration = configuration;
    }

    public IConfiguration Configuration { get; }

    // This method gets called by the runtime. Use this method to add services to the container.
    public async Task ConfigureServices(IServiceCollection services)
    {
        services.AddAuthorization();

        services.RegisterEasyNetQ("host=localhost;username=admin;password=admin");
        services.AddSingleton(typeof(CreateOrderHandler));
        services.AddControllers();
    }

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }

        app.UseHttpsRedirection();

        app.UseRouting();

        app.UseAuthorization();
        app.UseEndpoints(endpoints => { endpoints.MapControllers(); });
    }
}

but after runing the project I am getting this Exception:

Application startup exception System.InvalidOperationException: Unable to find the required services. Please add all the required services by calling 'IServiceCollection.AddAuthorization' inside the call to 'ConfigureServices(...)' in the application startup code. at Microsoft.AspNetCore.Builder.AuthorizationAppBuilderExtensions.VerifyServicesRegistered(IApplicationBuilder app) at Microsoft.AspNetCore.Builder.AuthorizationAppBuilderExtensions.UseAuthorization(IApplicationBuilder app) at project.Order.Startup.Configure(IApplicationBuilder app, IWebHostEnvironment env) in /82a891c3-0533-4937-9b8d-44d0c7405e9f/Roaming/project/project.Order/Startup.cs:line 50 at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor, Boolean wrapExceptions) at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
at Microsoft.AspNetCore.Hosting.ConfigureBuilder.Invoke(Object instance, IApplicationBuilder builder) at Microsoft.AspNetCore.Hosting.ConfigureBuilder.<>c__DisplayClass4_0.b__0(IApplicationBuilder builder) at Microsoft.AspNetCore.Hosting.GenericWebHostBuilder.<>c__DisplayClass13_0.b__2(IApplicationBuilder app) at Microsoft.AspNetCore.HostFilteringStartupFilter.<>c__DisplayClass0_0.b__0(IApplicationBuilder app) at Microsoft.AspNetCore.Hosting.GenericWebHostService.StartAsync(CancellationToken cancellationToken) Unhandled exception. System.InvalidOperationException: Unable to find the required services. Please add all the required services by calling 'IServiceCollection.AddAuthorization' inside the call to 'ConfigureServices(...)' in the application startup code

I search a lot but cannot find any hint.

2
Have you tried by adding app.UseAuthentication(); before app.UseAuthorization();?TanvirArjel
Have you plaid with the order of things? I think ythere is something about * First adding controllers and * Also adding routing AFTER them. The error message definitely is bad.TomTom
yes, I examined all possible order@TomTomNavid_pdp11
yes I Try it.... @TanvirArjelNavid_pdp11

2 Answers

2
votes
public async Task ConfigureServices(IServiceCollection services)

The ConfigureServies method is not expected to return a Task. This means the ASP.NET Core framework is unable to find this particular ConfigureServices method that you've added.

Update the signature, as follows:

public void ConfigureServices(IServiceCollection services)

With this change, the ConfigureServices method will be called, which will add the services, as intended.

1
votes
void ConfigureServices(IServiceCollection services) {

    //...

    services.AddAuthorization();

    //...
}

Please add all the required services by calling 'IServiceCollection.AddAuthorization' inside the call to 'ConfigureServices(...)