Global.asax and timer timer call WebService run

Source: Internet
Author: User

Through the Global.asax Application_Start method in the website

Add Timer timed Call WebService

The webservice is responsible for a method in the background

To add data to a table in a database

Steps:

1. Create a new Web site by vs

2. Join Global.asax

3. Join WebService Edit and add references

4. Edit the Global.asax

5. Save Run Site View effect

=============================

1. Create a new Web site by vs

2. Join Global.asax

-----------------

The default content is as follows:

<%@ application Language= "C #"%>

<script runat= "Server" >

void Application_Start (object sender, EventArgs e)

{

Code that runs when the application starts

}

void Application_End (object sender, EventArgs e)

{

Code that runs when the application shuts down

}

void Application_Error (object sender, EventArgs e)

{

Code that runs when an unhandled error occurs

}

void Session_Start (object sender, EventArgs e)

{

Code that runs when a new session starts

}

void Session_End (object sender, EventArgs e)

{

Code that runs at the end of the session.

Note: Only the sessionstate mode in the Web.config file is set to

The Session_End event is raised when InProc. If the session mode is set to StateServer

or SQL Server, the event is not raised.

}

</script >

3. Join WebService Edit and add references

4. The following edits are made to Global.asax:

----------------------------

System.Timers.Timer MyTimer = new System.Timers.Timer (3000);

void Application_Start (object sender, EventArgs e)

{

Code that runs when the application starts

mytimer.elapsed + = new System.Timers.ElapsedEventHandler (ontimedevent);

Mytimer.interval = 3000;

Mytimer.enabled = true;

}

private static void Ontimedevent (object source, System.Timers.ElapsedEventArgs e)

{

localhost. Service1 a = new localhost. Service1 ();

string s = A.helloworld ();

}

void Application_End (object sender, EventArgs e)

{

Mytimer.dispose ()//code that runs when the application shuts down

}

5. Save Run Site View effect

From: http://blog.163.com/cristianojzhou@126/blog/static/166335404201082903327241/

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.