Solved

polling a webservice in .net 4.5

Posted on 2013-11-01
4
852 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.

732 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