Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

KeyNotFoundException in Startup ConfigureServices AddMvc()

Since 30.05.2018 my ASP.NET Core code in Startup.cs

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

throw exception as below :

An error occurred while starting the application. KeyNotFoundException: The given key was not present in the dictionary.

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

csproj

<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" />

like image 763
radnarock Avatar asked Jun 01 '18 12:06

radnarock


2 Answers

I was suffering the same issues. The options to remedy this are outlined here https://github.com/aspnet/Home/issues/3132

For me I choose the option of adding the ApplicationPartManager before adding MVC, eg.

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

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

Source

like image 57
Scott Willis Avatar answered Nov 01 '22 14:11

Scott Willis


We were facing the same exact issue starting a few days ago as well.

We also noticed that both we and you were using AspNetCore 1.1 (a fairly old version).

On a hunch, we updated to the latest (2.1) and that resolved our issue.

like image 2
Zak Avatar answered Nov 01 '22 14:11

Zak