C# ASP.NET样板文件&;Windows服务
我正在创建一个基于ABP的简单ASP.NET解决方案,作为该解决方案的一部分,我使用了一个标准的Windows服务,该服务应该执行小型后台操作(到目前为止只有ICMP ping,但以后可能会更多) 是否可以在此Windows服务中使用ABP应用程序服务(理想情况下使用IoC)C# ASP.NET样板文件&;Windows服务,c#,asp.net,asp.net-boilerplate,C#,Asp.net,Asp.net Boilerplate,我正在创建一个基于ABP的简单ASP.NET解决方案,作为该解决方案的一部分,我使用了一个标准的Windows服务,该服务应该执行小型后台操作(到目前为止只有ICMP ping,但以后可能会更多) 是否可以在此Windows服务中使用ABP应用程序服务(理想情况下使用IoC) 谢谢您的建议。当然,您可以在Windows服务项目中使用AppServices。也可以在windows服务中编写后台作业。您需要从Windows服务引用应用程序项目。因为每个项目都表现为模块。您的新Windows服务需要注
谢谢您的建议。当然,您可以在Windows服务项目中使用AppServices。也可以在windows服务中编写后台作业。您需要从Windows服务引用应用程序项目。因为每个项目都表现为模块。您的新Windows服务需要注册为模块。因此,您可以使用依赖项服务和其他有用的ABP库 我将向您展示一些关于模块化的示例代码。但我建议您阅读模块文档: MyWindowsServiceManagementModule.cs
[DependsOn(typeof(MySampleProjectApplicationModule))]
public class MyWindowsServiceManagementModule : AbpModule
{
public override void Initialize()
{
IocManager.RegisterAssemblyByConvention(Assembly.GetExecutingAssembly());
}
}
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...
}
}
}
public class MyWindowsServiceManagementBootstrapper : AbpBootstrapper
{
public override void Initialize()
{
base.Initialize();
}
public override void Dispose()
{
//release your resources...
base.Dispose();
}
}
MyWindowsServiceWinService.cs
[DependsOn(typeof(MySampleProjectApplicationModule))]
public class MyWindowsServiceManagementModule : AbpModule
{
public override void Initialize()
{
IocManager.RegisterAssemblyByConvention(Assembly.GetExecutingAssembly());
}
}
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...
}
}
}
public class MyWindowsServiceManagementBootstrapper : AbpBootstrapper
{
public override void Initialize()
{
base.Initialize();
}
public override void Dispose()
{
//release your resources...
base.Dispose();
}
}
MyWindowsServiceManagementBootstrapper.cs
[DependsOn(typeof(MySampleProjectApplicationModule))]
public class MyWindowsServiceManagementModule : AbpModule
{
public override void Initialize()
{
IocManager.RegisterAssemblyByConvention(Assembly.GetExecutingAssembly());
}
}
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...
}
}
}
public class MyWindowsServiceManagementBootstrapper : AbpBootstrapper
{
public override void Initialize()
{
base.Initialize();
}
public override void Dispose()
{
//release your resources...
base.Dispose();
}
}
Ps:当我在头上写代码时,它可能会抛出错误,但基本上这应该能指导你 谢谢。虽然它不是开箱即用的(正如你警告我的那样),但它是一个至关重要的指导,最终帮助我解决了这个问题。谢谢。@MartinSlezák如果上述方法没有像您预期的那样奏效,您能否提供有效的解决方案?谢谢根据我的阅读,我认为在ABP的最新版本中,上面的内容已经更改为类似于
var bootstrapper=AbpBootstrapper.Create()然后是引导程序.Initialize()代码>正确吗?尝试此操作时,我收到以下错误消息:无法创建组件“Abp.BackgroundJobs.BackgroundJobStore”,因为它需要满足依赖关系Abp.BackgroundJobs.BackgroundJobStore'正在等待以下依赖项:-服务'Abp.Domain.Repositories.IRepository'2有什么想法吗?你能用正确的方式更新你的答案吗?@LeonardoSpina是的,我能让它工作。最后,代码与Migrator实用程序中包含的代码非常相似。看一看,如果你不能让一些东西起作用,我会试着把一个样品放在一起。@GaryEwanPark谢谢!但实际上我仍然得到了这样的答案:Castle.MicroKernel.Handlers.HandlerException:“无法创建组件”Abp.BackgroundJobs.BackgroundJobStore“,因为它需要满足依赖关系。”Abp.BackgroundJobs.BackgroundJobStore'正在等待以下依赖项:-服务'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]]“没有登记。”