Feb 3, 2012 at 1:04 PM
Edited Feb 3, 2012 at 1:17 PM
I am trying to set an ETag based on this blog article
Attempt 1: Using a DelegatingHandler
However I am having issues because Wp6 read operations are deliberately asynchronous which means I cannot retrieve the original object to determine the version in a synchronous way.
My asynchronous approach is below:
ObjectContent oc = response.Content as ObjectContent;
if (oc != null)
// TODO: Setting this sometimes causes a "Collection was modified; enumeration operation may not execute."
// because the CopyHeadersToNameValueCollection is executing at the same
response.Headers.ETag = ETagProvider.GetETag(t.Result);
This doesn't work because it spawns the asynchronous Read which eventually completes and tries to update the Headers collection, meanwhile the parent task in my DelegatingHandler thinks it's finished so the
HttpMessageHandlerChannel goes into the
which eventually calls into CopyHeadersToNameValueCollection.
This means two threads trying to access the same collection, hence the error: Collection was modified; enumeration operation may not execute (takes a bit of hammering the F5 key to get this error!)
Attempt 2: Using a MediaTypeFormatter
The alternative approach discussed in the docs below suggest using a MediaTypeFormatter:
However I cannot see any good methods where I have access to the object and the headers collection since OnWriteToStream only exposes the request headers and overriding OnGetResponseHeaders only has the types and raw response message.
Please can anyone help? Should I be trying to read the value synchronously (e.g. accessing the Result property which will suspend the current thread) - seems like making a deliberately async call synchronous is a bad idea that might work locally but fail
under load. Should a MediaTypeFormatter work? Is my approach all wrong? :-)