Ninject with Windows Service

Ninject with Windows Service

Lately, I had to develop a Window services that had to use ninject dependency injection from another project. Although I follow this blog from Martin, there were items I had to change.

Step 1: After u build your windows services, import the dll from the project that contains your dependency injection file and add to the references of your window services.

Step 2: In your main Program.cs for window services, insert your dependency file in the StandardKernel. For this example  AppInfrastructureNinjectModule is a class file that contains all bindings.

static class Program
   {
       /// <summary>
       /// The main entry point for the application.
       /// </summary>
       static void Main(string[] args)
       {
           using (IKernel kernel = new StandardKernel(new AppInfrastructureNinjectModule()))
           {
               var services = kernel.Get<Services>();

               ServiceBase[] ServicesToRun;
               ServicesToRun = new ServiceBase[]
               {
                     new GeofenceService(services)
               };
               ServiceBase.Run(ServicesToRun);
           }  // end of kernel

       }

 

AppInfrastureNinjectModule file that would have your binding of Interfaces for each of your repository tables and links to your Services.

public class AppInfrastructureNinjectModule : NinjectModule
   {
       public override void Load()
       {
         
           Bind<IAccountService>().To<AccountService>();
           Bind<IAuthenticationService>().To<AuthenticationService>();
           Bind<IEmailSendingService>().To<EmailSendingService>();
           Bind<ISmsSendingService>().To<SmsSendingService>();

           Bind<IRepository<Person>>().To<Repository<Person>>();
           Bind<IRepository<Profile>>().To<Repository<Profile>>();
           Bind<IRepository<Organisation>>().To<Repository<Organisation>>();
        }     
  }

Step 3: Create a Services class file that contains functions that you would want to create using some Interface from existing binding from the AppInfrastureNinjectModule(). In the main Program.cs file call Kernel.Get<Service> to call that services file.

var services = Kernel.Get<Service>.  The services file would like this:-

   public class Services
    {
        private IGeoService _geoService;
        private IQueService _queService;
        private IPersonService _personService;
        public Services(IGeoService geoService, IQueService queService, IPersonService personService)
        {
            _geoService = geoService;
            _queService = queService;
            _personService = personService;
        
        } 

     public bool IsInQue(int geoFenceId)   {  
     //get in Que table store PersonId not ProfileId
     if (_queService.GetAll().Any(x => x.GeoFenceId == geoFenceId)) {
     return true; }
     else  {
    return false;
   }  // end of IsInQue

 } // end of Services

Step 4: In the program.cs file, call the services in Window services (GeofenceService is our window services class file) so that you can call your functions that you defined in services class file anywhere in Geofence Window Service.

ServicesToRun = new ServiceBase[]
               {
                     new GeofenceService(services)
               };

Step 5. Use the functions you defined in Services file in Geofence Window services file. In this example IsInQue function is called inside of Geofence WindowService.

 public partial class GeofenceService : ServiceBase
    {
 //initialize the services in the Window GeoFence Services.
  public GeofenceService(Services services)
        {
            InitializeComponent();
            _services = services;
          }
//Onstart to set up a timer
   protected override void OnStart(string[] args)
        {  

              // taken out some code .. just show the main code 
            // Set up a timer to trigger every minute.  
            System.Timers.Timer timer = new System.Timers.Timer();
            timer.Interval = 60000; // 60 seconds  
            timer.Elapsed += new System.Timers.ElapsedEventHandler(this.OnTimer);
            timer.Start();

            // Update the service state to Running.  
            serviceStatus.dwCurrentState = ServiceState.SERVICE_RUNNING;
      }
   //in the ontimer you can call the _IsInQue function
     public void OnTimer(object sender, System.Timers.ElapsedEventArgs args)
        {
           _services.IsInQue(activeFence.Id);
  }
} // end of GeofenceService : ServiceBase

 

Leave a Reply

Your email address will not be published. Required fields are marked *