GFlisch / Arc4u.Guidance.Doc

Other
5 stars 1 forks source link

The ICacheContext service implementation is added twice #236

Open vvdb-architecture opened 2 months ago

vvdb-architecture commented 2 months ago

When the Guidance generates services (including Yarp), it adds the following statement in Program.cs:

    services.AddCacheContext(Configuration);

This statement does a services.TryAddSingleton<ICacheContext, CacheContext>();.

But the following is also added: in appsettings.json:

    "RegisterTypes": [
       ...
      "Arc4u.Caching.CacheContext, Arc4u.Standard.Caching",
      ...
    ]

Since the ComponentModelContainer is built after the execution of the AddCacheContext and the registration process in AttributeInspector registers "blind", the net effect is that that ICacheContext is registered twice!

It "works" because when you inject the interface, the "last" instance will be obtained in the current implementation of Microsoft's IServiceProvider. But I don't know if this is an official behavior.

The solution is to remove the definition in appsettings.json. This issues may apply to other caches (like Arc4u.Caching.Memory.MemoryCache) which is registered selectively in AddCacheContext but is also mentioned in RegisterTypes as "Arc4u.Caching.Memory.MemoryCache, Arc4u.Standard.Caching.Memory".

This only applies to back-ends, not front-ends!