Grouping the same WCF requests

If the WCF service receives the same request, it means the same MD5 for all the parameters that I want to block everything except the first request, until processing is complete and notifies all pending clients.

What is the best way to do this? Am I something like a channel shell, maybe there is a final implementation for archiving this?

+4
source share
2 answers

I'm not sure what will be the β€œbest” for WCF architecture, but you should consider installing InstanceContextMode to Single, as you are likely to take many synchronization steps for what you want to do here.

How about something like that? You will obviously need to do some synchronization in the dictionary itself, but at least this is the beginning.

private IDictionary<string, RequestToken> RequestTokens = new Dictionary<string, RequestToken>(); public MyResponse MyMethod(MyRequest request) { // get the MD5 for the request var md5 = GetMD5Hash(request); // check if another thread is processing/has processed an identical request RequestToken token; if (RequestTokens.TryGetValue(md5, out token)) { // if the token exists already then wait till we can acquire the lock // which indicates the processing has finished and a response is ready // for us to reuse lock (token.Sync) { return token.Response; } } else { var token = new Token(md5); lock (token.Sync) { RequestTokens.Add(md5, token); // do processing here.. var response = .... token.Response = response; return response; } } } private class RequestToken { private readonly object _sync = new object(); public RequestToken(string md5) { MD5 = md5; } public string MD5 { get; private set; } public object Sync { get { return _sync; } } public MyResponse Response { get; set; } } 

For me, this is something that I would like to distract from my business logic, and I personally will use PostSharp and write a small attribute to handle all of this.

I wrote a Memoizer attribute that does something similar in query-based response caching lines, but without synchronization steps, so you can probably take a look at what I did and change it accordingly to achieve what you need .

+1
source

First, I think of a custom hash table implementation that should handle MD% and look for it before starting a new request processing.

0
source

All Articles