InvalidOperationException: Unable to resolve service for type 'Microsoft.Extensions.Caching.Distributed.IDistributedCache' while attempting to activate 'OpenIddict.OpenIddictInitializer'.
Startup.cs:
`
services.AddDbContext
{
options.UseSqlServer(connectionString, sql => sql.MigrationsAssembly("AppContext"));
options.UseOpenIddict
});
services.AddIdentity<ApplicationUser, ApplicationRole>()
.AddEntityFrameworkStores<AppContext>()
.AddDefaultTokenProviders();
services.Configure<IdentityOptions>(options =>
{
options.ClaimsIdentity.UserNameClaimType = OpenIdConnectConstants.Claims.Name;
options.ClaimsIdentity.UserIdClaimType = OpenIdConnectConstants.Claims.Subject;
options.ClaimsIdentity.RoleClaimType = OpenIdConnectConstants.Claims.Role;
});
services.AddOpenIddict<long>(options =>
{
options.AddEntityFrameworkCoreStores<AppContext>();
options.AddMvcBinders();
// Enable the token endpoint (required to use the password flow).
options.EnableTokenEndpoint("/connect/token");
options.AllowPasswordFlow();
options.DisableHttpsRequirement();
});
services.AddAuthentication()
.AddOAuthValidation();
services.AddScoped(typeof(IRepository<>), typeof(Repository<>));
`
.csproj:
<PackageReference Include="AspNet.Security.OAuth.Introspection" Version="2.0.0-rc1-final" />
<PackageReference Include="AspNet.Security.OAuth.Validation" Version="2.0.0-*" />
<PackageReference Include="GeoCoordinate.NetCore" Version="1.0.0.1" />
<PackageReference Include="IdentityModel" Version="2.15.0" />
<PackageReference Include="Microsoft.AspNetCore.Cors" Version="2.0.0" />
<PackageReference Include="Microsoft.AspNetCore.All" Version="2.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="2.0.1" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="2.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet" Version="2.0.0" />
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="2.0.0" />
<PackageReference Include="OpenIddict" Version="2.0.0-*" />
<PackageReference Include="OpenIddict.EntityFrameworkCore" Version="2.0.0-*" />
<PackageReference Include="OpenIddict.Mvc" Version="2.0.0-*" />
<PackageReference Include="SendGrid" Version="9.9.0" />
Updated from openictc v.2.0.0-- to v2.0.0-rc1-final. Same error.
The caching services are usually registered by MVC. Do you call services.AddMvc() somewhere? If you don't use MVC for the token endpoint action, you can manually register the required services by calling services.AddDistributedMemoryCache();.
Im using services.AddMvcCore()
By calling services.AddDistributedMemoryCache(); the problem fixed.
Thanks so much.
Most helpful comment
The caching services are usually registered by MVC. Do you call
services.AddMvc()somewhere? If you don't use MVC for the token endpoint action, you can manually register the required services by callingservices.AddDistributedMemoryCache();.