Solved

polling a webservice in .net 4.5

Posted on 2013-11-01
4
813 Views
Last Modified: 2013-11-01
I am working in vs2012 and .net 4.5.  I have an existing web application that I need to add some functionality to.  Here is one of the things I need to do:  I login to a web service and, once I have logged in, I need to poll the service for "events" on my web page, ie: url = http://thesite/events, which returns a json package.

I have done this in a desktop app using a background worker thread in c# and it works just fine, but I'm not sure how to approach this in a asp.net project.  any direction appreciated.
0
Comment
Question by:dhenderson12
  • 2
  • 2
4 Comments
 
LVL 40

Expert Comment

by:Kyle Abrahams
ID: 39616757
You can use an <asp:timer> which is loosley equivalent to a background thread.

Set a timer interval
add the timer_tick  event

in timer_tick
stop the timer
process Web service
update whatever
start the timer.

Timer lives as long as you're on the page.
0
 

Author Comment

by:dhenderson12
ID: 39616824
thx for the response.  the site I'm polling has a timeout value attached (?timeout=60) so that if nothing happens during the 60 seconds, not event is returned and at 60 seconds it will return a response (of course, if something happens within the 60 seconds it will return a response right away).  

so, my action list is:
1. call the site for events
2. async wait for the return
3. read the return and do something
4. call site for events

how could I call the site async with the timer?  I know I sound slow (or dumb) but I'm trying to get my head around this ...
0
 
LVL 40

Accepted Solution

by:
Kyle Abrahams earned 500 total points
ID: 39617316
Not slow (or dumb) . . . if you're transitioning to the web from windows it's a different environment so of course you're going to have questions.

What's nice about the timer is that it's already async for you (that is to say, you can still interact with the UI on the main thread).

Think of the timer as a background thread, and then you can invoke the service synchronously.  I hope that makes sense?



Here's some code I previously wrote as an example:  

-  Web Service:

    //taskInfo is a class with a few properties.
     static TaskInfo task = new TaskInfo();

         [WebMethod]
        public TaskInfo GetTask()
        {
            return task;
        }

Open in new window



-  On the page:
protected void Timer1_Tick(object sender, EventArgs e)
    {

        Timer1.Enabled = false;
         MyWS.MyProjectWSSoap ws = null;
         ws = Session["Myservice"] as MyWS.MyProjectWSSoap ;
        Panel.Visible = true;

       MyWS.MyProjectWSSoap.TaskInfo ti =  ws.GetTask();


        if (Overall_Progress.Value !=  ti.overall_progress)
            Overall_Progress.Value =  ti.overall_progress;

        if (Current_Progress.Value != ti.current_progress)
            Current_Progress.Value =  ti.current_progress;

        lblCurrentStep.Text = ti.step_name;
        
        lblRefresh.Text = "Last Refresh: " + System.DateTime.Now.ToString("MM/dd/yyyy hh:mm:ss");

        
        //The process has finished.  Cleanup
        if (Session["dw_ProcessComplete"]  != null && (bool)Session["dw_ProcessComplete"] == true)
        {
          //I had to add another timer_tick to push a file down . . . you most likely not need this but I left it to show you it can be done.
            Timer2.Enabled = true;         
        }

        if (Session["dw_ErrorMessage"] != null)
        {
            lblError.Text = Session["dw_ErrorMessage"].ToString();
            pnlError.Update();
        }
        if (string.IsNullOrEmpty(lblError.Text) && !(Session["dw_ProcessComplete"]  != null && (bool)Session["dw_ProcessComplete"] == true))
            Timer1.Enabled = true;
    }

Open in new window




Feel free to ask more questions.
0
 

Author Closing Comment

by:dhenderson12
ID: 39617621
beautiful!  thanks for the help.
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now