c# - 缓存 WCF 服务的问题 - "AspNetCacheProfileAttribute can only be used with GET operations"

标签 c# asp.net wcf caching

我有一个 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")]

网络配置:

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

据我所知,这都是非常标准的东西,但可能我正在尝试以错误的方式执行此操作,因此我们将不胜感激。

非常感谢

最佳答案

您需要使用WebGet而不是WebInvoke

差异很微妙但很重要:

WebInvoke

Represents an attribute indicating that a service operation is logically an invoke operation and that it can be called by the WCF REST programming model.

WebGet

Represents an attribute indicating that a service operation is logically a retrieval operation and that it can be called by the WCF REST programming model.

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

The WebInvokeAttribute attribute is applied to a service operation … and associates the operation with a UriTemplate as well as an underlying transport verb that represents an invocation (for example, HTTP POST, PUT, or DELETE). … The WebInvokeAttribute determines what HTTP method that a service operation responds to. By default, all methods that have the WebInvokeAttribute applied respond to POST requests. The Method property allows you to specify a different HTTP method. If you want a service operation to respond to GET, use the WebGetAttribute instead.

关于c# - 缓存 WCF 服务的问题 - "AspNetCacheProfileAttribute can only be used with GET operations",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50833997/

相关文章:

c# - 无法从远程机器获取元数据,但本地没问题

c# - 使用 EF 数据库设置 WCF 服务的正确方法是什么?

wcf - WCF REST 的服务行为

c# - 使用 Pulumi 为 Azure 应用程序配置增加值(value)

c# - 使用点 (.) 或感叹号 (!) 拆分字符串,但忽略新字符串开头的空格

c# - 从十进制数据在 C# 中创建图像或图形?

c# - Controller 是否有可能以干净的方式返回基于参数的图像?

c# - asp.net webform 打印一致性

c# - 如何使这个 C# 函数可重用?

c# - "?"类型修饰符优先级 vs 逻辑与运算符 (&) vs address-of operator (&)