如何扩展WCF WebHttp(REST)以支持ETAG和条件GET?
我有一个只读的WCF REST服务(all如何扩展WCF WebHttp(REST)以支持ETAG和条件GET?,wcf,rest,header,webhttp,Wcf,Rest,Header,Webhttp,我有一个只读的WCF REST服务(allGET's baby!),我想为我服务中的每个操作添加ETag/Conditional GET支持 基本上,我对扩展本文中的技术感兴趣: 我的站点由几个XML文件支持,我的应用程序知道(并引发一个事件)它们中的任何一个发生了变化。但我不明白扩展点在哪里。如何连接到管道中,为每个调用而不是一次一个地添加这些头?这就是HttpOperationHandler在新的WCF Web API中的用途 我不知道是否有任何简单的方法可以用WebHttpBinding
GET
's baby!),我想为我服务中的每个操作添加ETag/Conditional GET支持
基本上,我对扩展本文中的技术感兴趣:
我的站点由几个XML文件支持,我的应用程序知道(并引发一个事件)它们中的任何一个发生了变化。但我不明白扩展点在哪里。如何连接到管道中,为每个调用而不是一次一个地添加这些头?这就是HttpOperationHandler在新的WCF Web API中的用途
我不知道是否有任何简单的方法可以用WebHttpBinding来实现它。事实证明这并不是那么糟糕。我使用了一个
IDispatchMessageInspector
,我将它连接到一个应用于我所有服务的ServiceBehavior中。我对请求如何被路由感到有点不舒服,但它似乎起了作用
public class ConditionalGetMessageInspector : IDispatchMessageInspector
{
private enum GetState { Modified, Unmodified }
private string ETag {
get { return XmlDataLoader.LastUpdatedTicks.ToString(); }
}
private DateTime LastModified {
get { return new DateTime(XmlDataLoader.LastUpdatedTicks);}
}
public object AfterReceiveRequest(ref Message request,
IClientChannel channel, InstanceContext instanceContext)
{
try
{
WebOperationContext.Current.IncomingRequest
.CheckConditionalRetrieve(ETag);
}
catch (WebFaultException)
{
instanceContext.Abort();
return GetState.Unmodified;
}
// No-op
return GetState.Modified;
}
public void BeforeSendReply(ref Message reply, object correlationState)
{
if ((GetState)correlationState == GetState.Unmodified)
{
WebOperationContext.Current.OutgoingResponse.StatusCode =
HttpStatusCode.NotModified;
WebOperationContext.Current.OutgoingResponse.SuppressEntityBody =
true;
}
else
{
WebOperationContext.Current.OutgoingResponse.SetETag(ETag);
WebOperationContext.Current.OutgoingResponse.LastModified =
LastModified;
}
}
}