Solved

polling a webservice in .net 4.5

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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

690 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