Windows Service with Timer based on CRM configuration value

Here I will be describing about a scenario where a windows service will perform a task based on time configured in CRM.

Please find the below problem statement:

Create a custom entity named “Configuration” which will hold the interval time at which the windows service should fire an email. Following are the steps to solve the problem:

1 . Browse to CRM>Settings>Customization>Customize Entities>Configuration:

2. Click on Add and enter the following values and click on OK button:

3. Click on “Save and Close” and Publish.

4. Goto >Workspace>Extensions >Configuration

5. Click on New and enter the following values:

6. Click on Save and Close.

7. Please refer the link for how to create a windows service.

Now in your existing windows service add the following code:

public class Program : ServiceBase{
private Timer timer = null;
private Microsoft.Crm.SdkTypeProxy.CrmService service = new Microsoft.Crm.SdkTypeProxy.CrmService();
private string[] timeValues = null;
public Program()
{
this.ServiceName = "EmailService";
this.GetCrmService();
//// Set the timer to fire every sixty seconds
//// so 1000 = 1 second. )
this.timer = new Timer(60000);
this.timer.Elapsed += new System.Timers.ElapsedEventHandler(this.TimerElapsed);
}
public static void Main()
{
ServiceBase.Run(new Program());
}
protected override void OnStart(string[] args)
{
base.OnStart(args);
this.timeValues = <span style="background-color: yellow;">GetEmailConfiguration(this.service)</span>;//this method returns the time interval value form CRM
this.timer.Start();
}
protected override void OnStop()
{
base.OnStop();
this.timer.Stop();
}
protected void TimerElapsed(object sender, ElapsedEventArgs e)
{
foreach (string times in this.timeValues)
{
if (DateTime.Now.ToString("HH:mm", CultureInfo.InvariantCulture) == times)
{

//Code for sending Email based on the values in CRM

}
}
}

8. Build the solution in release mode and Go to the Release Folder.

9. Change the configuration as per the requirements in the EmailService.exe.config.

10. Goto command prompt and copy the following:

 cd C:\WINDOWS\Microsoft.NET\Framework\v4.0.30319 

11. Enter and then again copy the following:

Installutil.exe <path of the exe>\ <name of the .exe file>

12. Go to services.msc and double-click on “EmailService

13. Go to “LogOn” tab and select “This Account” and give the appropriate credentials.

14. Right Click on the service and start the service as shown below:

15.  Now the service has started and will send the email at specified intervals to the respective recipients.

Happy Coding 🙂

Advertisements

Creating Windows Service

In this article I will be focusing on the steps for creating your first windows service using a console applictation.You can find many more articles on the same ,but here I will be showing the steps to create windows service quickly when your project needs it urgently and you don’t have time to go through lengthy articles.

Hope this article helps you to create the windows service in a very short time and meet your project deadlinesJ

Following are the steps for creating the windows service:

Step 1 :
Add a new Console Application in C# :

Step 2 :

Add reference the following dlls :

  • System.Configuration.Install
  • System.ServiceProcess

Step 3 :

Add the following code to the Program.cs file:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceProcess;
namespace ServiceExample
{
public class Program : ServiceBase
{
public Program()
{
this.ServiceName = "My Service";
}
static void Main(string[] args)
{
ServiceBase.Run(new Program());
}
protected override void OnStart(string[] args)
{
base.OnStart(args);
}
protected override void OnStop()
{
base.OnStop();
}
}
}

Step 4 :

Add a Component Class and name it as “MyServiceInstaller.cs”

Step 5 :

Add the following code to the MyServiceInstaller.cs file:

namespace ServiceExample
{
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration.Install;
using System.Linq;
using System.ServiceProcess;
using System.Text;
/// <summary>
/// RunInstaller MyServiceInstaller
/// </summary>
[RunInstaller(true)]
public class MyServiceInstaller : Installer
{
/// <summary>
/// Initializes a new instance of the <see cref="MyServiceInstaller"/> class.
/// </summary>
public MyServiceInstaller()
{
var processInstaller = new ServiceProcessInstaller();
var serviceInstaller = new ServiceInstaller();
////set the privileges
processInstaller.Account = ServiceAccount.LocalSystem;
serviceInstaller.DisplayName = "My Service";
serviceInstaller.StartType = ServiceStartMode.Manual;
////must be the same as what was set in Program's constructor
serviceInstaller.ServiceName = "My Service";
this.Installers.Add(processInstaller);
this.Installers.Add(serviceInstaller);
}
}
}

Step 6 :

Delete the MyServiceInstaller.Designer.cs file as shown below

Step 7 :

Build the solution and now it is ready to be installed.

Step 8 :

  • Goto the command prompt
  • Type the following line :

     C:\> cd C:\Windows\Microsoft.NET\Framework\v4.0.30319 
  • Type the following line :

     C:\Windows\Microsoft.NET\Framework\v4.0.30319>installutil <projectlocation>\ServiceExample.exe[Name of the exe]

After this the service is successfully installed.Now we need to start the service.

Step 9 :

  • Goto services.msc.
  • Now you will find your service and then select and start the same.

  • Now manually start the service and Go to “LogOn” tab and select “This Account” and give the appropriate credentials.

Following the above steps successfully completes the installation of your windows service.

Note # :

If you face a security issue while installing the service, the open Visual Studio Command Prompt from Run As Administrator.

After this follow steps 8 & 9 to complete the installation of the service.

Thank You 🙂