简体   繁体   中英

ASP.NET Boilerplate & Windows service

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM