Core timed task of HangFire

ASP.NET Core using Hangfire very simple, first of all, Nuget installation package

> install-package Hangfire -pre

Then ConfigureServicesadd the configuration code:

 public  void ConfigureServices (IServiceCollection Services) 
        { 
            services.AddMvc (); 
            services.Configure <the AppSettings> (Configuration.GetSection ( " the AppSettings " )); 
             // After the database links configured, will generate a corresponding number of tables in a database corresponding table 
            services.AddHangfire (X => x.UseSqlServerStorage ( " the Data = LocalHost the Source; = Tab_Hangfire the Initial Cataog; the Integrated Security = to true; to true the Persist Security Info = " )); 

            // services.AddTimedJob (); 
        }

Then Configureadd the configuration code:

 public  void the Configure (App IApplicationBuilder, IHostingEnvironment the env) 
        { 
            IF (env.IsDevelopment ()) 
            { 
                app.UseDeveloperExceptionPage (); 
            } 

            app.UseHangfireServer (); 
            app.UseHangfireDashboard (); 
            // ". 3 0 * * *"
             // RecurringJob.AddOrUpdate (() => Console.WriteLine ( "! the Recurring"), Cron.Minutely ());
// there are several parameters: <class method to be executed is located> HTTP: // localhost: 52815 / hangfire / number name periodically job class methods and parameters Corn expression RecurringJob.AddOrUpdate < the InsertData > ( " Prozkb" ., The p-=> the p- INSERT ( " ZKB " ) , ?" 0 * / 1 * * * " , System.TimeZoneInfo.Local);
// this Prozkb is RecurringJobId
// app.UseTimedJob (); app.UseMvc ( ); }
 public class InsertData
    {
        public void insert(string name)
        {
            string sql = "  insert into InsertData values('"+ name + "','',GETDATE())";           
            ExeNonQuery_B2B(sql);
        }

        public static void ExeNonQuery_B2B(string cmd)
        {
            SqlConnection con = new SqlConnection();
            con.ConnectionString = "Data Source=LocalHost;Initial Catalog= Tab_Hangfire;Integrated Security=true;Persist Security Info=true";
            con.Open();
            SqlCommand com = new SqlCommand();
            com.Connection = con;
            com.CommandType = CommandType.Text;
            com.CommandText = cmd;
            SqlDataReader dr = com.ExecuteReader();
            dr.Close();
            con.Close();
        }
    }

http: // localhost: 52815 / hangfire /            execute to the database is almost a minute

12:38: 06.4170000 almost time

 

 

 

Guess you like

Origin www.cnblogs.com/ZkbFighting/p/11294467.html