OsharpNS轻量级core快速开发框架简明⼊门教程-Osharp.Hangfire使⽤
OsharpNS轻量级 core快速开发框架简明⼊门教程
教程⽬录
1. 从零开始启动Osharp
1.1.
1.2.
1.3.
1.4.
2. Osharp代码⽣成器的使⽤
2.1
2.2
3. Osharp部分模块使⽤
3.1
3.2
3.3
4. Osharp深度学习和使⽤
4.1
4.2
4.3. ⾃定义模块的定义(Senparc.Weixin的使⽤)
4.4. 继续学习中....
OsharpNS官⽅资源
项⽬地址:
演⽰地址:直接使⽤QQ登录可以查看效果
⽂档地址:正在完善中....
发布博客:⼤神看这个⽂档应该就能跑起来,从零开始启动Osharp基于此⽂档完成
VS⽣成器插件:
官⽅交流QQ:85895249
OsharpNS.Hangfire使⽤
1. 启⽤OsharpNS.Hangfire
配置⽂件中到配置节点Hangfire(配置⽂件有两个,⼀个开发,⼀个发布,别改错地⽅)
"Hangfire": {
"WorkerCount": 20,
"StorageConnectionString": "Server=phone.qiadoo;Port=3306;UserId=candoo;Password=密码;Da
tabase=CanDoo.KaKa.Hangfire;charset='utf8';Allow User Variables=True", //这⾥是数据库的连接串⽤什么数据库就⽤什么数据库的连 "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. 看看效果spring framework是什么软件
3.1 按照连接串去新建Hangfire⽤的空数据库(只要库就可以,表⾃⼰会⽣成)
3.2 启动web项⽬
3.3 访问就能看到Hangfire的界⾯了
3.4 具体的使⽤参考,⽂件位于CanDoo.Test.Web.Hangfire
// -----------------------------------------------------------------------
// <copyright file="HangfireJobRunner.cs" company="OSharp开源团队">
// Copyright (c) 2014-2018 OSharp. All rights reserved.
// </copyright>
// <site></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();
}
}
}
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论