Openiddict-core: Question about caching

Created on 14 Jan 2017  路  4Comments  路  Source: openiddict/openiddict-core

Hello, I'm working on a project using OpenIddict and I'm trying to figure out how the caching works.

I understand that OpenIddict uses Microsoft.Extensions.Caching.Abstractions in order to let the final project determine which cache to use (Redis, SQLServer or Memory) and inject the respective dependency.

However, I'm going through the sample (Mvc.Server) source code and I cannot find a reference to Microsoft.Extensions.Caching.Memory (or, for that matter, Microsoft.Extensions.Caching.Redis or Microsoft.Extensions.Caching.SqlServer).

I noticed that UseOpenIddict (in OpenIddictExtensions.cs) checks if there is a distributed cache registered in the options and, if not (the default), calls ApplicationServices.GetService<IDistributedCache>();

(My assumption is that it uses Microsoft.Extensions.Caching.Memory but I couldn't find neither a reference to this package in project.json nor a call to services.AddCaching() in ConfigureServices.)

So my question is: where in the Mvc.Server sample is the IDistributedCaching dependency injected? How does the framework knows the project is using Memory instead of Redis or SqlServer?

Thanks in advance and please let me know if you need any additional information.

question

All 4 comments

So my question is: where in the Mvc.Server sample is the IDistributedCaching dependency injected?

It's indirectly injected by MVC: https://github.com/aspnet/Mvc/blob/dev/src/Microsoft.AspNetCore.Mvc.TagHelpers/DependencyInjection/TagHelperExtensions.cs#L34-L35.

@PinpointTownes thanks for the blazingly fast response! If it's not too much to ask, how does the framework differentiate between this indirect injection and another, explicitly injected in the project?

For example, I understand that if I reference Microsoft.Extensions.Caching.SqlServer in my project.json and add services.AddSingleton<IDistributedCache, SqlServerCache>() to ConfigureServices, OpenIddict will use SqlServer for caching in my project.

How does the framework know when to use Memory (for the TagHelpers) and when to use SqlServer (for OpenIddict), if both dependencies were injected for IDistributedCaching?

Once again, thanks so much for all your help regarding OpenIddict and AspNet in general. Best regards!

How does the framework know when to use Memory (for the TagHelpers) and when to use SqlServer (for OpenIddict), if both dependencies were injected for IDistributedCaching?

Easy: the last registration always wins. Note that MemoryDistributedCache is usually registered using TryAdd, so it doesn't replace previous IDistributedCache registrations.

Thanks!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

AlphaCreativeDev picture AlphaCreativeDev  路  3Comments

mattwcole picture mattwcole  路  3Comments

ivanmariychuk picture ivanmariychuk  路  6Comments

matthewwren picture matthewwren  路  3Comments

masakura picture masakura  路  3Comments