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


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

教程目錄

  1. 從零開始啟動Osharp

    1.1. 使用OsharpNS項目模板創建項目

    1.2. 配置數據庫連接串並啟動項目

    1.3. OsharpNS.Swagger使用實例(登錄和授權)

    1.4. Angular6的前端項目啟動

  2. Osharp代碼生成器的使用

    2.1 生成器的使用

    2.2 生成代碼詳解(如何自己實現業務功能)

  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();
           }
       }

   }


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM