Answered by:
Timer Control in WCF Service hosted in Window Service?

Question
-
I want to call my WCF service which is hosted in Window Service on a regular interval period. I know I have to use the Timer control to invoke the service but I am not clear on the below points.:
Which Timer Control I should use?
Where should I write my code in Service Implementation class, or the Widows Service Host Project where I have written my below code.
Can anybody clear these points? Thanks in advance.
Protected Overrides Sub OnStart(ByVal args() As String) If myHost IsNot Nothing Then myHost.Close() End If myHost = New ServiceHost(GetType(Sentry_TaskManagerVersion3.SentryTMDemoVer3)) myHost.Open() End Sub ------ Protected Overrides Sub OnStop() ' Add code here to perform any tear-down necessary to stop your service. If myHost IsNot Nothing Then myHost.Close() myHost = Nothing End If End Sub
Friday, May 11, 2012 1:45 PM
Answers
-
Hi Shyam,
So based on your description, you have a WCF service which is hosted via windows service and you want to call it regularly by a constant time interval, correct?If so, I think you have several different options to implememnt the timer (for invoking the service):
1. As other member mentioned, you can use .NET built-in timer class (like the System.Threading.Timer class) to add code in your own application(either in the windows service itself or an external console or service appplication) which constantly invoke the WCF service operations.
#Timer Class
http://msdn.microsoft.com/en-us/library/system.threading.timer.aspx#Comparing the Timer Classes in the .NET Framework Class Library
http://msdn.microsoft.com/en-us/magazine/cc164015.aspx
2. You can create a simple console app which contains the code to invoke the WCF service. Then, use windows Task Scheduler to execute the console app in a regular time interval:#Schedule a task
http://windows.microsoft.com/en-US/windows7/Schedule-a-task#Task Scheduler Overview
http://technet.microsoft.com/en-us/library/cc721871.aspx
3. If you have SQL server in your local environment and some SQL guys familar with agent jobs, you can also consider using SQL server agent job to run a regular task (by time interval ). SQL agent job can support both internal code (through SQL or store-procedure or SSIS packages) or external applications.#How to: Schedule a Job (SQL Server Management Studio)
http://msdn.microsoft.com/en-us/library/ms191439(v=SQL.100).aspx#Creating SQL Server Agent Jobs
http://msdn.microsoft.com/en-us/library/ms135739(v=sql.105).aspxPlease remember to mark the replies as answers if they help and unmark them if they provide no help. Putting communities in your palms. Launch the browser on your phone now, type aka.ms/msforums and get connected!
- Marked as answer by Peter pi - MSFT Monday, May 21, 2012 2:48 AM
Monday, May 14, 2012 3:32 AM
All replies
-
You would be using the System.Threading Timer class not a Timer.control.http://msdn.microsoft.com/en-US/library/swx5easy(v=VS.80).aspxFriday, May 11, 2012 1:51 PM
-
Hi Shyam,
So based on your description, you have a WCF service which is hosted via windows service and you want to call it regularly by a constant time interval, correct?If so, I think you have several different options to implememnt the timer (for invoking the service):
1. As other member mentioned, you can use .NET built-in timer class (like the System.Threading.Timer class) to add code in your own application(either in the windows service itself or an external console or service appplication) which constantly invoke the WCF service operations.
#Timer Class
http://msdn.microsoft.com/en-us/library/system.threading.timer.aspx#Comparing the Timer Classes in the .NET Framework Class Library
http://msdn.microsoft.com/en-us/magazine/cc164015.aspx
2. You can create a simple console app which contains the code to invoke the WCF service. Then, use windows Task Scheduler to execute the console app in a regular time interval:#Schedule a task
http://windows.microsoft.com/en-US/windows7/Schedule-a-task#Task Scheduler Overview
http://technet.microsoft.com/en-us/library/cc721871.aspx
3. If you have SQL server in your local environment and some SQL guys familar with agent jobs, you can also consider using SQL server agent job to run a regular task (by time interval ). SQL agent job can support both internal code (through SQL or store-procedure or SSIS packages) or external applications.#How to: Schedule a Job (SQL Server Management Studio)
http://msdn.microsoft.com/en-us/library/ms191439(v=SQL.100).aspx#Creating SQL Server Agent Jobs
http://msdn.microsoft.com/en-us/library/ms135739(v=sql.105).aspxPlease remember to mark the replies as answers if they help and unmark them if they provide no help. Putting communities in your palms. Launch the browser on your phone now, type aka.ms/msforums and get connected!
- Marked as answer by Peter pi - MSFT Monday, May 21, 2012 2:48 AM
Monday, May 14, 2012 3:32 AM