如何扩展WCF WebHttp(REST)以支持ETags和条件获取? [英] How do I extend WCF WebHttp (REST) to support ETags and Conditional Gets?
问题描述
我有一个只读的WCF REST服务(所有 GET
的宝贝!)我想在我的每一个操作中添加ETag /有条件获得支持服务。
我的网站支持一对XML文件,而我的应用程序知道(并引发一个事件),当它们中的任何一个发生变化时。我不明白扩展点在哪里。如何在管道中添加这些标头,而不是一次一个地添加这些标头?
结果不那么糟糕。我使用了一个 IDispatchMessageInspector
,并将其应用于所有服务中的ServiceBehavior。
>公共类ConditionalGetMessageInspector:IDispatchMessageInspector $ b $我有点不舒服,请求被如何路由,但它似乎工作。 b {
private enum GetState {Modified,Unmodified}
私有字符串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 $ b $ .CheckConditionalRetrieve(ETag);
}
catch(WebFaultException)
{
instanceContext.Abort();
返回GetState.Unmodified;
}
// No-op
return GetState.Modified;
$ b $ 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;
}
}
}
I have a read-only WCF REST service (all GET
's baby!) I'd like to add ETag/Conditional get support to every single operation in my service.
Basically I'm interested in extending the technique in this article: http://blogs.msdn.com/b/endpoint/archive/2010/02/25/conditional-get-and-etag-support-in-wcf-webhttp-services.aspx
My site is backed by a couple of XML files, and my app knows (and raises an event) when any of them change. I don't understand where the extension points are though. How do I hook into the pipeline to add these headers for every call instead of one-at-a-time?
This turned out not to be so bad. I used an IDispatchMessageInspector
which I hooked into a ServiceBehavior that's applied to all my services. I'm a little uncomfortable with how the request gets routed but it seems to work.
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;
}
}
}
这篇关于如何扩展WCF WebHttp(REST)以支持ETags和条件获取?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!