繁体   English   中英

ASP.NET Boilerplate和Windows服务

[英]ASP.NET Boilerplate & Windows service

我正在创建一个基于ABP的简单ASP.NET解决方案,作为该解决方案的一部分,我使用了一个标准Windows服务,该服务应该执行较小的后台操作(到目前为止,仅进行ICMP ping操作,但以后可能会执行更多操作)。

是否可以在此Windows服务内部使用ABP应用程序服务(理想情况下使用IoC)?

感谢您的任何建议。

当然,您可以在Windows Service项目中使用AppServices。 您也可以在Windows服务中编写后台作业。 您需要从Windows Service引用您的Application项目。 由于每个项目都表现为模块。 您的新Windows服务需要注册为模块。 因此,您可以使用依赖项服务和其他有用的ABP库。

我将向您展示一些有关调制的示例代码。 但我建议您阅读模块文档: 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();
            }
        }

附:在我将代码写在头顶的时候,它可能会引发错误,但是基本上这应该可以指导您。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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