This message was discovered on ASPFriends.com 'ngfx-services' list.
Responses highlighted in red are from those people who are likely to be able to contribute good, authoratitive information to this discussion. They include Microsoft employees, MVP's and others who IMHO contribute well to these kinds of discussions.
| Jim Davis |
I tried posting this to ASPNGFreeForAll but received no really qualified replies. I am posting it here hoping to get a little direction.
This article seems to really hit my target end result. http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguidnf/ht ml/cptskCreatingServiceProgrammatically.asp Does anyone know the best way to keep a service in loop? Force it to re-run a script without being restarted.
I have done some research under the requested namespace for automation, but only found information that confused me a bit. I don't have the resources to go through this namespace to build something from scratch. I have seen how easy it is to take a class file and turn it into Web Service, I was hoping that there was similar way to create a ServiceProcess the merely ran a piece of Code on a designated timed interval (3 minutes). And don't you have to install services like this on the server?
Does anyone have some sample code regarding this?
Thanks, Jim Davis
-----Original Message----- From: Arnold, Jim [mailto:Click here to reveal e-mail address] Sent: Friday, October 12, 2001 10:26 AM To: aspngfreeforall Subject: [aspngfreeforall] RE: Automation .NET
Look at System.ServiceProcess.
Jim
-----Original Message----- From: Jim Davis [mailto:Click here to reveal e-mail address] Sent: Friday, October 12, 2001 3:15 PM To: aspngfreeforall Subject: [aspngfreeforall] Automation .NET
I am writing an app that will rely on some sort of automation to be run independently off the server. It needs to be more consistent then just placing it in the default Page_Load as to be run whenever accessed by an Anonymous user. I was thinking of WSH but I need the ability to manipulate a Data Set. Is there anything in ASP.NET that mimics WSH and it's ability to become a scheduled service running in the background?
Thanks, Jim Davis
Thanks,
Jim Davis
|
|
|
| |
|
| |
| |
| Mitch Denny (VIP) |
Jim,
There are a number of good samples in the quickstarts that come with the Framework SDK, you can find them at http://docs.aspng.com/QuickStart/, more specifically they have some examples of services as the following URL: http://docs.aspng.com/quickstart/howto/doc/simpleservice.aspx
Just to short circut alot of reading which probably needs further explaination anyway, a service is already running in a loop. All you need to do is find a way to trigger your execution periodically. Take a look at the following code:
using System; using System.Collections; using System.ComponentModel; using System.Data; using System.Diagnostics; using System.ServiceProcess; using System.Timers;
public class Sandpit46Service : System.ServiceProcess.ServiceBase {
private Timer timer = null;
public Sandpit46Service() {
this.ServiceName = "Sandpit46Service"; this.timer = new Timer(5000); this.timer.Elapsed += new ElapsedEventHandler(this.ticker_Elapsed);
}
protected override void OnStart(string[] args) {
this.timer.Start();
}
protected override void OnPause() {
this.timer.Stop();
}
protected override void OnContinue() {
this.timer.Start();
}
protected override void OnStop() {
this.timer.Stop();
}
private void ticker_Elapsed(object sender, ElapsedEventArgs e) {
EventLog.WriteEntry( "Sandpit46Service", "Elapsed", EventLogEntryType.Information, 0 );
}
public static void Main() {
System.ServiceProcess.ServiceBase.Run(new Sandpit46Service());;
}
}
Sorry for the undocumented nature, but I will explain more about it here. Notice that the class that inherits from ServiceBase has a private instance of a Timer object, and in the constructor I attach an event handler to its elapsed event.
The timer is configured (in this example) to go off every 5000 milliseconds, causing the "timer_Elapsed" event handler to be executed. This is the sort of behaviour that you are after right? Just at a larger time interval (180000 milliseconds).
Notice how I haven't actually started the timer in the constructor. A well behaved service doesn't do anything until its requested to start, so you can see how I have overrided the four methods:
OnStart(string[] args); OnPause(); OnContinue(); OnStop();
And put appropriate code to start and stop the timer.
In the eventhandler, you could put in your own code quite easily, or make it fire off an external script using the System.Diagnostics.Process class. If you wanted to get really tricky, you could create a configuration file for the service that listed classes in assemblies which implement an interface you define and using reflection execute that code, kind of a generic tool for timed execution of .NET code. Of course that raises a whole heap of security issues which can cause audit crews to escort you from your employeers premises - but the option is still there regardless.
Now, incase you don't know how to write one, here is an installer class for this service so that you can play around with it:
using System; using System.Collections; using System.ComponentModel; using System.Configuration.Install; using System.ServiceProcess;
[RunInstallerAttribute(true)] public class Sandpit46ServiceInstaller : Installer {
private ServiceInstaller serviceInstaller = null; private ServiceProcessInstaller serviceProcessInstaller = null;
public Sandpit46ServiceInstaller() {
serviceInstaller = new ServiceInstaller(); serviceInstaller.ServiceName = "Sandpit46Service"; serviceInstaller.DisplayName = "Sandpit 46 Service"; serviceInstaller.StartType = ServiceStartMode.Manual; this.Installers.Add(serviceInstaller);
serviceProcessInstaller = new ServiceProcessInstaller(); serviceProcessInstaller.Account = ServiceAccount.LocalSystem; this.Installers.Add(serviceProcessInstaller);
}
}
Once again, I apologise for the undocumented nature of the source, feel free to ask questions about it.
---------------------------------------- - Mitch Denny - http://www.warbyte.com - Click here to reveal e-mail address - +61 (414) 610-141 -
[Original message clipped]
|
|
|
| |
|
|
| |
|
| |
| Jim Davis |
Mitch,
Thanks for your reply.
I posted the question in hopes that I would get another stable alternative to a (WSH) VBS file running from the command line and setup to do so by a scheduling program. So my goal is only to get a script in an event that re-runs every three minutes or so on the server. I only have a couple of questions, when the service is running in a loop, does that mean that every time the service loops it is actually restarting (OnStart/OnStop)? I understand that any code needing to be executed should go under private void ticker_Elapsed, but what if I need to run VB (your example seems to be in C#)? How can incorporate a class file I build in VB? I haven't actually built a Class File yet, but I have read some really good books and it doesn't seem that hard. My only stumbling block would be importing that Class into the Service, then by calling a method of the class during the ticker_Elapsed my code will be executed in VB (which is the only language I know).
Going over some of the links you sent me, does this...
http://docs.aspng.com/quickstart/util/srcview.aspx?path=/quickstart/howto/sa mples/Services/ServiceApplication/SimpleService/SimpleService.src
...cover everything you did in your C# example? If so I do think I will need the Class file after all.
Thanks for your reply,
Jim Davis
-----Original Message----- From: Mitch Denny [mailto:Click here to reveal e-mail address] Sent: Wednesday, October 17, 2001 7:48 PM To: ngfx-services Subject: [ngfx-services] RE: Building A Service
Jim,
There are a number of good samples in the quickstarts that come with the Framework SDK, you can find them at http://docs.aspng.com/QuickStart/, more specifically they have some examples of services as the following URL: http://docs.aspng.com/quickstart/howto/doc/simpleservice.aspx
Just to short circut alot of reading which probably needs further explaination anyway, a service is already running in a loop. All you need to do is find a way to trigger your execution periodically. Take a look at the following code:
using System; using System.Collections; using System.ComponentModel; using System.Data; using System.Diagnostics; using System.ServiceProcess; using System.Timers;
public class Sandpit46Service : System.ServiceProcess.ServiceBase {
private Timer timer = null;
public Sandpit46Service() {
this.ServiceName = "Sandpit46Service"; this.timer = new Timer(5000); this.timer.Elapsed += new ElapsedEventHandler(this.ticker_Elapsed);
}
protected override void OnStart(string[] args) {
this.timer.Start();
}
protected override void OnPause() {
this.timer.Stop();
}
protected override void OnContinue() {
this.timer.Start();
}
protected override void OnStop() {
this.timer.Stop();
}
private void ticker_Elapsed(object sender, ElapsedEventArgs e) {
EventLog.WriteEntry( "Sandpit46Service", "Elapsed", EventLogEntryType.Information, 0 );
}
public static void Main() {
System.ServiceProcess.ServiceBase.Run(new Sandpit46Service());;
}
}
Sorry for the undocumented nature, but I will explain more about it here. Notice that the class that inherits from ServiceBase has a private instance of a Timer object, and in the constructor I attach an event handler to its elapsed event.
The timer is configured (in this example) to go off every 5000 milliseconds, causing the "timer_Elapsed" event handler to be executed. This is the sort of behaviour that you are after right? Just at a larger time interval (180000 milliseconds).
Notice how I haven't actually started the timer in the constructor. A well behaved service doesn't do anything until its requested to start, so you can see how I have overrided the four methods:
OnStart(string[] args); OnPause(); OnContinue(); OnStop();
And put appropriate code to start and stop the timer.
In the eventhandler, you could put in your own code quite easily, or make it fire off an external script using the System.Diagnostics.Process class. If you wanted to get really tricky, you could create a configuration file for the service that listed classes in assemblies which implement an interface you define and using reflection execute that code, kind of a generic tool for timed execution of .NET code. Of course that raises a whole heap of security issues which can cause audit crews to escort you from your employeers premises - but the option is still there regardless.
Now, incase you don't know how to write one, here is an installer class for this service so that you can play around with it:
using System; using System.Collections; using System.ComponentModel; using System.Configuration.Install; using System.ServiceProcess;
[RunInstallerAttribute(true)] public class Sandpit46ServiceInstaller : Installer {
private ServiceInstaller serviceInstaller = null; private ServiceProcessInstaller serviceProcessInstaller = null;
public Sandpit46ServiceInstaller() {
serviceInstaller = new ServiceInstaller(); serviceInstaller.ServiceName = "Sandpit46Service"; serviceInstaller.DisplayName = "Sandpit 46 Service"; serviceInstaller.StartType = ServiceStartMode.Manual; this.Installers.Add(serviceInstaller);
serviceProcessInstaller = new ServiceProcessInstaller(); serviceProcessInstaller.Account = ServiceAccount.LocalSystem; this.Installers.Add(serviceProcessInstaller);
}
}
Once again, I apologise for the undocumented nature of the source, feel free to ask questions about it.
---------------------------------------- - Mitch Denny - http://www.warbyte.com - Click here to reveal e-mail address - +61 (414) 610-141 -
[Original message clipped]
| [ngfx-services] member Click here to reveal e-mail address = YOUR ID | http://www.asplists.com/asplists/aspngbeta.asp = JOIN/QUIT | http://www.asplists.com/search = SEARCH Archives
|
|
|
| |
|
|
| |
|
| |
| Mitch Denny (VIP) |
Jim,
Actually. I did wonder why you didn't just use a VBScript with a Scheduler. In my experience that is pretty stable if you have your operational processes in order.
On to answering your questions. When a service starts, the executable containing the service code is run. This isn't actually when the service is "started", what actually happens is that the service is "put out there" and the operating system tells it what to do (start, stop etc).
This is actually called a message loop. It is convienient to thinking of a message loop as something akin to juggling, the object is kept up in the air, but the juggling process isn't actually changing its state.
In the same way, the message loop doesn't start or stop the service, it just keeps it in the air. It is up to the service control manager whether that service starts or stops.
The message loop takes messages from the operating system and passes them to the service object, if the operating system has no messages for the service, then the message loop just sits there and spins its wheels - this is what it is usually doing all the time.
I see that you found the VB sample in the SDK samples, that should step you though creating a VB version of pretty much the same service. The C# sample isn't the same as the one in the SDK samples, but its pretty close, at list similar in function.
VB.NET, C# and almost all other languages which currently run on the .NET Framework are based on the concepts of classes and objects, so it is highly likely that you are going to need to write classes from time to time.
---------------------------------------- - Mitch Denny - http://www.warbyte.com - Click here to reveal e-mail address - +61 (414) 610-141 -
[Original message clipped]
|
|
|
| |
|
|
| |
|
|
|
|
|