事实证明这并没有那么糟糕。我使用了一个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;
}
}
}