Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

polling a webservice in .net 4.5

Posted on 2013-11-01
4
Medium Priority
?
891 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 41

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 41

Accepted Solution

by:
Kyle Abrahams earned 2000 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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…

604 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