You can trace the stream lifetimes using the Spy method I present in this question.
You aren't far off here - I think the problem might be that you are being a bit over-zealous with the Observable.Using
. The Observable.Return
's can complete causing a race condition where the disposal of the response stream can take place before the stream reader is finished. I think this might behave more reliably:
var reqUri = new Uri(
string.Format("https://cds.cern.ch/record/{0}/export/xm?ln=en", docId));
var s = Observable.FromAsyncPattern<WebResponse>(
wr.BeginGetResponse,
wr.EndGetResponse)
.Invoke()
.Select(response => response.GetResponseStream())
.SelectMany(stream =>
Observable.Using( () => new StreamReader(stream),
reader => Observable.StartAsync(reader.ReadToEndAsync)))
.Select(ParseToMD);