Example #1
0
        private ActivatedContext ActivateJob(JobActivatorScope scope, ActivationContext context)
        {
            object instance;

            try
            {
                instance = scope.Resolve(context.BackgroundJob.Job.Type);
            }
            catch (Exception e)
            {
                return(new ActivatedContext(context, null, e));
            }

            return(new ActivatedContext(context, instance, null));
        }
        /// <summary>
        /// 重写以实现构建服务并设置各个平台的Resolver
        /// </summary>
        /// <param name="services">服务映射信息集合</param>
        /// <param name="assemblies">要检索的程序集集合</param>
        /// <returns>服务提供者</returns>
        protected override IServiceProvider BuildAndSetResolver(IServiceCollection services, Assembly[] assemblies)
        {
            ContainerBuilder builder = new ContainerBuilder();

            builder.Populate(services);
            IContainer   container = builder.Build();
            JobActivator activator = new AutofacJobActivator(container);

            JobActivator.Current = activator;
            HangfireIocResolver.LifetimeResolveFunc = type =>
            {
                JobActivatorScope scope = CallContext.LogicalGetData(AutofacJobActivator.LifetimeScopeKey) as JobActivatorScope;
                if (scope == null)
                {
                    return(null);
                }
                return(scope.Resolve(type));
            };
            return((IServiceProvider)activator.ActivateJob(typeof(IServiceProvider)));
        }
Example #3
0
        private object CreateInstance(JobActivatorScope scope, ActivationContext context)
        {
            var filterInfo = new JobFilterInfo(_filterProvider.GetFilters(context.BackgroundJob.Job));

            BeforeActivation(context, filterInfo.ActivationFilters);
            var activatedContext = ActivateJob(scope, context);

            AfterActivation(activatedContext, filterInfo.ActivationFilters);

            if (activatedContext.Exception != null)
            {
                throw activatedContext.Exception;
            }

            if (activatedContext.Instance == null)
            {
                throw new InvalidOperationException(
                          String.Format("JobActivator returned NULL instance of the '{0}' type.", context.BackgroundJob.Job.Type));
            }

            return(activatedContext.Instance);
        }
Example #4
0
        internal PerformContext(
            [NotNull] IStorageConnection connection,
            [NotNull] BackgroundJob backgroundJob,
            [NotNull] IJobCancellationToken cancellationToken,
            [NotNull] IProfiler profiler,
            [NotNull] JobActivatorScope scope
            )
        {
            if (connection == null)
            {
                throw new ArgumentNullException(nameof(connection));
            }
            if (backgroundJob == null)
            {
                throw new ArgumentNullException(nameof(backgroundJob));
            }
            if (cancellationToken == null)
            {
                throw new ArgumentNullException(nameof(cancellationToken));
            }
            if (profiler == null)
            {
                throw new ArgumentNullException(nameof(profiler));
            }
            if (scope == null)
            {
                throw new ArgumentNullException(nameof(scope));
            }

            Connection        = connection;
            BackgroundJob     = backgroundJob;
            CancellationToken = cancellationToken;
            Profiler          = profiler;
            _scope            = scope;

            Items = new Dictionary <string, object>();
        }