Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8655545
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T15:03:29+00:00 2026-06-12T15:03:29+00:00

I am using Reactive Extensions to create anonymous Observable to perform web request, post-process

  • 0

I am using Reactive Extensions to create anonymous Observable to perform web request, post-process it and return some value. This technique helped me to separate the mechanics of value acquisition from other classes that use these values. Web requests are performed frequently, so there’s a timer that constructs and invokes requests using this technique. I build Observable as following:

Observable.FromAsyncPattern<WebResponse>(getRequest.BeginGetResponse, getRequest.EndGetResponse)

Actually, endpoint is not always available. In the case of web request timeout callbacks specified in Subscribe() methods are never called. I’m handling this scenario with Timeout() Rx extension. Also I’m doing post-processing of request result. Here’s piece of my emulation code that I implemented to address the issue:

var request = HttpWebRequest.Create("http://10.5.5.137/unreachable_endpoint");
var obs = Observable.FromAsyncPattern<WebResponse>(request.BeginGetResponse, request.EndGetResponse)();
var disposable = obs.Select(res => res.ContentLength).Timeout(TimeSpan.FromSeconds(2)).Subscribe(...)

Then I implemented disposing the return value of Subscribe() method to unsubscribe callback delegates from the Observable. Actually I have a collection of triples <IObservable obs, IDisposable disp, bool RequestComplete>. Subscribe() method callbacks set RequestComplete = true and over time all disposables get Disposed if they are not needed anymore, i.e. callbacks have already been invoked. Everything seemed to be fine, but I noticed that the collection is constantly growing and I don’t know why.

So I implemented the emulation of this situation. This code doesn’t have disposing part – I replaced it with simple request counter:

public static class RequestCounter
{
    private static object syncRoot = new object();

    private static int count = 0;

    public static void Increment()
    {
        lock (syncRoot)
        {
            count++;
        }
    }

    public static void Decrement()
    {
        lock (syncRoot)
        {
            count--;
        }
    }

    public static int RequestCount
    {
        get { return count; }
    }
}

And here’s the test itself:

public void RunTest()
    {
        while (true)
        {
            var request = HttpWebRequest.Create("http://10.5.5.137/unreachable_endpoint");
            var obs = Observable.FromAsyncPattern<WebResponse>(request.BeginGetResponse, request.EndGetResponse)();
            RequestCounter.Increment();
            var disposable = obs.Select(res => res.ContentLength).Timeout(TimeSpan.FromSeconds(2)).Subscribe(
                res =>
                {
                    RequestCounter.Decrement();
                    Console.WriteLine("Pending requests count: {0}", RequestCounter.RequestCount);
                },
                ex =>
                {
                    RequestCounter.Decrement();
                    Console.WriteLine("Pending requests count: {0}", RequestCounter.RequestCount);
                },
                () =>
                {
                });
        }
    }

Before web request counter gets incremented. After request timeout it gets decremented. But the request counter grows infinite. Can You explain me what am I missing?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T15:03:30+00:00Added an answer on June 12, 2026 at 3:03 pm

    The subscription to the observable sequence is asynchronous. Your while loop keeps going as fast as it can. Increments will outweigh the delayed decrements.

    If you’re using Rx v2.0 with .NET 4.5, you could use await to run your loop and proceed to the following iteration when the previous request completed:

    while (true)
    {
        var obs = ...;
    
        RequestCounter.Increment();
    
        await obs.Select(...).Timeout(...);
    
        RequestCounter.Decrement();
    
        Console.WriteLine(...);
    }
    

    The bigger question is why you’re trying to dispose the subscriptions if the requests completed? Rx cleans up after itself when a sequence reaches a terminal state (OnError, OnCompleted), so it seems you could simply drop the IDisposable objects on the floor and not worry about those.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm creating multiple asynchronous web requests using IObservables and reactive extensions. So this creates
I'm using RX extensions and WF4 to create a workflow which reacts to observable
I am using a Reactive Extensions Observable data stream tied to a COM port
I have the following code in a WPF application using Reactive Extensions for .NET:
well. i was use Reactive Extensions (Rx) package async Request,Before you can start writing
I'm using reactive extensions in my wp7 app that I'm making and I wish
How would you structure a simple simulation engine using Reactive Extensions? For example, suppose
I'm using the Reactive Extensions (Rx) and a repository pattern to facilitate getting data
I am learning RX (Reactive Extensions), and have found someone posted some code nearly
I'm using Reactive Extensions for easy event handling in my ViewModels (Silverlight and/or Wp7

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.