gpt4 book ai didi

c# - 启动 ConfigureServices AddMvc() 中的 KeyNotFoundException

转载 作者:可可西里 更新时间:2023-11-01 07:51:12 25 4
gpt4 key购买 nike

自 2018 年 5 月 30 日起,我在 Startup.cs 中的 ASP.NET Core 代码

public IServiceProvider ConfigureServices(IServiceCollection services)
{
// Add services to the collection.
services.AddMvc();
}

抛出如下异常:

启动应用程序时发生错误。KeyNotFoundException:字典中不存在给定的键。

KeyNotFoundException: The given key was not present in the dictionary. System.Collections.Generic.Dictionary.get_Item(TKey key) Microsoft.AspNetCore.Mvc.Internal.DefaultAssemblyPartDiscoveryProvider+CandidateResolver.ComputeClassification(string dependency) Microsoft.AspNetCore.Mvc.Internal.DefaultAssemblyPartDiscoveryProvider+CandidateResolver.ComputeClassification(string dependency) Microsoft.AspNetCore.Mvc.Internal.DefaultAssemblyPartDiscoveryProvider+CandidateResolver.ComputeClassification(string dependency) Microsoft.AspNetCore.Mvc.Internal.DefaultAssemblyPartDiscoveryProvider+CandidateResolver.ComputeClassification(string dependency) Microsoft.AspNetCore.Mvc.Internal.DefaultAssemblyPartDiscoveryProvider+CandidateResolver+d__4.MoveNext() System.Linq.Enumerable+d__17.MoveNext() System.Linq.Enumerable+WhereSelectEnumerableIterator.MoveNext() Microsoft.Extensions.DependencyInjection.MvcCoreServiceCollectionExtensions.GetApplicationPartManager(IServiceCollection services) Microsoft.Extensions.DependencyInjection.MvcCoreServiceCollectionExtensions.AddMvcCore(IServiceCollection services) Microsoft.Extensions.DependencyInjection.MvcServiceCollectionExtensions.AddMvc(IServiceCollection services) MyWebApiProject.Startup.ConfigureServices(IServiceCollection services) in Startup.cs System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() Microsoft.AspNetCore.Hosting.ConventionBasedStartup.ConfigureServices(IServiceCollection services) Microsoft.AspNetCore.Hosting.Internal.WebHost.EnsureApplicationServices() Microsoft.AspNetCore.Hosting.Internal.WebHost.BuildApplication()

工程项目

<TargetFramework>net461</TargetFramework>

<ItemGroup>
<PackageReference Include="Autofac" Version="4.6.2" />
<PackageReference Include="Autofac.Extensions.DependencyInjection" Version="4.1.0" />
<PackageReference Include="EntityFramework" Version="6.2.0" />
<PackageReference Include="Microsoft.ApplicationInsights.AspNetCore" Version="2.0.1" />
<PackageReference Include="Microsoft.AspNetCore" Version="1.1.2" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="1.1.2" />
<PackageReference Include="Microsoft.AspNetCore.Mvc" Version="1.1.3" />
<PackageReference Include="Microsoft.AspNetCore.SignalR.Server" Version="0.2.0-preview2-22683" />
<PackageReference Include="Microsoft.AspNetCore.WebSockets" Version="1.0.2" />
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="1.1.2" />
<PackageReference Include="Microsoft.IdentityModel.Tokens" Version="5.1.5" />
<PackageReference Include="Microsoft.VisualStudio.SlowCheetah" Version="3.0.61" />
<PackageReference Include="NLog.Web.AspNetCore" Version="4.4.1" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="1.0.0" />
<PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="5.1.5" />
<PackageReference Include="System.Net.Http" Version="4.3.3" />

最佳答案

我遇到了同样的问题。此处概述了解决此问题的选项 https://github.com/aspnet/Home/issues/3132

对我来说,我选择在添加 MVC 之前添加 ApplicationPartManager 的选项,例如。

var manager = new ApplicationPartManager();
manager.ApplicationParts.Add(new AssemblyPart(typeof(Startup).Assembly));

services.AddSingleton(manager);
services.AddMvc();

Source

关于c# - 启动 ConfigureServices AddMvc() 中的 KeyNotFoundException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50643072/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com