为什么在 .netcore 3.1 中找不到 WebApi 授权服务? [英] Why WebApi Authorization service could not be found in .netcore 3.1?

查看:18
本文介绍了为什么在 .netcore 3.1 中找不到 WebApi 授权服务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在 .netcore 3.1 中创建一个 webapi 项目,这是我在 program.cs 中的代码

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>();
            });
}

这是我的startup.cs:

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:

应用程序启动异常 System.InvalidOperationException: Unable找到所需的服务.请添加所有必需的服务在调用中调用IServiceCollection.AddAuthorization"应用程序启动代码中的ConfigureServices(...)".在Microsoft.AspNetCore.Builder.AuthorizationAppBuilderExtensions.VerifyServicesRegistered(IApplicationBuilder应用程序)在Microsoft.AspNetCore.Builder.AuthorizationAppBuilderExtensions.UseAuthorization(IApplicationBuilderapp) 在 project.Order.Startup.Configure(IApplicationBuilder app,IWebHostEnvironment 环境)在/82a891c3-0533-4937-9b8d-44d0c7405e9f/Roaming/project/project.Order/Startup.cs:System.RuntimeMethodHandle.InvokeMethod(对象目标,Object[] 参数、签名 sig、布尔构造函数、布尔值wrapExceptions) 在System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlagsinvokeAttr、Binder 绑定器、Object[] 参数、CultureInfo 文化)
在 Microsoft.AspNetCore.Hosting.ConfigureBuilder.Invoke(Object实例,IApplicationBuilder 构建器)在Microsoft.AspNetCore.Hosting.ConfigureBuilder.<>c__DisplayClass4_0.b__0(IApplicationBuilder建设者)在Microsoft.AspNetCore.Hosting.GenericWebHostBuilder.<>c__DisplayClass13_0.b__2(IApplicationBuilder应用程序)在Microsoft.AspNetCore.HostFilteringStartupFilter.<>c__DisplayClass0_0.b__0(IApplicationBuilder应用程序)在Microsoft.AspNetCore.Hosting.GenericWebHostService.StartAsync(CancellationToken取消令牌)未处理的异常.System.InvalidOperationException:无法找到所需的服务.请通过致电添加所有必需的服务'IServiceCollection.AddAuthorization' 内部调用应用程序启动代码中的'ConfigureServices(...)'

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.

推荐答案

public async Task ConfigureServices(IServiceCollection services)

ConfigureServies 方法不应返回 Task.这意味着 ASP.NET Core 框架无法找到您添加的这个特定的 ConfigureServices 方法.

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.

更新签名,如下:

public void ConfigureServices(IServiceCollection services)

通过此更改,将调用 ConfigureServices 方法,该方法将按预期添加服务.

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

这篇关于为什么在 .netcore 3.1 中找不到 WebApi 授权服务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆