OsharpNS輕量級.net core快速開發框架簡明入門教程-Osharp.Hangfire使用

OsharpNS輕量級.net core快速開發框架簡明入門教程

教程目錄html

  1. 從零開始啓動Osharp前端

    1.1. 使用OsharpNS項目模板建立項目git

    1.2. 配置數據庫鏈接串並啓動項目github

    1.3. OsharpNS.Swagger使用實例(登陸和受權)web

    1.4. Angular6的前端項目啓動數據庫

  2. Osharp代碼生成器的使用框架

    2.1 生成器的使用async

    2.2 生成代碼詳解(如何本身實現業務功能)ide

  3. Osharp部分模塊使用學習

    3.1 Osharp.Redis使用

    3.2 Osharp.Hangfire使用

    3.3 Osharp.Permissions使用

  4. Osharp深度學習和使用

    4.1 切換數據庫(從SqlServer改成MySql)

    4.2 多上下文配置(多個數據庫的使用)

    4.3. 自定義模塊的定義(Senparc.Weixin的使用)

    4.4. 繼續學習中....

OsharpNS官方資源
項目地址:https://github.com/i66soft/osharp-ns20
演示地址:https://www.osharp.org 直接使用QQ登陸能夠查看效果
文檔地址:https://docs.osharp.org 正在完善中....
發佈博客:https://www.cnblogs.com/guomingfeng/p/osharpns-publish.html 大神看這個文檔應該就能跑起來,從零開始啓動Osharp基於此文檔完成
VS生成器插件:https://marketplace.visualstudio.com/items?itemName=LiuliuSoft.osharp
官方交流QQ羣:85895249

OsharpNS.Hangfire使用

  1. 啓用OsharpNS.Hangfire

    配置文件中找到配置節點Hangfire (配置文件有兩個,一個開發,一個發佈,別改錯地方)

    "Hangfire": {
          "WorkerCount": 20,
          "StorageConnectionString": "Server=phone.qiadoo.com;Port=3306;UserId=candoo;Password=密碼;Database=CanDoo.KaKa.Hangfire;charset='utf8';Allow User Variables=True", //這裏是數據庫的鏈接串 用什麼數據庫就用什麼數據庫的鏈接串(官方默認是SqlServer的,我這裏用的是MySql)
    
          "DashboardUrl": "/hangfire",
          "Roles": "", //這個有可能肯定能夠訪問的用戶的角色,沒測試過
    
          "Enabled": true
    
        }
  2. 改用MySql做爲Hangfire的數據庫

    官方使用的是SqlServer,只要配置好鏈接串就能夠使用了,我用的是MySql,因此要改動一些東西

    2.1 安裝Hangfire的MySql支持庫

    經過Nuget安裝`Hangfire.MySql.Core`

    2.2 Startups中繼承HangfirePack新建一個MyHangfirePack

    using Hangfire;
    using Hangfire.MySql.Core;
    using Microsoft.Extensions.Configuration;
    using Microsoft.Extensions.DependencyInjection;
    using OSharp.Extensions;
    using OSharp.Hangfire;
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Threading.Tasks;
    
    namespace CanDoo.Test.Web.Startups
    {
        public class MyHangfirePack : HangfirePack
        {
            /// <summary>
            /// AddHangfire委託,重寫可配置Hangfire服務,好比使用UseSqlServerStorage等
            /// </summary>
            /// <param name="services">服務容器</param>
            /// <returns></returns>
            protected override Action<IGlobalConfiguration> GetHangfireAction(IServiceCollection services)
            {
                IConfiguration configuration = services.GetConfiguration();
                string storageConnectionString = configuration["OSharp:Hangfire:StorageConnectionString"].CastTo<string>();
                if (storageConnectionString != null)
                {
                    return config => config.UseStorage(new MySqlStorage(storageConnectionString));
                }
    
                return config => { };
            }
        }
    }
  3. 看看效果

    3.1 按照鏈接串去新建Hangfire用的空數據庫(只要庫就能夠,表本身會生成)

    3.2 啓動web項目

    3.3 訪問http://localhost:7001/hangfire/就能看到Hangfire的界面了

    3.4 具體的使用參考,文件位於CanDoo.Test.Web.Hangfire

// -----------------------------------------------------------------------
   //  <copyright file="HangfireJobRunner.cs" company="OSharp開源團隊">
   //      Copyright (c) 2014-2018 OSharp. All rights reserved.
   //  </copyright>
   //  <site>http://www.osharp.org</site>
   //  <last-editor>郭明鋒</last-editor>
   //  <last-date>2018-12-31 17:36</last-date>
   // -----------------------------------------------------------------------
   
   using System;
   using System.Collections.Generic;
   using System.Diagnostics;
   using System.Linq;
   using System.Threading.Tasks;
   
   using Hangfire;
   
   using CanDoo.Test.Identity;
   using CanDoo.Test.Identity.Entities;
   
   using Microsoft.AspNetCore.Identity;
   using Microsoft.Extensions.DependencyInjection;
   
   using OSharp.Collections;
   using OSharp.Dependency;
   using OSharp.Entity;
   using OSharp.Hangfire;

   namespace CanDoo.Test.Web.Hangfire
   {
       [Dependency(ServiceLifetime.Singleton)]
       public class HangfireJobRunner : IHangfireJobRunner
       {
           public void Start()
           {
               BackgroundJob.Enqueue<UserManager<User>>(m => m.FindByIdAsync("1"));
               string jobId = BackgroundJob.Schedule<UserManager<User>>(m => m.FindByIdAsync("2"), TimeSpan.FromMinutes(2));
               BackgroundJob.ContinueWith<TestHangfireJob>(jobId, m => m.GetUserCount());
               RecurringJob.AddOrUpdate<TestHangfireJob>(m => m.GetUserCount(), Cron.Minutely, TimeZoneInfo.Local);
               RecurringJob.AddOrUpdate<TestHangfireJob>(m=>m.LockUser2(), Cron.Minutely, TimeZoneInfo.Local);
           }
       }

       public class TestHangfireJob
       {
           private readonly IIdentityContract _identityContract;
           private readonly IServiceProvider _provider;
    
           /// <summary>
           /// 初始化一個<see cref="TestHangfireJob"/>類型的新實例
           /// </summary>
           public TestHangfireJob(IIdentityContract identityContract, IServiceProvider provider)
           {
               _identityContract = identityContract;
               _provider = provider;
           }
    
           /// <summary>
           /// 獲取用戶數量
           /// </summary>
           public string GetUserCount()
           {
               List<string> list = new List<string>();
               list.Add(_identityContract.Users.Count().ToString());
               list.Add(_identityContract.GetHashCode().ToString());
               return list.ExpandAndToString();
           }
    
           public async Task<string> LockUser2()
           {
               List<string> list = new List<string>();
               UserManager<User> userManager = _provider.GetService<UserManager<User>>();
               User user2 = await userManager.FindByIdAsync("2");
               list.Add($"user2.IsLocked: {user2.IsLocked}");
               user2.IsLocked = !user2.IsLocked;
               await userManager.UpdateAsync(user2);
               IUnitOfWork unitOfWork = _provider.GetUnitOfWork<User, int>();
               unitOfWork.Commit();
               user2 = await userManager.FindByIdAsync("2");
               list.Add($"user2.IsLocked: {user2.IsLocked}");
               return list.ExpandAndToString();
           }
       }

   }
相關文章
相關標籤/搜索