1

I am creating a simple ASP.NET solution that is based on the ABP and as a part of this solution I use a standard Windows service that is supposed to do small background operations (so far only ICMP ping, but later probably more).

Is it possible to use the ABP application services inside of this Windows service (ideally using IoC)?

Thanks for any suggestion.

Martin Slezák
  • 181
  • 2
  • 11

1 Answers1

2

ofcourse you can use your AppServices in a Windows Service project. Also you can write background jobs in the windows service. You need to reference to your Application project from Windows Service. As each project behaves as module. Your new Windows Service needs to be registered as module. so you can use dependency services and other useful ABP libraries.

I'll show you some sample codes about moduling. But i recommend you to read the module doc : https://aspnetboilerplate.com/Pages/Documents/Module-System

MyWindowsServiceManagementModule.cs

 [DependsOn(typeof(MySampleProjectApplicationModule))]
    public class MyWindowsServiceManagementModule : AbpModule
    {
        public override void Initialize()
        {
            IocManager.RegisterAssemblyByConvention(Assembly.GetExecutingAssembly());

        }

    }

MyWindowsServiceWinService.cs

public partial class MyWindowsServiceWinService : ServiceBase
    {
        private MyWindowsServiceManagementBootstrapper _bootstrapper;

        public MyWindowsServiceWinService()
        {
            InitializeComponent();
        }

        protected override void OnStart(string[] args)
        {
            try
            {
                _bootstrapper = new MyWindowsServiceManagementBootstrapper();
                _bootstrapper.Initialize();
            }

            catch (Exception ex)
            {
                //EventLog.WriteEntry("MyWindowsService can not be started. Exception message = " + ex.GetType().Name + ": " + ex.Message + " | " + ex.StackTrace, EventLogEntryType.Error);               
            }
        }

        protected override void OnStop()
        {
            try
            {
                _bootstrapper.Dispose();
            }           
            catch (Exception ex)
            {
                //log...
            }           
        }
    }

MyWindowsServiceManagementBootstrapper.cs

    public class MyWindowsServiceManagementBootstrapper : AbpBootstrapper
        {

            public override void Initialize()
            {
                base.Initialize(); 
            }

            public override void Dispose()
            {
                //release your resources...
                base.Dispose();
            }
        }

Ps: As I wrote the codes on the top off my head, it might throw errors but basically this should guide you.

Alper Ebicoglu
  • 8,884
  • 1
  • 49
  • 55
  • Thanks. Although it did not work out of the box (as you have warned me), it was a crucial guidance that at the end helped me to fix the issue. Thanks. – Martin Slezák Oct 02 '17 at 15:59
  • @MartinSlezák if the above did not work as you expected, would you be able to provide your solution that did work? Thanks! – Gary Ewan Park Mar 13 '18 at 09:57
  • Based on my reading, I think the above has changed in the recent version of ABP to be something like `var bootstrapper = AbpBootstrapper.Create();` and then `bootstrapper.Initialize();` Is that correct? When I try this, I get the following error message: Can't create component 'Abp.BackgroundJobs.BackgroundJobStore' as it has dependencies to be satisfied. 'Abp.BackgroundJobs.BackgroundJobStore' is waiting for the following dependencies: - Service 'Abp.Domain.Repositories.IRepository`2 Any ideas? Could you update your answer with the correct way? – Gary Ewan Park Mar 13 '18 at 10:46
  • @LeonardoSpina yes, I was able to get this to work. In the end, the code was very similar to that which is included in the Migrator utility. Have a look at that, and if you aren't able to get something working, I will try to put a sample together. – Gary Ewan Park May 01 '18 at 06:26
  • @GaryEwanPark Thanks! But actually I'm still getting this: Castle.MicroKernel.Handlers.HandlerException: 'Can't create component 'Abp.BackgroundJobs.BackgroundJobStore' as it has dependencies to be satisfied. 'Abp.BackgroundJobs.BackgroundJobStore' is waiting for the following dependencies: - Service 'Abp.Domain.Repositories.IRepository`2[[Abp.BackgroundJobs.BackgroundJobInfo, Abp, Version=3.2.4.0, Culture=neutral, PublicKeyToken=null],[System.Int64, System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]]' which was not registered. ' – Leonardo Spina May 01 '18 at 21:03
  • If I get a chance, I will try to create a small sample application that shows how to do what is required. I likely won't be able to look at this this week though. – Gary Ewan Park May 03 '18 at 18:42
  • @GaryEwanPark that'd be great Thanks Gary. Looking forward to seeing your example – Leonardo Spina May 03 '18 at 20:16
  • @LeonardoSpina That is great news! Perhaps post another answer here with your solution? – Gary Ewan Park May 05 '18 at 07:09