A database search table in a company project needs to add new records to the table because a new product is added, and the product description, category, and other related information must be added, the data in this search table cannot be updated manually from time to time, because two or three tables will launch new products. Therefore, two parts are implemented. One part uses the Wcf Service to update the data of the table to be searched, and the other part uses the windows service to regularly call the Wcf Service. you can also use the program background to call the wcf Service when manually updating the service. this article only describes how to create a windows service and how to generate an installation package for windows Services.
Create a windows Service Project
Click OK.
Generated a Service1.cs service class. I renamed it ValorService. cs. Double-click ValorService. cs to view its source code. This is the source code of my ValorService. cs class.
public partial class ValorService : ServiceBase
{
public ValorService()
{
InitializeComponent();
}
protected override void OnStart(string[] args)
{
System.Timers.Timer timer = new System.Timers.Timer(Convert.ToInt32(ConfigurationManager.AppSettings["Interval"]));
timer.AutoReset = true;
timer.Elapsed += new System.Timers.ElapsedEventHandler(OnTimeEvent);
timer.Enabled = true;
}
private void OnTimeEvent(object source,System.Timers.ElapsedEventArgs e)
{
if (DateTime.Now.Hour == Convert.ToInt32(ConfigurationManager.AppSettings["ElapsedHour"]))
{
UpdateKeywordsSerachDataServiceClient service = new UpdateKeywordsSerachDataServiceClient();
service.SyncKeywordsData();
}
}
protected override void OnStop()
{
}
}
System.Timers.Timer timer = new System.Timers.Timer(Convert.ToInt32(ConfigurationManager.AppSettings["Interval"]));
The above Code creates a Timer-type Timer object and specifies the scheduled interval (in milliseconds) for triggering the Elapsed (that is, the scheduled call method event) event, ConfigurationManager. appSettings ["Interval"]) read the configuration file as follows:
<add key="Interval" value="60000" />
Note: The default windows service project we created is. We need to change the target Framework to. NET Framework 4.When the AutoReset of the Timer is set to false, the Timer will trigger only one Elapsed event when the interval is reached for the first time. Enabled is set to false.
Enable timer when timer. Enabled is set to true.
timer.Elapsed += new System.Timers.ElapsedEventHandler(OnTimeEvent);
Specifies the method of the event that the timer will trigger when it arrives. Here, the OnTimeEvent method calls the WCF Service. The OnTimeEvent method has such a line of code"
if (DateTime.Now.Hour == Convert.ToInt32(ConfigurationManager.AppSettings["ElapsedHour"]))
<add key="ElapsedHour" value="10" />
What I need is to call the WCF Service at every day.
All windows Services have been completed.
Next we will package windows Services.
Double-click the ValorServce. cs file, and the design view is displayed. Right-click the file, and the page shown in is displayed.
Select add installer. A file ProjectInstaller. cs is generated. Double-click the file to go to the design page,
Right-click serviceInstaller1 and choose Properties from the above menu. In the displayed Properties dialog box, you can see the ServiceName attribute. This is the name of the windows service we generated. StartType specifies the Service Startup type, such as automatic, manual, and disabled.
Next, let's check and set the serviceProcessInstaller1 attribute. The Account attribute specifies the user type. Here NetworkService users are used. Users can go to msdn for details.
Now we create an installer project.
Right-click Setup => Add => project output. The following page is displayed:
Click OK. Right-click Setup and choose View> Custom operation, as shown in figure
See the following
Right-click a custom operation and choose => Add a custom operation.
Click OK and select Valorservice
Click "OK" to generate the Setup project to generate the windows service installation package.