我有一个 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>
据我所知,这都是非常标准的东西,但可能我正在尝试以错误的方式执行此操作,因此我们将不胜感激。
非常感谢
最佳答案
差异很微妙但很重要:
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/