コード例 #1
0
        public JobAgentServiceConfigurer AddTransientJobAgent(Type type)
        {
            if (!typeof(JobAgent).IsAssignableFrom(type))
            {
                throw new InvalidCastException($"type:{type.Name} is not AssignableFrom typeOf JobAgent");
            }
            if (JobAgentDic.ContainsKey(type))
            {
                return(this);
            }

            var meta = new JobMetaData
            {
                Transien = true
            };
            var hangJobUntilStopAttribute = type.GetCustomAttribute <HangJobUntilStopAttribute>();

            if (hangJobUntilStopAttribute != null)
            {
                meta.Hang = hangJobUntilStopAttribute.On;
            }
            if (JobAgentDic.TryAdd(type, meta))
            {
                Services.AddTransient(type);
            }
            return(this);
        }
コード例 #2
0
        /// <summary>
        /// 这种类型的job 可以并发运行
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public JobAgentServiceConfigurer AddTransientJobAgent <T>() where T : JobAgent
        {
            var type = typeof(T);

            if (JobAgentDic.ContainsKey(type))
            {
                return(this);
            }
            var meta = new JobMetaData
            {
                Transien = true
            };
            var hangJobUntilStopAttribute = type.GetCustomAttribute <HangJobUntilStopAttribute>();

            if (hangJobUntilStopAttribute != null)
            {
                meta.Hang = hangJobUntilStopAttribute.On;
            }
            if (JobAgentDic.TryAdd(type, meta))
            {
                Services.AddTransient <T>();
            }
            return(this);
        }
コード例 #3
0
        public JobAgentServiceConfigurer AddJobAgent(Type type)
        {
            if (!typeof(JobAgent).IsAssignableFrom(type))
            {
                throw new InvalidCastException($"type:{type.Name} is not AssignableFrom typeOf JobAgent");
            }

            if (JobAgentDic.ContainsKey(type))
            {
                throw new InvalidOperationException($"type:{type.Name} is registerd!");
            }

            //这三种标签不可以共存
            var scopedJobAttribute        = type.GetCustomAttribute <TransientJobAttribute>();
            var singletonJobAttribute     = type.GetCustomAttribute <SingletonJobAttribute>();//没有它就是默认的
            var hangJobUntilStopAttribute = type.GetCustomAttribute <HangJobUntilStopAttribute>();

            if (scopedJobAttribute == null && hangJobUntilStopAttribute == null && singletonJobAttribute == null)
            {
                singletonJobAttribute = new SingletonJobAttribute();
            }
            var array = new object[] { scopedJobAttribute, singletonJobAttribute, hangJobUntilStopAttribute };

            if (array.Count(r => r != null) > 1)
            {
                throw new InvalidCastException($"type:{type.Name} can not init with mulit xxxxJobAttribute!");
            }

            if (!(array.FirstOrDefault(r => r != null) is JobAttribute regesterMeta))
            {
                throw new InvalidCastException($"type:{type.Name} is not AssignableFrom typeOf JobAttribute");
            }

            var meta = new JobMetaData
            {
                RegisterId         = regesterMeta.RegisterId,
                RegisterName       = regesterMeta.RegisterName,
                EnableAutoRegister = regesterMeta.enableAutoRegister
            };

            if (hangJobUntilStopAttribute != null)
            {
                meta.Hang = hangJobUntilStopAttribute.On;
            }

            if (scopedJobAttribute != null)
            {
                meta.Transien = true;
                if (JobAgentDic.TryAdd(type, meta))
                {
                    Services.AddTransient(type);
                }
                return(this);
            }

            meta.Transien = false;
            if (JobAgentDic.TryAdd(type, meta))
            {
                Services.AddSingleton(type);
            }
            return(this);
        }