Commit 88875948 by dingsongjie

修复 bug

parent 4ffee379
......@@ -2,11 +2,13 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
namespace Backet.Api.Domain.Event
{
public class BacketCreatedEvent:IEvent
[EventInfo(EventName = "Backet")]
public class BacketCreatedEvent : IEvent
{
public string BacketId { get; set; }
}
......
......@@ -5,7 +5,7 @@ using System.Text;
namespace Pole.Core.EventBus.Event
{
[AttributeUsage(AttributeTargets.Class)]
public class EventAttribute: Attribute
public class EventInfoAttribute: Attribute
{
public string EventName { get; set; }
}
......
......@@ -8,6 +8,7 @@ using Microsoft.Extensions.DependencyInjection;
using Pole.Core.EventBus.EventHandler;
using System.Linq;
using Pole.Core.Exceptions;
using Pole.Core.EventBus.Event;
namespace Pole.Core.EventBus
{
......@@ -16,17 +17,28 @@ namespace Pole.Core.EventBus
readonly ConcurrentDictionary<string, List<object>> unitDict = new ConcurrentDictionary<string, List<object>>();
public ObserverUnitContainer(IServiceProvider serviceProvider)
{
var eventHandlerList = new List<(Type, EventHandlerAttribute)>();
var eventHandlerList = new List<(Type, EventInfoAttribute)>();
foreach (var assembly in AssemblyHelper.GetAssemblies(serviceProvider.GetService<ILogger<ObserverUnitContainer>>()))
{
foreach (var type in assembly.GetTypes().Where(m => typeof(IPoleEventHandler).IsAssignableFrom(m) && m.IsClass && !m.IsAbstract && !typeof(Orleans.Runtime.GrainReference).IsAssignableFrom(m)))
{
var attribute = type.GetCustomAttributes(typeof(EventHandlerAttribute), false).FirstOrDefault();
var eventHandlerInterface = type.GetInterfaces().FirstOrDefault(type => typeof(IPoleEventHandler).IsAssignableFrom(type) && !type.IsGenericType);
var basePoleEventHandlerInterface= eventHandlerInterface.GetInterfaces().FirstOrDefault(m=>m.IsGenericType);
if (basePoleEventHandlerInterface == null)
{
throw new PoleEventHandlerImplementException("PoleEventHandler interface must Inherited from IPoleEventHandler<TEvent>");
}
var eventType= basePoleEventHandlerInterface.GetGenericArguments().FirstOrDefault();
if (eventType == null)
{
throw new PoleEventHandlerImplementException("PoleEventHandler interface must Inherited from IPoleEventHandler<TEvent>");
}
var attribute = eventType.GetCustomAttributes(typeof(EventInfoAttribute), false).FirstOrDefault();
if (attribute != null)
{
eventHandlerList.Add((eventHandlerInterface, (EventHandlerAttribute)attribute));
eventHandlerList.Add((eventHandlerInterface, (EventInfoAttribute)attribute));
}
else
{
......
......@@ -25,8 +25,8 @@ namespace Pole.Core.Serialization
foreach (var type in assembly.GetTypes().Where(m => baseEventType.IsAssignableFrom(m)&&!m.IsAbstract))
{
var eventCode = type.FullName;
var eventAttribute = type.GetCustomAttributes(typeof(EventAttribute),false).FirstOrDefault();
if (eventAttribute is EventAttribute attribute )
var eventAttribute = type.GetCustomAttributes(typeof(EventInfoAttribute),false).FirstOrDefault();
if (eventAttribute is EventInfoAttribute attribute )
{
eventCode = attribute.EventName;
}
......
......@@ -38,8 +38,8 @@ namespace Pole.EventBus.RabbitMQ
}
public async Task AutoRegister()
{
var eventList = new List<(Type type, EventAttribute config)>();
var evenHandlertList = new List<(Type type, EventHandlerAttribute config)>();
var eventList = new List<(Type type, EventInfoAttribute config)>();
var evenHandlertList = new List<(Type type, EventInfoAttribute config)>();
AddEventAndEventHandlerInfoList(eventList, evenHandlertList);
foreach (var (type, config) in eventList)
{
......@@ -110,21 +110,21 @@ namespace Pole.EventBus.RabbitMQ
#region helpers
private void AddEventAndEventHandlerInfoList(List<(Type type, EventAttribute config)> eventList, List<(Type type, EventHandlerAttribute config)> eventHandlertList)
private void AddEventAndEventHandlerInfoList(List<(Type type, EventInfoAttribute config)> eventList, List<(Type type, EventInfoAttribute config)> eventHandlertList)
{
foreach (var assembly in AssemblyHelper.GetAssemblies(serviceProvider.GetService<ILogger<EventBusContainer>>()))
{
foreach (var type in assembly.GetTypes().Where(m => typeof(IEvent).IsAssignableFrom(m) && m.IsClass))
{
var attribute = type.GetCustomAttributes(typeof(EventAttribute), false).FirstOrDefault();
var attribute = type.GetCustomAttributes(typeof(EventInfoAttribute), false).FirstOrDefault();
if (attribute != null)
{
eventList.Add((type, (EventAttribute)attribute));
eventList.Add((type, (EventInfoAttribute)attribute));
}
else
{
eventList.Add((type, new EventAttribute() { EventName = type.FullName }));
eventList.Add((type, new EventInfoAttribute() { EventName = type.FullName }));
}
}
}
......@@ -132,13 +132,25 @@ namespace Pole.EventBus.RabbitMQ
foreach (var assembly in AssemblyHelper.GetAssemblies(serviceProvider.GetService<ILogger<EventBusContainer>>()))
{
foreach (var type in assembly.GetTypes().Where(m => typeof(IPoleEventHandler).IsAssignableFrom(m) && m.IsClass && !m.IsAbstract&&!typeof(Orleans.Runtime.GrainReference).IsAssignableFrom(m)))
foreach (var type in assembly.GetTypes().Where(m => typeof(IPoleEventHandler).IsAssignableFrom(m) && m.IsClass && !m.IsAbstract && !typeof(Orleans.Runtime.GrainReference).IsAssignableFrom(m)))
{
var attribute = type.GetCustomAttributes(typeof(EventHandlerAttribute), false).FirstOrDefault();
var eventHandlerInterface = type.GetInterfaces().FirstOrDefault(type => typeof(IPoleEventHandler).IsAssignableFrom(type) && !type.IsGenericType);
var basePoleEventHandlerInterface = eventHandlerInterface.GetInterfaces().FirstOrDefault(m => m.IsGenericType);
if (basePoleEventHandlerInterface == null)
{
throw new PoleEventHandlerImplementException("PoleEventHandler interface must Inherited from IPoleEventHandler<TEvent>");
}
var eventType = basePoleEventHandlerInterface.GetGenericArguments().FirstOrDefault();
if (eventType == null)
{
throw new PoleEventHandlerImplementException("PoleEventHandler interface must Inherited from IPoleEventHandler<TEvent>");
}
var attribute = eventType.GetCustomAttributes(typeof(EventInfoAttribute), false).FirstOrDefault();
if (attribute != null)
{
eventHandlertList.Add((type, (EventHandlerAttribute)attribute));
eventHandlertList.Add((eventHandlerInterface, (EventInfoAttribute)attribute));
}
else
{
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment