1

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 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?

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

With reluctance I'm partially taking your advice. Although I'm using the REST starter kit's HttpClient. The whole convoluted structure of WCF was way to much overkill for a simple REST client and hid to much of the details I needed. Thanks.
@Anthony Yes, the HttpClient is a much better option. That is what I am currently using.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.