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
 public object Resolve(Type type)
 {
     return(_scope.Resolve(type));
 }