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

  • Home
  • SEARCH
  • 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 8233147
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T18:02:22+00:00 2026-06-07T18:02:22+00:00

I am writing a C# class to wrap around a simple web service. The

  • 0

I am writing a C# class to wrap around a simple web service. The RESTful stuff was easy to wrap up, but now I need to raise an event when something on the server changes.

I’ve set the server up to make an event stream available, but I don’t know how to pick up the stream in C#.

Currently I’m dong something like this:

public class ServiceWrapper
{
    private readonly wc = new WebClient();

    public ServiceWrapper()
    {
        wc.OpenReadAsync(new Uri(UriOfEvent));
        wc.OpenReadCompleted += ServerEventOccurs;
    }

    private void ServerEventOccurs(object sender, OpenReadCompletedEventArgs args)
    {
        using (var sr = new StreamReader(args.Result))
        {
            var message = ParseStream(sr);
            RaiseServerEventOccurred(message);
        }

        wc.OpenReadAsync(new Uri(UriOfEvent));
    } 

    //usual code for declaring and raising ServerEventOccurred event
}

In my test the event gets picked up once, but not twice. The event on the server is essentially a switch – something happens and it goes on, something else happens and it goes off. I know the switching works, because I’ve hooked the event stream up to a normal web page to test it.

How should I be dealing with event streams in C#?

Edit 1: I’ve updated the code to fix the bug TimVK points out, but the event stream still isn’t being picked up the second time it should be.

  • 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-07T18:02:24+00:00Added an answer on June 7, 2026 at 6:02 pm

    Doesn’t it work when you put your wc as a property in the class instead of creating always a new one in your methods?

    public class ServiceWrapper 
    { 
        WebClient wc {get;set;}
        public ServiceWrapper() 
        { 
            wc = new WebClient(); 
            wc.OpenReadAsync(new Uri(UriOfEvent)); 
            wc.OpenReadCompleted += ServerEventOccurs; 
        } 
     
        private void ServerEventOccurs(object sender, OpenReadCompletedEventArgs args) 
        { 
            using (var sr = new StreamReader(args.Result)) 
            { 
                var message = ParseStream(sr); 
                RaiseServerEventOccurred(message); 
            } 
     
            wc = new WebClient(); 
            wc.OpenReadAsync(new Uri(UriOfEvent)); 
        }  
     
        //usual code for declaring and raising ServerEventOccurred event 
    } 
    

    Then I suppose the event should be raised everytime.

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

Sidebar

Related Questions

I'm writing a method to wrap another method as part of a class. It
I'm writing a bound Service class for android. In addition, I thought I'd provide
I'm writing an (unmanaged) C++ class to wrap the Windows PropertySheet . Essentially, something
I'm writing a node.js addon in C++. I wrap some class instances using node::ObjectWrap,
I'm writing a C++ class to wrap sockets (I know that there are good
I'm writing a helper class to wrap the functionality of JQuery Taconite plugin. The
I am sure this is pretty standard stuff, but I can't seem to wrap
Scala programmer should have known that this sort of writing : class Person{ var
I tried writing this class #include <memory> class ContainerUnique { public: ContainerUnique(void); ~ContainerUnique(void); private:
I'm writing a class which shares several different features with std::function (or at least

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.