首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在CacheOutput项目中使用WebApi2属性时,依赖项的解析失败

在CacheOutput项目中使用WebApi2属性时,依赖项的解析失败
EN

Stack Overflow用户
提问于 2014-05-22 09:12:52
回答 1查看 1.5K关注 0票数 5

在我的ASP.NET WebApi2项目中,我决定使用Strathweb.CacheOutput.WebApi2包提供缓存和电子标签功能。但是,在放置CacheOutput属性后,我开始得到一个错误:

代码语言:javascript
复制
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?

显然,包的内部DI容器无法解析缓存提供程序。

我使用属性的方式:

代码语言:javascript
复制
[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 = "")
    {
       ...
    }

请注意,我还在我的应用程序中使用了Unity作为DI容器。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-05-22 09:20:04

库使用以下逻辑查找缓存提供程序(使用了第一个匹配):

  1. 在您的Func<IApiOutputCache>Properties中检查一个HttpConfiguration
  2. 签入注册的IDependencyResolver
  3. 使用new MemoryCacheDefault()

通常,当未找到注册时,IDependencyResolver将返回null - an这个示例看起来您的UnityImplement正抛出一个Ex。

您可以通过切换到一个更好的DI :)来缓解这种情况,或者只在您的new MemoryCacheDefault()容器中将单个IApiOutputCache注册为IApiOutputCache

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23802478

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档