예제 #1
0
    public static MicroserviceFrameworkBuilder RegisterEventHandlers(this MicroserviceFrameworkBuilder builder)
    {
        MicroserviceFrameworkLoaderContext.Get(builder.Services).ResolveType += type =>
        {
            var interfaces            = type.GetInterfaces();
            var handlerInterfaceTypes = interfaces
                                        .Where(@interface => @interface.IsGenericType &&
                                               EventHandlerBaseType == @interface.GetGenericTypeDefinition())
                                        .ToList();

            if (handlerInterfaceTypes.Count == 0)
            {
                return;
            }

            foreach (var handlerInterfaceType in handlerInterfaceTypes)
            {
                var eventType = handlerInterfaceType.GenericTypeArguments[0];
                if (!eventType.IsEvent())
                {
                    throw new MicroserviceFrameworkException($"{eventType} 不是合法的事件类型");
                }

                // 消息队列,得知道系统实现了哪些 EventHandler 才去监听对应的 Topic,所以必须先注册监听。
                EventSubscriptionManager.Register(eventType, handlerInterfaceType);
                // 每次收到的消息都是独立的 Scope
                ServiceCollectionUtilities.TryAdd(builder.Services,
                                                  new ServiceDescriptor(handlerInterfaceType, type, ServiceLifetime.Scoped));
            }
        };

        return(builder);
    }
예제 #2
0
    /// <summary>
    /// 以类型实现的接口进行服务添加,需排除
    /// <see cref="IDisposable"/>等非业务接口,如无接口则注册自身
    /// </summary>
    /// <param name="services">服务映射信息集合</param>
    /// <param name="implementationType">要注册的实现类型集合</param>
    /// <param name="lifetime">注册的生命周期类型</param>
    private static void RegisterDependencyInjection(this IServiceCollection services,
                                                    Type implementationType, ServiceLifetime lifetime)
    {
        if (implementationType.IsAbstract || implementationType.IsInterface)
        {
            return;
        }

        // 1. 注册类型本身
        ServiceCollectionUtilities.TryAdd(services,
                                          new ServiceDescriptor(implementationType, implementationType, lifetime));

        var interfaceTypes = implementationType.GetInterfaces(
            typeof(ITransientDependency),
            typeof(ISingletonDependency),
            typeof(IScopeDependency),
            typeof(IDomainService),
            typeof(ISession),
            typeof(IRequest),
            typeof(IRequest <>)).ToArray();

        if (interfaceTypes.Length == 0)
        {
            return;
        }

        for (var i = 0; i < interfaceTypes.Length; i++)
        {
            var interfaceType = interfaceTypes[i];

            // 瞬时生命周期每次获取对象都是新的,因此实现的接口每个注册一次即可
            if (lifetime == ServiceLifetime.Transient)
            {
                ServiceCollectionUtilities.TryAdd(services,
                                                  new ServiceDescriptor(interfaceType, implementationType, ServiceLifetime.Transient));
            }
            else
            {
                if (i == 0)
                {
                    ServiceCollectionUtilities.TryAdd(services,
                                                      new ServiceDescriptor(interfaceType, implementationType, lifetime));
                }
                else
                {
                    //有多个接口时,后边的接口注册使用第一个接口的实例,保证同个实现类的多个接口获得同一个实例
                    var firstInterfaceType = interfaceTypes[0];
                    ServiceCollectionUtilities.TryAdd(services, new ServiceDescriptor(interfaceType,
                                                                                      provider => provider.GetService(firstInterfaceType), lifetime));
                }
            }
        }
    }