abp运行时System.InvalidOperationException: Scheme already exists: Bearer
最近想把搭的abp框架从core3.1跟风到net5,添加了自定义的swagger后,页面报错500.30,如图
查看日志后,内容如下:
1 2021-02-04 12:19:39.436 +08:00 [FTL] Host terminated unexpectedly! 2 Autofac.Core.DependencyResolutionException: An exception was thrown while activating λ:Microsoft.Extensions.Hosting.IHostedService[] -> Microsoft.AspNetCore.Hosting.GenericWebHostService -> Microsoft.AspNetCore.Server.IIS.Core.IISHttpServer -> Microsoft.AspNetCore.Authentication.AuthenticationSchemeProvider. 3 ---> Autofac.Core.DependencyResolutionException: An exception was thrown while invoking the constructor 'Void .ctor(Microsoft.Extensions.Options.IOptions`1[Microsoft.AspNetCore.Authentication.AuthenticationOptions])' on type 'AuthenticationSchemeProvider'. 4 ---> System.InvalidOperationException: Scheme already exists: Bearer 5 at Microsoft.AspNetCore.Authentication.AuthenticationOptions.AddScheme(String name, Action`1 configureBuilder) 6 at Microsoft.AspNetCore.Authentication.AuthenticationBuilder.<>c__DisplayClass4_0`2.<AddSchemeHelper>b__0(AuthenticationOptions o) 7 at Microsoft.Extensions.Options.ConfigureNamedOptions`1.Configure(String name, TOptions options) 8 at Microsoft.Extensions.Options.OptionsFactory`1.Create(String name) 9 at Microsoft.Extensions.Options.OptionsManager`1.<>c__DisplayClass5_0.<Get>b__0() 10 at System.Lazy`1.ViaFactory(LazyThreadSafetyMode mode) 11 at System.Lazy`1.ExecutionAndPublication(LazyHelper executionAndPublication, Boolean useDefaultConstructor) 12 at System.Lazy`1.CreateValue() 13 at System.Lazy`1.get_Value() 14 at Microsoft.Extensions.Options.OptionsCache`1.GetOrAdd(String name, Func`1 createOptions) 15 at Microsoft.Extensions.Options.OptionsManager`1.Get(String name) 16 at Microsoft.Extensions.Options.OptionsManager`1.get_Value() 17 at Microsoft.AspNetCore.Authentication.AuthenticationSchemeProvider..ctor(IOptions`1 options, IDictionary`2 schemes) 18 at lambda_method947(Closure , Object[] ) 19 at Autofac.Core.Activators.Reflection.BoundConstructor.Instantiate() 20 --- End of inner exception stack trace --- 21 at Autofac.Core.Activators.Reflection.BoundConstructor.Instantiate() 22 at Autofac.Core.Activators.Reflection.ReflectionActivator.ActivateInstance(IComponentContext context, IEnumerable`1 parameters) 23 at Autofac.Core.Activators.Reflection.ReflectionActivator.<ConfigurePipeline>b__11_0(ResolveRequestContext ctxt, Action`1 next) 24 at Autofac.Core.Resolving.Middleware.DelegateMiddleware.Execute(ResolveRequestContext context, Action`1 next) 25 at Autofac.Core.Resolving.Pipeline.ResolvePipelineBuilder.<>c__DisplayClass14_0.<BuildPipeline>b__1(ResolveRequestContext ctxt) 26 at Autofac.Core.Resolving.Middleware.DisposalTrackingMiddleware.Execute(ResolveRequestContext context, Action`1 next) 27 at Autofac.Core.Resolving.Pipeline.ResolvePipelineBuilder.<>c__DisplayClass14_0.<BuildPipeline>b__1(ResolveRequestContext ctxt) 28 at Autofac.Core.Resolving.Middleware.ActivatorErrorHandlingMiddleware.Execute(ResolveRequestContext context, Action`1 next) 29 --- End of inner exception stack trace --- 30 at Autofac.Core.Resolving.Middleware.ActivatorErrorHandlingMiddleware.Execute(ResolveRequestContext context, Action`1 next) 31 at Autofac.Core.Resolving.Pipeline.ResolvePipelineBuilder.<>c__DisplayClass14_0.<BuildPipeline>b__1(ResolveRequestContext ctxt) 32 at Autofac.Core.Pipeline.ResolvePipeline.Invoke(ResolveRequestContext ctxt) 33 at Autofac.Core.Resolving.Middleware.RegistrationPipelineInvokeMiddleware.Execute(ResolveRequestContext context, Action`1 next) 34 at Autofac.Core.Resolving.Pipeline.ResolvePipelineBuilder.<>c__DisplayClass14_0.<BuildPipeline>b__1(ResolveRequestContext ctxt) 35 at Autofac.Core.Resolving.Middleware.SharingMiddleware.Execute(ResolveRequestContext context, Action`1 next) 36 at Autofac.Core.Resolving.Pipeline.ResolvePipelineBuilder.<>c__DisplayClass14_0.<BuildPipeline>b__1(ResolveRequestContext ctxt) 37 at Autofac.Core.Resolving.Pipeline.ResolvePipelineBuilder.<>c__DisplayClass14_0.<BuildPipeline>b__1(ResolveRequestContext ctxt) 38 at Autofac.Core.Resolving.Middleware.CircularDependencyDetectorMiddleware.Execute(ResolveRequestContext context, Action`1 next) 39 at Autofac.Core.Resolving.Pipeline.ResolvePipelineBuilder.<>c__DisplayClass14_0.<BuildPipeline>b__1(ResolveRequestContext ctxt) 40 at Autofac.Core.Pipeline.ResolvePipeline.Invoke(ResolveRequestContext ctxt) 41 at Autofac.Core.Resolving.ResolveOperation.GetOrCreateInstance(ISharingLifetimeScope currentOperationScope, ResolveRequest request) 42 at Autofac.Core.Resolving.ResolveOperation.ExecuteOperation(ResolveRequest request) 43 at Autofac.Core.Resolving.ResolveOperation.Execute(ResolveRequest request) 44 at Autofac.Core.Lifetime.LifetimeScope.ResolveComponent(ResolveRequest request) 45 at Autofac.ResolutionExtensions.TryResolveService(IComponentContext context, Service service, IEnumerable`1 parameters, Object& instance) 46 at Autofac.ResolutionExtensions.ResolveOptionalService(IComponentContext context, Service service, IEnumerable`1 parameters) 47 at Autofac.ResolutionExtensions.ResolveOptional(IComponentContext context, Type serviceType, IEnumerable`1 parameters) 48 at Autofac.ResolutionExtensions.ResolveOptional(IComponentContext context, Type serviceType) 49 at Autofac.Extensions.DependencyInjection.AutofacServiceProvider.GetService(Type serviceType) 50 at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetService[T](IServiceProvider provider) 51 at Microsoft.Extensions.Hosting.Internal.Host.StartAsync(CancellationToken cancellationToken) 52 at Microsoft.Extensions.Hosting.HostingAbstractionsHostExtensions.RunAsync(IHost host, CancellationToken token) 53 at Microsoft.Extensions.Hosting.HostingAbstractionsHostExtensions.RunAsync(IHost host, CancellationToken token) 54 at Microsoft.Extensions.Hosting.HostingAbstractionsHostExtensions.Run(IHost host) 55 at Pro215.Web.Program.Main(String[] args) in D:\abp_Project3\Pro215\src\Pro215.Web\Program.cs:line 31
老外也遇到了这个问题,链接:https://stackoverflow.com/questions/51775330/invalidoperationexception-scheme-already-exists-bearer/51793639
我的情况与他不一样,经过查找web项目module中,ConfigureServices方法里,调用ConfigureAuthentication时,添加了认证,我自己自定义又添加了一遍,导致出现Scheme already exists: Bearer错误,因为已经存在还添加,不报错才怪。