Solved

polling a webservice in .net 4.5

Posted on 2013-11-01
4
798 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 39

Expert Comment

by:Kyle Abrahams
Comment Utility
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
Comment Utility
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 39

Accepted Solution

by:
Kyle Abrahams earned 500 total points
Comment Utility
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
Comment Utility
beautiful!  thanks for the help.
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

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:…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

763 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

9 Experts available now in Live!

Get 1:1 Help Now