hangfire 초기화 작업 메커니즘

9493 단어 초기화

hangfire 초기화 구성 방법: UseHangfire.
   public static class OwinBootstrapper

    {

        /// <summary>

        /// Bootstraps Hangfire components using the given configuration

        /// action and maps Hangfire Dashboard to the app builder pipeline

        /// at the configured path ('/hangfire' by default).

        /// </summary>

        /// <param name="app">The app builder</param>

        /// <param name="configurationAction">Configuration action</param>

        public static void UseHangfire([NotNull] this IAppBuilder app,[NotNull] Action<IBootstrapperConfiguration> configurationAction)

        {

            if (app == null) throw new ArgumentNullException("app");

            if (configurationAction == null) throw new ArgumentNullException("configurationAction");

            //

            var configuration = new BootstrapperConfiguration(); 

            //       

            configurationAction(configuration);



            if (configuration.Activator != null)

            {

                JobActivator.Current = configuration.Activator;

            }



            if (configuration.Storage == null)

            {

                throw new InvalidOperationException("Job storage was not configured. Please call either `UseStorage` method or its overloads.");

            }

            //          

            JobStorage.Current = configuration.Storage;

            //   UseFilter()            

            foreach (var filter in configuration.Filters)

            {

                GlobalJobFilters.Filters.Add(filter);

            }

            //   UseServers()             ,           ,         server

            foreach (var server in configuration.Servers)

            {

                app.RunHangfireServer(server());

            }

            //  Route   filter   owin   

            app.MapHangfireDashboard(configuration.DashboardPath, configuration.AuthorizationFilters);

        }

    }

 
개발자가 직접 Ibootstrapper Configuration 인터페이스를 실현하도록 위탁하는 것이 아니라 위탁하는 방식으로 처리하다
이런 방식은 확실히 개발자가 사용하기에 더욱 편리하다. 
 
 
 
      public static void MapHangfireDashboard(

            [NotNull] this IAppBuilder app,

            string dashboardPath,

            IEnumerable<IAuthorizationFilter> authorizationFilters,

            JobStorage storage)

        {

            if (app == null) throw new ArgumentNullException("app");



            SignatureConversions.AddConversions(app);



            app.Map(dashboardPath,subApp => subApp.Use<DashboardMiddleware>(

                storage,

                DashboardRoutes.Routes,

                authorizationFilters));

        }
MapHangfireDashboard    DashboardMiddleware   own   

dashboardPath    “/hangfire”  ,         Http://www.xxx.com/hangfire            DashboardMiddleware

DashboardMiddleware OwinMiddleware
 //    DashboardMiddleware 



        public override Task Invoke(IOwinContext context)

        {

            // RouteCollection        ,

            var dispatcher = _routes.FindDispatcher(context.Request.Path.Value);

            

            if (dispatcher == null)

            {

                //    OwinMiddleware    

                //         OwinMiddleware

                return Next.Invoke(context);

            }

             

            foreach (var filter in _authorizationFilters)

            {

                if (!filter.Authorize(context.Environment))

                {

                    context.Response.StatusCode = (int) HttpStatusCode.Unauthorized;

                    return Task.FromResult(false);

                }

            }

            

            //     

            var dispatcherContext = new RequestDispatcherContext(

                _storage,

                context.Environment,

                dispatcher.Item2);

            

            return dispatcher.Item1.Dispatch(dispatcherContext);

        }

 
 
  
RequestDispatcherContext             ,        ,       。
 

좋은 웹페이지 즐겨찾기