Dependency resolution error when using the CacheOutput attribute in a WebApi2 project

In my ASP.NET WebApi2 project, I decided to use the Strathweb.CacheOutput.WebApi2 package to provide caching and electronic tag functionality. However, after entering the CacheOutput attribute, I started getting an error:

Microsoft.Practices.Unity.ResolutionFailedException: Resolution of the dependency failed, type = "WebApi.OutputCache.Core.Cache.IApiOutputCache", name = "(none)".
Exception occurred while: while resolving.
Exception is: InvalidOperationException - The current type, WebApi.OutputCache.Core.Cache.IApiOutputCache, is an interface and cannot be constructed. Are you missing a type mapping?

Obviously, the internal container DI container cannot be allowed by the cache provider.

How to use the attribute:

[HttpGet, Route("")]
[CacheOutput(ClientTimeSpan = 60 * 60 * 4, ServerTimeSpan = 60 * 60 * 4)]
public HttpResponseMessage Get(
    string products,
    int? startYear = null,
    int? endYear = null,
    int? drillDownYear = null,
    int? drilldownMonth = null,
     string callback = "")
    {
       ...
    }

Just to point out, I also use Unity in my application as a DI container.

+4
source share
1 answer

The following logic is used in lib to find the cache provider (the first match is used):

  • Func<IApiOutputCache> Properties HttpConfiguration
  • IDependencyResolver
  • new MemoryCacheDefault()

IDependencyResolver null, . , Unity Ex.

, DI:) new MemoryCacheDefault() IApiOutputCache Unity.

+3

Source: https://habr.com/ru/post/1541558/


All Articles