【问题标题】:Issue with caching wcf service - "AspNetCacheProfileAttribute can only be used with GET operations"缓存 wcf 服务的问题 - “AspNetCacheProfileAttribute 只能用于 GET 操作”
【发布时间】:2018-11-22 20:16:33
【问题描述】:

我有一个 WCF 服务,它确实有效,但确实需要缓存响应。 我遵循了一些在线指导,但收到以下错误:

AspNetCacheProfileAttribute can only be used with GET operations.

我已经在 Postman 中检查了这一点,以完全确定我的客户端代码没有以错误的方式发出请求,结果相同。

界面如下:

public interface INavbar
{
    [OperationContract]
    [WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Bare)]        
    List<Navbar.simpleNav> getNavList();
}

该服务返回一个列表作为 JSON 对象,我尝试添加缓存的 2 个更改是将 AspNetCacheProfile 属性添加到类并将缓存配置文件详细信息添加到我的 web.config。

类的属性:

[AspNetCacheProfile("CacheFor180Seconds")]

web.config:

<caching>
  <outputCacheSettings>
    <outputCacheProfiles>
      <add name="CacheFor180Seconds" duration="180" varyByParam="none"/>
    </outputCacheProfiles>
  </outputCacheSettings>
</caching>

据我所知,这都是非常标准的东西,但可能是我试图以错误的方式做到这一点,因此感谢任何帮助。

非常感谢

【问题讨论】:

    标签: c# asp.net wcf caching


    【解决方案1】:

    您需要使用WebGet 而不是WebInvoke

    区别很微妙但很重要:

    WebInvoke

    表示一个属性,指示服务操作在逻辑上是一个invoke操作,并且它可以被 WCF REST 编程模型调用。

    网页获取

    表示一个属性,指示服务操作在逻辑上是检索操作,并且可以由 WCF REST 编程模型调用。

    再往下,在 WebInvoke 的备注部分:

    WebInvokeAttribute 属性应用于服务操作……并将操作与 UriTemplate 以及表示调用的底层传输动词相关联(例如,HTTP POST、PUT 或 DELETE)。 … WebInvokeAttribute 确定服务操作响应的 HTTP 方法。默认情况下,所有应用了 WebInvokeAttribute 的方法都会响应 POST 请求。 Method 属性允许您指定不同的 HTTP 方法。 如果您希望服务操作响应 GET,请改用 WebGetAttribute。

    【讨论】:

    • 非常感谢,感谢双方的解释
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-19
    • 1970-01-01
    • 2010-11-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多