Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 11 additions & 22 deletions Masa.Contrib.sln
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,6 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Masa.Contrib.Dispatcher.Eve
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Masa.Contrib.Dispatcher.Events.OrderEqualBySaga.Tests", "test\Masa.Contrib.Dispatcher.Events.OrderEqualBySaga.Tests\Masa.Contrib.Dispatcher.Events.OrderEqualBySaga.Tests.csproj", "{7D083C64-FF32-43C4-A82C-32C4A4EC1414}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Masa.Contrib.Dispatcher.Events.OrderLessThanZeroByFeature.Tests", "test\Masa.Contrib.Dispatcher.Events.OrderLessThanZeroByFeature.Tests\Masa.Contrib.Dispatcher.Events.OrderLessThanZeroByFeature.Tests.csproj", "{DE2E92D3-929F-40E7-B8D0-502A57170A3E}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Masa.Contrib.Dispatcher.Events.OrderLessThanZeroBySaga.Tests", "test\Masa.Contrib.Dispatcher.Events.OrderLessThanZeroBySaga.Tests\Masa.Contrib.Dispatcher.Events.OrderLessThanZeroBySaga.Tests.csproj", "{0B139C21-8AFD-41CD-82FE-36E64FDEDE50}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Masa.Contrib.Dispatcher.Events.Tests", "test\Masa.Contrib.Dispatcher.Events.Tests\Masa.Contrib.Dispatcher.Events.Tests.csproj", "{3870E8F1-B269-425D-8B03-58835FD53610}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Masa.Contrib.Dispatcher.IntegrationEvents.EventLogs.EF", "src\Dispatcher\Masa.Contrib.Dispatcher.IntegrationEvents.EventLogs.EF\Masa.Contrib.Dispatcher.IntegrationEvents.EventLogs.EF.csproj", "{519A99D2-8094-48EC-A888-C0B4E017A4C1}"
Expand Down Expand Up @@ -134,6 +130,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Masa.BuildingBlocks.SearchE
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Masa.BuildingBlocks.Service.MinimalAPIs", "src\BuildingBlocks\MASA.BuildingBlocks\src\Service\Masa.BuildingBlocks.Service.MinimalAPIs\Masa.BuildingBlocks.Service.MinimalAPIs.csproj", "{E72E105D-B15F-4D69-9A13-CAA49D4889D6}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Masa.Contrib.Dispatcher.Events.HandlerOrder.Tests", "test\Masa.Contrib.Dispatcher.Events.HandlerOrder.Tests\Masa.Contrib.Dispatcher.Events.HandlerOrder.Tests.csproj", "{4A052E17-4D9E-41EF-89A5-73B917053F8E}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -206,22 +204,6 @@ Global
{7D083C64-FF32-43C4-A82C-32C4A4EC1414}.Release|Any CPU.Build.0 = Release|Any CPU
{7D083C64-FF32-43C4-A82C-32C4A4EC1414}.Release|x64.ActiveCfg = Release|Any CPU
{7D083C64-FF32-43C4-A82C-32C4A4EC1414}.Release|x64.Build.0 = Release|Any CPU
{DE2E92D3-929F-40E7-B8D0-502A57170A3E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{DE2E92D3-929F-40E7-B8D0-502A57170A3E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{DE2E92D3-929F-40E7-B8D0-502A57170A3E}.Debug|x64.ActiveCfg = Debug|Any CPU
{DE2E92D3-929F-40E7-B8D0-502A57170A3E}.Debug|x64.Build.0 = Debug|Any CPU
{DE2E92D3-929F-40E7-B8D0-502A57170A3E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{DE2E92D3-929F-40E7-B8D0-502A57170A3E}.Release|Any CPU.Build.0 = Release|Any CPU
{DE2E92D3-929F-40E7-B8D0-502A57170A3E}.Release|x64.ActiveCfg = Release|Any CPU
{DE2E92D3-929F-40E7-B8D0-502A57170A3E}.Release|x64.Build.0 = Release|Any CPU
{0B139C21-8AFD-41CD-82FE-36E64FDEDE50}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{0B139C21-8AFD-41CD-82FE-36E64FDEDE50}.Debug|Any CPU.Build.0 = Debug|Any CPU
{0B139C21-8AFD-41CD-82FE-36E64FDEDE50}.Debug|x64.ActiveCfg = Debug|Any CPU
{0B139C21-8AFD-41CD-82FE-36E64FDEDE50}.Debug|x64.Build.0 = Debug|Any CPU
{0B139C21-8AFD-41CD-82FE-36E64FDEDE50}.Release|Any CPU.ActiveCfg = Release|Any CPU
{0B139C21-8AFD-41CD-82FE-36E64FDEDE50}.Release|Any CPU.Build.0 = Release|Any CPU
{0B139C21-8AFD-41CD-82FE-36E64FDEDE50}.Release|x64.ActiveCfg = Release|Any CPU
{0B139C21-8AFD-41CD-82FE-36E64FDEDE50}.Release|x64.Build.0 = Release|Any CPU
{3870E8F1-B269-425D-8B03-58835FD53610}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{3870E8F1-B269-425D-8B03-58835FD53610}.Debug|Any CPU.Build.0 = Debug|Any CPU
{3870E8F1-B269-425D-8B03-58835FD53610}.Debug|x64.ActiveCfg = Debug|Any CPU
Expand Down Expand Up @@ -494,6 +476,14 @@ Global
{E72E105D-B15F-4D69-9A13-CAA49D4889D6}.Release|Any CPU.Build.0 = Release|Any CPU
{E72E105D-B15F-4D69-9A13-CAA49D4889D6}.Release|x64.ActiveCfg = Release|Any CPU
{E72E105D-B15F-4D69-9A13-CAA49D4889D6}.Release|x64.Build.0 = Release|Any CPU
{4A052E17-4D9E-41EF-89A5-73B917053F8E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{4A052E17-4D9E-41EF-89A5-73B917053F8E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{4A052E17-4D9E-41EF-89A5-73B917053F8E}.Debug|x64.ActiveCfg = Debug|Any CPU
{4A052E17-4D9E-41EF-89A5-73B917053F8E}.Debug|x64.Build.0 = Debug|Any CPU
{4A052E17-4D9E-41EF-89A5-73B917053F8E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{4A052E17-4D9E-41EF-89A5-73B917053F8E}.Release|Any CPU.Build.0 = Release|Any CPU
{4A052E17-4D9E-41EF-89A5-73B917053F8E}.Release|x64.ActiveCfg = Release|Any CPU
{4A052E17-4D9E-41EF-89A5-73B917053F8E}.Release|x64.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -520,8 +510,6 @@ Global
{62760E2C-D3D6-4824-997F-35033E6EB92C} = {2BE750A5-8AC7-457C-9BB2-6E3D5E2D23B8}
{965C85E2-D94E-43DE-BFC2-B9D157242EBB} = {2BE750A5-8AC7-457C-9BB2-6E3D5E2D23B8}
{7D083C64-FF32-43C4-A82C-32C4A4EC1414} = {2BE750A5-8AC7-457C-9BB2-6E3D5E2D23B8}
{DE2E92D3-929F-40E7-B8D0-502A57170A3E} = {2BE750A5-8AC7-457C-9BB2-6E3D5E2D23B8}
{0B139C21-8AFD-41CD-82FE-36E64FDEDE50} = {2BE750A5-8AC7-457C-9BB2-6E3D5E2D23B8}
{3870E8F1-B269-425D-8B03-58835FD53610} = {2BE750A5-8AC7-457C-9BB2-6E3D5E2D23B8}
{519A99D2-8094-48EC-A888-C0B4E017A4C1} = {FBD326D3-E59C-433E-A88E-14E179E3093D}
{E946A129-34ED-4069-B44E-EC7B98751006} = {FBD326D3-E59C-433E-A88E-14E179E3093D}
Expand Down Expand Up @@ -560,6 +548,7 @@ Global
{316B1D0A-9CF7-4E5C-A39A-8A389B075A19} = {DC578D74-98F0-4F19-A230-CFA8DAEE0AF1}
{2F4986D6-3F56-4C05-8A1D-399594F96093} = {DC578D74-98F0-4F19-A230-CFA8DAEE0AF1}
{E72E105D-B15F-4D69-9A13-CAA49D4889D6} = {DC578D74-98F0-4F19-A230-CFA8DAEE0AF1}
{4A052E17-4D9E-41EF-89A5-73B917053F8E} = {2BE750A5-8AC7-457C-9BB2-6E3D5E2D23B8}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {40383055-CC50-4600-AD9A-53C14F620D03}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,32 +27,31 @@ public EventHandlerAttribute(int order, bool enableRetry, int retryTimes) : this

}

public EventHandlerAttribute(int order, bool enableRetry, bool isCancel) : this(order, enableRetry, isCancel, enableRetry ? DefaultRetryCount : 0)
public EventHandlerAttribute(int order, bool enableRetry, bool isCancel) : this(order, enableRetry, isCancel,
enableRetry ? DefaultRetryCount : 0)
{

}

public EventHandlerAttribute(int order, bool enableRetry, bool isCancel, int retryTimes) : this(order, FailureLevels.Throw, enableRetry, retryTimes, isCancel)
public EventHandlerAttribute(int order, bool enableRetry, bool isCancel, int retryTimes) : this(order, FailureLevels.Throw, enableRetry,
retryTimes, isCancel)
{

}

public EventHandlerAttribute(int order, FailureLevels failureLevels, bool enableRetry) : this(order, failureLevels, enableRetry, enableRetry ? DefaultRetryCount : 0)
public EventHandlerAttribute(int order, FailureLevels failureLevels, bool enableRetry) : this(order, failureLevels, enableRetry,
enableRetry ? DefaultRetryCount : 0)
{

}

public EventHandlerAttribute(int order, FailureLevels failureLevels, bool enableRetry, bool isCancel) : this(order, failureLevels, enableRetry, enableRetry ? DefaultRetryCount : 0, isCancel)
public EventHandlerAttribute(int order, FailureLevels failureLevels, bool enableRetry, bool isCancel) : this(order, failureLevels,
enableRetry, enableRetry ? DefaultRetryCount : 0, isCancel)
{

}

public EventHandlerAttribute(int order, FailureLevels failureLevels, bool enableRetry, int retryTimes) : this(order, failureLevels, enableRetry, retryTimes, false)
{

}

public EventHandlerAttribute(int order, FailureLevels failureLevels, bool enableRetry, int retryTimes, bool isCancel)
public EventHandlerAttribute(int order, FailureLevels failureLevels, bool enableRetry, int retryTimes, bool isCancel = false)
{
Order = order;
FailureLevels = failureLevels;
Expand All @@ -68,11 +67,23 @@ public EventHandlerAttribute(int order, FailureLevels failureLevels, bool enable

private const int DefaultOrder = int.MaxValue;

private int _order;

/// <summary>
/// Used to control the order in which methods are executed, in ascending order. default is 100
/// Must be greater than or equal to 0
/// </summary>
public int Order { get; set; }
public int Order
{
get => _order;
set
{
if (value < 0)
throw new ArgumentOutOfRangeException("The order must be greater than or equal to 0");

_order = value;
}
}

public FailureLevels FailureLevels { get; set; }

Expand All @@ -96,11 +107,11 @@ public EventHandlerAttribute(int order, FailureLevels failureLevels, bool enable

internal int ActualRetryTimes => EnableRetry ? RetryTimes : 0;

internal TaskInvokeDelegate InvokeDelegate { get; private set; }
internal TaskInvokeDelegate? InvokeDelegate { get; private set; }

private object Instance { get; set; } = default!;

private object EventHandler { get; set; }
private object? EventHandler { get; set; }

internal bool IsEventHandler => FailureLevels == FailureLevels.Throw || FailureLevels == FailureLevels.ThrowAndCancel;

Expand Down Expand Up @@ -129,7 +140,7 @@ private async Task ExcuteSagaAction<TEvent>(IServiceProvider serviceProvider, TE
if (EventHandler == null)
{
var handlers = serviceProvider.GetServices<IEventHandler<TEvent>>();
var handler = handlers.Where(x => x.GetType().Equals(InstanceType)).FirstOrDefault()!;
var handler = handlers.FirstOrDefault(x => x.GetType() == InstanceType)!;
EventHandler = handler;
}
await ((IEventHandler<TEvent>)EventHandler).HandleAsync(@event);
Expand All @@ -139,12 +150,13 @@ private async Task ExcuteSagaAction<TEvent>(IServiceProvider serviceProvider, TE
if (EventHandler == null)
{
var handlers = serviceProvider.GetServices<ISagaEventHandler<TEvent>>();
var handler = handlers.Where(x => x.GetType().Equals(InstanceType)).FirstOrDefault()!;
var handler = handlers.FirstOrDefault(x => x.GetType() == InstanceType)!;
EventHandler = handler;
}
await ((ISagaEventHandler<TEvent>)EventHandler).CancelAsync(@event);
}
}

internal bool IsHandlerMissing(int maxCancelOrder) => FailureLevels == FailureLevels.ThrowAndCancel && Order < maxCancelOrder || FailureLevels == FailureLevels.Throw && Order <= maxCancelOrder;
internal bool IsHandlerMissing(int maxCancelOrder) => FailureLevels == FailureLevels.ThrowAndCancel && Order < maxCancelOrder ||
FailureLevels == FailureLevels.Throw && Order <= maxCancelOrder;
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ private void Add(Type keyEventType,
dispatchRelativeNetwork.Add(keyEventType, new List<EventHandlerAttribute>());
}

if (!dispatchRelativeNetwork[keyEventType].Any(x => x.ActionMethodInfo.Equals(handlers.ActionMethodInfo) && x.InstanceType.Equals(handlers.InstanceType)))
if (!dispatchRelativeNetwork[keyEventType].Any(x => x.ActionMethodInfo.Equals(handlers.ActionMethodInfo) && x.InstanceType == handlers.InstanceType))
{
dispatchRelativeNetwork[keyEventType].Add(handlers);
}
Expand All @@ -51,7 +51,7 @@ public void Build()
{
if (CancelRelationNetwork.TryGetValue(relation.Key, out List<EventHandlerAttribute>? cancelRelations))
{
var cancelHandlers = cancelRelations.TakeWhile(handler => relationOption.IsCancelHandler(relationOption.Handler)).Reverse().ToList();
var cancelHandlers = cancelRelations.TakeWhile(cancelHandler => relationOption.IsCancelHandler(cancelHandler)).Reverse().ToList();
relationOption.AddCancelHandler(cancelHandlers);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,19 @@ namespace Masa.Contrib.Dispatcher.Events.Internal.Dispatch;

internal class Dispatcher : DispatcherBase
{
public Dispatcher(IServiceCollection services, Assembly[] assemblies, bool forceInit = false) : base(services, assemblies, forceInit) { }
public Dispatcher(IServiceCollection services, Assembly[] assemblies, bool forceInit = false) : base(services, assemblies, forceInit)
{
}

public Dispatcher Build(ServiceLifetime lifetime)
{
foreach (var assembly in _assemblies)
foreach (var assembly in Assemblies)
{
AddRelationNetwork(assembly);
}
foreach (var dispatchInstance in GetAddServiceTypeList())
{
_services.Add(dispatchInstance, dispatchInstance, lifetime);
Services.Add(dispatchInstance, dispatchInstance, lifetime);
}
Build();
return this;
Expand All @@ -24,7 +26,7 @@ private void AddRelationNetwork(Assembly assembly)
{
if (!type.IsConcrete())
{
continue;//Handler and Cancel must be normal classes, not abstract classes or interfaces
continue; //Handler and Cancel must be normal classes, not abstract classes or interfaces
}

foreach (var method in type.GetMethods())
Expand All @@ -36,33 +38,32 @@ private void AddRelationNetwork(Assembly assembly)

private void AddRelationNetwork(Type type, MethodInfo method)
{
var attribute = method.GetCustomAttributes(typeof(EventHandlerAttribute), true).FirstOrDefault();
var handler = attribute as EventHandlerAttribute;
if (attribute is not null && handler is not null)
try
{
var parameters = method.GetParameters();
if (parameters == null ||
parameters.Length != 1 ||
!parameters.Any(parameter => typeof(IEvent).IsAssignableFrom(parameter.ParameterType)))
{
throw new ArgumentOutOfRangeException(string.Format("[{0}] must have only one argument and inherit from Event", method.Name));
}
if (IsSagaMode(type, method))
var attribute = method.GetCustomAttributes(typeof(EventHandlerAttribute), true).FirstOrDefault();
if (attribute is not null && attribute is EventHandlerAttribute handler)
{
return;
}
var parameters = method.GetParameters();

if (handler.Order < 0)
{
throw new ArgumentOutOfRangeException("The order must be greater than or equal to 0");
}
if (parameters == null || parameters.Length != 1 ||
!parameters.Any(parameter => typeof(IEvent).IsAssignableFrom(parameter.ParameterType)))
throw new ArgumentOutOfRangeException($"[{method.Name}] must have only one argument and inherit from Event");

if (IsSagaMode(type, method))
return;

var parameter = parameters.FirstOrDefault()!;
handler.ActionMethodInfo = method;
handler.InstanceType = type;
handler.EventType = parameter.ParameterType;
handler.BuildExpression();
AddRelationNetwork(parameter.ParameterType, handler);
var parameter = parameters.FirstOrDefault()!;
handler.ActionMethodInfo = method;
handler.InstanceType = type;
handler.EventType = parameter.ParameterType;
handler.BuildExpression();
AddRelationNetwork(parameter.ParameterType, handler);
}
}
catch (Exception ex)
{
Logger?.LogError($"Dispatcher: Failed to get EventBus network, type name: [{type.FullName ?? type.Name}], method: [{method.Name}]", ex);
throw;
}
}
}
Loading