Пример #1
0
 /// <summary>
 /// 判断订阅列表中是否存在订阅引用。
 /// </summary>
 /// <param name="descriptor">一个订阅引用。</param>
 /// <returns>如果过订阅列表中存在 <paramref name="descriptor"/>,则返回 <see langword="true"/>。</returns>
 protected virtual bool ContainsCore(EventSubscriptionDescriptor descriptor)
 {
     lock (Subscriptions)
     {
         return(Subscriptions.Any(evt => evt.Identity.CaseSensitiveEquals(descriptor.Identity)));
     }
 }
        /// <summary>
        /// 从一个订阅类型创建事件订阅的描述信息。
        /// </summary>
        /// <param name="dependencyType">依赖项声明类型。</param>
        /// <param name="lifetime">依赖项生命周期。</param>
        /// <param name="subscriptionMethod">订阅方法。</param>
        /// <returns></returns>
        public static IEnumerable <EventSubscriptionDescriptor> FromMethod(Type dependencyType, ServiceLifetime lifetime, MethodInfo subscriptionMethod)
        {
            Guard.ArgumentNotNull(subscriptionMethod, nameof(subscriptionMethod));

            var attributes = subscriptionMethod.GetCustomAttributes <EventSubscriptionAttribute>(false);

            if (attributes.IsNullOrEmpty())
            {
                throw new ArgumentException($"事件回调类型 {subscriptionMethod.Name} 缺少订阅描述信息,可能由于没有应用 {nameof(EventSubscriptionAttribute)} 属性。", nameof(subscriptionMethod));
            }
            foreach (var attribute in attributes)
            {
                if (attribute.DeclaringDependencyType == null || attribute.DeclaringDependencyType.Equals(dependencyType))
                {
                    var descriptor = new EventSubscriptionDescriptor();

                    descriptor.MethodName          = subscriptionMethod.Name;
                    descriptor.Caller              = BuildDelegate(subscriptionMethod);
                    descriptor.ThreadOptions       = attribute.ThreadOptions;
                    descriptor.Policy              = attribute.Policy;
                    descriptor.EventName           = attribute.EventName;
                    descriptor.Lifetime            = lifetime;
                    descriptor.DependencyType      = attribute.DeclaringDependencyType ?? dependencyType;
                    descriptor.IsDeclareDependency = attribute.DeclaringDependencyType != null;

                    descriptor.Identity = CryptoHelper.Encrypt32MD5($"{descriptor.EventName}:{descriptor.DependencyType.AssemblyQualifiedName}{subscriptionMethod.DeclaringType.AssemblyQualifiedName}.{subscriptionMethod.Name}");

                    yield return(descriptor);
                }
            }
        }
Пример #3
0
        protected internal virtual void SubscribeCore(EventSubscriptionDescriptor eventSubscription)
        {
            Guard.ArgumentNotNull(eventSubscription, nameof(eventSubscription));

            lock (Subscriptions)
            {
                Subscriptions.Add(eventSubscription);
            }
        }
Пример #4
0
        private void ExecuteAndReleaseHandler(object sender, object args, EventSubscriptionDescriptor s)
        {
            IServiceScope scope = null;

            try
            {
                object instance = null;
                if (s.Lifetime == ServiceLifetime.Scoped) //对于生命周期为一个下文的我们先尝试查找应用程序中的上下文尝试来加载。
                {
                    IWorkContextAccessor workContext = _serviceProvider.GetRequiredService <IWorkContextAccessor>();
                    var context = workContext.GetContext();
                    if (context == null)
                    {
                        IServiceScopeFactory fac = _serviceProvider.GetRequiredService <IServiceScopeFactory>();
                        scope = fac.CreateScope();
                        Type dependenyType = s.DependencyType;
                        instance = scope.ServiceProvider.GetRequiredService(s.DependencyType);
                    }
                    else
                    {
                        instance = context.ResolveRequired(s.DependencyType);
                    }
                }
                else
                {
                    instance = _serviceProvider.GetRequiredService(s.DependencyType);
                }
                try
                {
                    s.Caller.DynamicInvoke(instance, sender, args);
                }
                catch (ArgumentException)
                {
                    throw new SchubertException($@"事件订阅方法中的事件参数和事件发布的参数不匹配。{System.Environment.NewLine} 事件 ""{s.EventName}"" 要求订阅方法 {s.DependencyType}.{s.MethodName} 签名必须为 void (object sender, {args?.GetType() ?? typeof(object)})");
                }
                if (s.Policy == SubscriptionPolicy.Once)
                {
                    this.UnsubscribeCore(s);
                }
            }
            finally
            {
                if (scope != null)
                {
                    scope.Dispose();
                }
                if (s.Lifetime == ServiceLifetime.Transient)
                {
                    IDisposable disposable = sender as IDisposable;
                    if (disposable != null)
                    {
                        disposable.Dispose();
                    }
                }
            }
        }
Пример #5
0
 protected internal virtual void UnsubscribeCore(EventSubscriptionDescriptor descriptor)
 {
     lock (Subscriptions)
     {
         EventSubscriptionDescriptor subscription = Subscriptions.FirstOrDefault(evt => evt.Identity.CaseSensitiveEquals(descriptor.Identity));
         if (subscription != null)
         {
             Subscriptions.Remove(subscription);
         }
     }
 }