diff --git a/docs/en/Background-Workers-Hangfire.md b/docs/en/Background-Workers-Hangfire.md index e1e6fc71ad..2ea7fd633e 100644 --- a/docs/en/Background-Workers-Hangfire.md +++ b/docs/en/Background-Workers-Hangfire.md @@ -66,6 +66,33 @@ public class MyLogWorker : HangfireBackgroundWorkerBase > You can directly implement the `IHangfireBackgroundWorker`, but `HangfireBackgroundWorkerBase` provides some useful properties like Logger. +### UnitOfWork + +For use with `UnitOfWorkAttribute`, you need to define an interface for worker: + +```csharp +public interface IMyLogWorker : IHangfireBackgroundWorker +{ +} + +[ExposeServices(typeof(IMyLogWorker))] +public class MyLogWorker : HangfireBackgroundWorkerBase, IMyLogWorker +{ + public MyLogWorker() + { + RecurringJobId = nameof(MyLogWorker); + CronExpression = Cron.Daily(); + } + + [UnitOfWork] + public override Task DoWorkAsync() + { + Logger.LogInformation("Executed MyLogWorker..!"); + return Task.CompletedTask; + } +} +``` + ## Register BackgroundWorkerManager After creating a background worker class, you should add it to the `IBackgroundWorkerManager`. The most common place is the `OnApplicationInitialization` method of your module class: @@ -78,6 +105,9 @@ public class MyModule : AbpModule ApplicationInitializationContext context) { context.AddBackgroundWorker(); + + //If the interface is defined + //context.AddBackgroundWorker(); } } ```` diff --git a/docs/en/Modules/Cms-Kit/Index.md b/docs/en/Modules/Cms-Kit/Index.md index 7d773973e0..46ecaad8c8 100644 --- a/docs/en/Modules/Cms-Kit/Index.md +++ b/docs/en/Modules/Cms-Kit/Index.md @@ -19,15 +19,14 @@ Click to a feature to understand and learn how to use it. All features are individually usable. If you disable a feature, it completely disappears from your application, even from the database tables, by the help of the [Global Features](../../Global-Features.md) system. ## Pre Requirements +- This module depends on [BlobStoring](../../Blob-Storing.md) module for keeping media content. +> Make sure `BlobStoring` module is installed and at leats one provider is configured properly. For more information, check the [documentation](../../Blob-Storing.md). -CMS Kit uses [distributed cache](../../Caching.md) for responding faster. - +- CMS Kit uses [distributed cache](../../Caching.md) for responding faster. > Using a distributed cache, such as [Redis](../../Redis-Cache.md), is highly recommended for data consistency in distributed/clustered deployments. ## How to Install -> This module is depends on [BlobStoring](../../Blob-Storing.md) module, please install `BlobStoring` module first and add a provider. For more information, check the [documentation](../../Blob-Storing.md). - [ABP CLI](../../CLI.md) allows installing a module to a solution using the `add-module` command. You can install the CMS Kit module in a command-line terminal with the following command: ```bash @@ -77,4 +76,4 @@ All tables/collections use the `Cms` prefix by default. Set static properties on This module uses `CmsKit` for the connection string name. If you don't define a connection string with this name, it fallbacks to the `Default` connection string. -See the [connection strings](https://docs.abp.io/en/abp/latest/Connection-Strings) documentation for details. \ No newline at end of file +See the [connection strings](https://docs.abp.io/en/abp/latest/Connection-Strings) documentation for details. diff --git a/docs/zh-Hans/Background-Workers-Hangfire.md b/docs/zh-Hans/Background-Workers-Hangfire.md index bd63ff91e9..5e53928ca1 100644 --- a/docs/zh-Hans/Background-Workers-Hangfire.md +++ b/docs/zh-Hans/Background-Workers-Hangfire.md @@ -67,6 +67,33 @@ public class MyLogWorker : HangfireBackgroundWorkerBase > 你可以直接实现 `IHangfireBackgroundWorker`, 但是 `HangfireBackgroundWorkerBase` 提供了一些有用的属性,例如 `Logger`. +### UnitOfWork + +使用 `UnitOfWorkAttribute` 你需要为工作者定义一个接口: + +```csharp +public interface IMyLogWorker : IHangfireBackgroundWorker +{ +} + +[ExposeServices(typeof(IMyLogWorker))] +public class MyLogWorker : HangfireBackgroundWorkerBase, IMyLogWorker +{ + public MyLogWorker() + { + RecurringJobId = nameof(MyLogWorker); + CronExpression = Cron.Daily(); + } + + [UnitOfWork] + public override Task DoWorkAsync() + { + Logger.LogInformation("Executed MyLogWorker..!"); + return Task.CompletedTask; + } +} +``` + ## 注册到后台工作者管理器 创建一个后台工作者后, 你应该添加到 `IBackgroundWorkerManager`, 最常用的地方是在你模块类的 `OnApplicationInitialization` 方法中: @@ -79,6 +106,9 @@ public class MyModule : AbpModule ApplicationInitializationContext context) { context.AddBackgroundWorker(); + + //如果定义了接口 + //context.AddBackgroundWorker(); } } ```` diff --git a/framework/src/Volo.Abp.BackgroundWorkers.Hangfire/Volo/Abp/BackgroundWorkers/Hangfire/HangfireBackgroundWorkerManager.cs b/framework/src/Volo.Abp.BackgroundWorkers.Hangfire/Volo/Abp/BackgroundWorkers/Hangfire/HangfireBackgroundWorkerManager.cs index 33e0ebde4c..8449dc3df0 100644 --- a/framework/src/Volo.Abp.BackgroundWorkers.Hangfire/Volo/Abp/BackgroundWorkers/Hangfire/HangfireBackgroundWorkerManager.cs +++ b/framework/src/Volo.Abp.BackgroundWorkers.Hangfire/Volo/Abp/BackgroundWorkers/Hangfire/HangfireBackgroundWorkerManager.cs @@ -4,6 +4,7 @@ using System.Threading; using System.Threading.Tasks; using Hangfire; using Volo.Abp.DependencyInjection; +using Volo.Abp.DynamicProxy; using Volo.Abp.Threading; namespace Volo.Abp.BackgroundWorkers.Hangfire; @@ -25,14 +26,14 @@ public class HangfireBackgroundWorkerManager : IBackgroundWorkerManager, ISingle { if (worker is IHangfireBackgroundWorker hangfireBackgroundWorker) { + var unProxyWorker = ProxyHelper.UnProxy(hangfireBackgroundWorker); if (hangfireBackgroundWorker.RecurringJobId.IsNullOrWhiteSpace()) { - RecurringJob.AddOrUpdate(() => hangfireBackgroundWorker.DoWorkAsync(), - hangfireBackgroundWorker.CronExpression); + RecurringJob.AddOrUpdate(() => ((IHangfireBackgroundWorker)unProxyWorker).DoWorkAsync(),hangfireBackgroundWorker.CronExpression); } else { - RecurringJob.AddOrUpdate(hangfireBackgroundWorker.RecurringJobId,() => hangfireBackgroundWorker.DoWorkAsync(), + RecurringJob.AddOrUpdate(hangfireBackgroundWorker.RecurringJobId,() => ((IHangfireBackgroundWorker)unProxyWorker).DoWorkAsync(), hangfireBackgroundWorker.CronExpression); } } @@ -64,7 +65,7 @@ public class HangfireBackgroundWorkerManager : IBackgroundWorkerManager, ISingle return Task.CompletedTask; } - var adapterType = typeof(HangfirePeriodicBackgroundWorkerAdapter<>).MakeGenericType(worker.GetType()); + var adapterType = typeof(HangfirePeriodicBackgroundWorkerAdapter<>).MakeGenericType(ProxyHelper.GetUnProxiedType(worker)); var workerAdapter = Activator.CreateInstance(adapterType) as IHangfireBackgroundWorker; RecurringJob.AddOrUpdate(() => workerAdapter.DoWorkAsync(), GetCron(period.Value)); diff --git a/framework/src/Volo.Abp.BackgroundWorkers.Quartz/Volo/Abp/BackgroundWorkers/Quartz/QuartzBackgroundWorkerManager.cs b/framework/src/Volo.Abp.BackgroundWorkers.Quartz/Volo/Abp/BackgroundWorkers/Quartz/QuartzBackgroundWorkerManager.cs index 954f21bcda..060afd3ac6 100644 --- a/framework/src/Volo.Abp.BackgroundWorkers.Quartz/Volo/Abp/BackgroundWorkers/Quartz/QuartzBackgroundWorkerManager.cs +++ b/framework/src/Volo.Abp.BackgroundWorkers.Quartz/Volo/Abp/BackgroundWorkers/Quartz/QuartzBackgroundWorkerManager.cs @@ -1,8 +1,9 @@ -using System; +using System; using System.Threading; using System.Threading.Tasks; using Quartz; using Volo.Abp.DependencyInjection; +using Volo.Abp.DynamicProxy; using Volo.Abp.Threading; namespace Volo.Abp.BackgroundWorkers.Quartz; @@ -56,7 +57,7 @@ public class QuartzBackgroundWorkerManager : IBackgroundWorkerManager, ISingleto } else { - var adapterType = typeof(QuartzPeriodicBackgroundWorkerAdapter<>).MakeGenericType(worker.GetType()); + var adapterType = typeof(QuartzPeriodicBackgroundWorkerAdapter<>).MakeGenericType(ProxyHelper.GetUnProxiedType(worker)); var workerAdapter = Activator.CreateInstance(adapterType) as IQuartzBackgroundWorkerAdapter; diff --git a/framework/src/Volo.Abp.BackgroundWorkers.Quartz/Volo/Abp/BackgroundWorkers/Quartz/QuartzPeriodicBackgroundWorkerAdapter.cs b/framework/src/Volo.Abp.BackgroundWorkers.Quartz/Volo/Abp/BackgroundWorkers/Quartz/QuartzPeriodicBackgroundWorkerAdapter.cs index 7361fdfeb9..e0477f7106 100644 --- a/framework/src/Volo.Abp.BackgroundWorkers.Quartz/Volo/Abp/BackgroundWorkers/Quartz/QuartzPeriodicBackgroundWorkerAdapter.cs +++ b/framework/src/Volo.Abp.BackgroundWorkers.Quartz/Volo/Abp/BackgroundWorkers/Quartz/QuartzPeriodicBackgroundWorkerAdapter.cs @@ -1,7 +1,8 @@ -using System; +using System; using System.Reflection; using System.Threading.Tasks; using Quartz; +using Volo.Abp.DynamicProxy; using Volo.Abp.Threading; namespace Volo.Abp.BackgroundWorkers.Quartz; @@ -26,16 +27,16 @@ public class QuartzPeriodicBackgroundWorkerAdapter : QuartzBackgroundWo public void BuildWorker(IBackgroundWorker worker) { int? period; - var workerType = worker.GetType(); + var workerType = ProxyHelper.GetUnProxiedType(worker); if (worker is AsyncPeriodicBackgroundWorkerBase or PeriodicBackgroundWorkerBase) { - if (typeof(TWorker) != worker.GetType()) + if (typeof(TWorker) != workerType) { throw new ArgumentException($"{nameof(worker)} type is different from the generic type"); } - var timer = worker.GetType().GetProperty("Timer", BindingFlags.Instance | BindingFlags.NonPublic)?.GetValue(worker); + var timer = workerType.GetProperty("Timer", BindingFlags.Instance | BindingFlags.NonPublic)?.GetValue(worker); if (worker is AsyncPeriodicBackgroundWorkerBase) { diff --git a/framework/src/Volo.Abp.Core/Volo/Abp/DynamicProxy/ProxyHelper.cs b/framework/src/Volo.Abp.Core/Volo/Abp/DynamicProxy/ProxyHelper.cs index c35fc0bd2f..2bf147573f 100644 --- a/framework/src/Volo.Abp.Core/Volo/Abp/DynamicProxy/ProxyHelper.cs +++ b/framework/src/Volo.Abp.Core/Volo/Abp/DynamicProxy/ProxyHelper.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Linq; using System.Reflection; @@ -9,7 +9,7 @@ public static class ProxyHelper private const string ProxyNamespace = "Castle.Proxies"; /// - /// Returns dynamic proxy target object if this is a proxied object, otherwise returns the given object. + /// Returns dynamic proxy target object if this is a proxied object, otherwise returns the given object. /// It supports Castle Dynamic Proxies. /// public static object UnProxy(object obj) @@ -33,6 +33,20 @@ public static class ProxyHelper public static Type GetUnProxiedType(object obj) { - return UnProxy(obj).GetType(); + if (obj.GetType().Namespace == ProxyNamespace) + { + var target = UnProxy(obj); + if (target != null) + { + if (target == obj) + { + return obj.GetType().GetTypeInfo().BaseType; + } + + return target.GetType(); + } + } + + return obj.GetType(); } } diff --git a/framework/src/Volo.Abp.Ddd.Application.Contracts/Volo/Abp/Application/Dtos/LimitedResultRequestDto.cs b/framework/src/Volo.Abp.Ddd.Application.Contracts/Volo/Abp/Application/Dtos/LimitedResultRequestDto.cs index dae8bae0fb..d72f7b0a77 100644 --- a/framework/src/Volo.Abp.Ddd.Application.Contracts/Volo/Abp/Application/Dtos/LimitedResultRequestDto.cs +++ b/framework/src/Volo.Abp.Ddd.Application.Contracts/Volo/Abp/Application/Dtos/LimitedResultRequestDto.cs @@ -49,3 +49,51 @@ public class LimitedResultRequestDto : ILimitedResultRequest, IValidatableObject } } } + +/// +/// Simply implements . +/// +[Serializable] +public class ExtensibleLimitedResultRequestDto : ExtensibleEntityDto, ILimitedResultRequest, IValidatableObject +{ + /// + /// Default value: 10. + /// + public static int DefaultMaxResultCount { get; set; } = 10; + + /// + /// Maximum possible value of the . + /// Default value: 1,000. + /// + public static int MaxMaxResultCount { get; set; } = 1000; + + /// + /// Maximum result count should be returned. + /// This is generally used to limit result count on paging. + /// + [Range(1, int.MaxValue)] + public virtual int MaxResultCount { get; set; } = DefaultMaxResultCount; + + public override IEnumerable Validate(ValidationContext validationContext) + { + foreach(var result in base.Validate(validationContext)) + { + yield return result; + } + + if (MaxResultCount > MaxMaxResultCount) + { + var localizer = validationContext.GetRequiredService>(); + + yield return new ValidationResult( + localizer[ + "MaxResultCountExceededExceptionMessage", + nameof(MaxResultCount), + MaxMaxResultCount, + typeof(ExtensibleLimitedResultRequestDto).FullName, + nameof(MaxMaxResultCount) + ], + new[] { nameof(MaxResultCount) }); + } + } +} diff --git a/framework/src/Volo.Abp.Ddd.Application.Contracts/Volo/Abp/Application/Dtos/ListResultDto.cs b/framework/src/Volo.Abp.Ddd.Application.Contracts/Volo/Abp/Application/Dtos/ListResultDto.cs index e5bb81aa2e..361b27faac 100644 --- a/framework/src/Volo.Abp.Ddd.Application.Contracts/Volo/Abp/Application/Dtos/ListResultDto.cs +++ b/framework/src/Volo.Abp.Ddd.Application.Contracts/Volo/Abp/Application/Dtos/ListResultDto.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using Volo.Abp.ObjectExtending; namespace Volo.Abp.Application.Dtos; @@ -7,7 +8,8 @@ namespace Volo.Abp.Application.Dtos; public class ListResultDto : IListResult { /// - public IReadOnlyList Items { + public IReadOnlyList Items + { get { return _items ?? (_items = new List()); } set { _items = value; } } @@ -30,3 +32,32 @@ public class ListResultDto : IListResult Items = items; } } + +[Serializable] +public class ExtensibleListResultDto : ExtensibleObject, IListResult +{ + /// + public IReadOnlyList Items + { + get { return _items ?? (_items = new List()); } + set { _items = value; } + } + private IReadOnlyList _items; + + /// + /// Creates a new object. + /// + public ExtensibleListResultDto() + { + + } + + /// + /// Creates a new object. + /// + /// List of items + public ExtensibleListResultDto(IReadOnlyList items) + { + Items = items; + } +} diff --git a/framework/src/Volo.Abp.Ddd.Application.Contracts/Volo/Abp/Application/Dtos/PagedAndSortedResultRequestDto.cs b/framework/src/Volo.Abp.Ddd.Application.Contracts/Volo/Abp/Application/Dtos/PagedAndSortedResultRequestDto.cs index c48aacd938..229178adbf 100644 --- a/framework/src/Volo.Abp.Ddd.Application.Contracts/Volo/Abp/Application/Dtos/PagedAndSortedResultRequestDto.cs +++ b/framework/src/Volo.Abp.Ddd.Application.Contracts/Volo/Abp/Application/Dtos/PagedAndSortedResultRequestDto.cs @@ -10,3 +10,12 @@ public class PagedAndSortedResultRequestDto : PagedResultRequestDto, IPagedAndSo { public virtual string Sorting { get; set; } } + +/// +/// Simply implements . +/// +[Serializable] +public class ExtensiblePagedAndSortedResultRequestDto : ExtensiblePagedResultRequestDto, IPagedAndSortedResultRequest +{ + public virtual string Sorting { get; set; } +} diff --git a/framework/src/Volo.Abp.Ddd.Application.Contracts/Volo/Abp/Application/Dtos/PagedResultDto.cs b/framework/src/Volo.Abp.Ddd.Application.Contracts/Volo/Abp/Application/Dtos/PagedResultDto.cs index 58ce4117cb..5860a47846 100644 --- a/framework/src/Volo.Abp.Ddd.Application.Contracts/Volo/Abp/Application/Dtos/PagedResultDto.cs +++ b/framework/src/Volo.Abp.Ddd.Application.Contracts/Volo/Abp/Application/Dtos/PagedResultDto.cs @@ -32,3 +32,33 @@ public class PagedResultDto : ListResultDto, IPagedResult TotalCount = totalCount; } } + +/// +/// Implements . +/// +/// Type of the items in the list +[Serializable] +public class ExtensiblePagedResultDto : ExtensibleListResultDto, IPagedResult +{ + /// + public long TotalCount { get; set; } //TODO: Can be a long value..? + + /// + /// Creates a new object. + /// + public ExtensiblePagedResultDto() + { + + } + + /// + /// Creates a new object. + /// + /// Total count of Items + /// List of items in current page + public ExtensiblePagedResultDto(long totalCount, IReadOnlyList items) + : base(items) + { + TotalCount = totalCount; + } +} diff --git a/framework/src/Volo.Abp.Ddd.Application.Contracts/Volo/Abp/Application/Dtos/PagedResultRequestDto.cs b/framework/src/Volo.Abp.Ddd.Application.Contracts/Volo/Abp/Application/Dtos/PagedResultRequestDto.cs index 12fe39ea6e..cfbbc31939 100644 --- a/framework/src/Volo.Abp.Ddd.Application.Contracts/Volo/Abp/Application/Dtos/PagedResultRequestDto.cs +++ b/framework/src/Volo.Abp.Ddd.Application.Contracts/Volo/Abp/Application/Dtos/PagedResultRequestDto.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.ComponentModel.DataAnnotations; namespace Volo.Abp.Application.Dtos; @@ -12,3 +12,13 @@ public class PagedResultRequestDto : LimitedResultRequestDto, IPagedResultReques [Range(0, int.MaxValue)] public virtual int SkipCount { get; set; } } + +/// +/// Simply implements . +/// +[Serializable] +public class ExtensiblePagedResultRequestDto : ExtensibleLimitedResultRequestDto, IPagedResultRequest +{ + [Range(0, int.MaxValue)] + public virtual int SkipCount { get; set; } +} diff --git a/templates/app/aspnet-core/test/MyCompanyName.MyProjectName.Web.Tests/MyProjectNameWebTestModule.cs b/templates/app/aspnet-core/test/MyCompanyName.MyProjectName.Web.Tests/MyProjectNameWebTestModule.cs index ef8c3a4deb..ad63cd93e6 100644 --- a/templates/app/aspnet-core/test/MyCompanyName.MyProjectName.Web.Tests/MyProjectNameWebTestModule.cs +++ b/templates/app/aspnet-core/test/MyCompanyName.MyProjectName.Web.Tests/MyProjectNameWebTestModule.cs @@ -27,7 +27,7 @@ public class MyProjectNameWebTestModule : AbpModule { context.Services.PreConfigure(builder => { - builder.PartManager.ApplicationParts.Add(new AssemblyPart(typeof(MyProjectNameWebModule).Assembly)); + builder.PartManager.ApplicationParts.Add(new CompiledRazorAssemblyPart(typeof(MyProjectNameWebModule).Assembly)); }); }