开发者

Implementing a client-side cache using WCF, REST and standard HTTP headers

I have a Perl based REST service and I'm using C# and WCF to make a client to talk to the service. I have a few expensive calls开发者_JS百科 and would like to construct a caching system. I need the ability to check and see if newer versions of the cached data exist on the server. I had the idea to use the standard "If-Modified-Since" request header and "304 Not Modified" response status code, however I'm having trouble catching the exception that is thrown on the response.

My client class derives from ClientBase<>. Here is the method that I use to call a service method:

private T RunMethod<T>(ReqHeaderType reqHeaders, ResHeaderType resHeaders, Func<T> meth)
{
    //Get request and response headers
    var reqProp = GetReqHeaders(reqHeaders);
    var resProp = GetResHeaders(resHeaders);

    using (var scope = new OperationContextScope(this.InnerChannel))
    {
        //Set headers
        OperationContext
            .Current
            .OutgoingMessageProperties[HttpRequestMessageProperty.Name] = reqProp;
        OperationContext
            .Current
            .OutgoingMessageProperties[HttpResponseMessageProperty.Name] = resProp;

        //Return the result of the call
        return meth();
    }
}  

The exception occurs when the call back, which runs the service method, is executed. Is there a way to catch the exception and check if it is a "Not Modified" response?


In my opinion, you really only want to use WCF channels on the client if you are using non-web WCF bindings on the server.

In your case you are not even using .Net on the server so I think WCF is going to cause you a whole lot of pain.

I suggest you simply use the HttpWebRequest and HttpWebResponse classes in System.Net. If you do that you can also take advantage of the built in caching that is provided by WinINet cache. If you set the caching policy in the client Http client you will get all the caching behaviour you need for free.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜