138 lines
4.8 KiB
Markdown
138 lines
4.8 KiB
Markdown
## 后台服务设置
|
||
|
||
### Quartz写法
|
||
|
||
- 持久化存储生成数据SQL语句参考
|
||
- https://github.com/quartznet/quartznet/tree/main/database/tables
|
||
|
||
### 原生写法
|
||
|
||
- 设置每秒执行多少,设置后台服务,原生写法
|
||
|
||
```csharp
|
||
using Microsoft.Extensions.Hosting;
|
||
|
||
namespace Bunny.Service.BackgroundModule;
|
||
|
||
/// <summary>
|
||
/// 定时任务
|
||
/// </summary>
|
||
public class TemplateBackgroundModule : BackgroundService
|
||
{
|
||
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
|
||
{
|
||
while (!stoppingToken.IsCancellationRequested)
|
||
{
|
||
Console.WriteLine("TemplateService started");
|
||
await Task.Delay(1000, stoppingToken);
|
||
await Task.Run(() => { Console.WriteLine("执行了。。。"); }, stoppingToken);
|
||
await Task.Delay(1000, stoppingToken);
|
||
Console.WriteLine("--------------------------------");
|
||
}
|
||
}
|
||
}
|
||
```
|
||
|
||
## 验证码生成
|
||
|
||
- 验证码生成相关配置
|
||
|
||
```csharp
|
||
var service = builder.Services;
|
||
// 使用图形验证码
|
||
service.AddDistributedMemoryCache();
|
||
// 验证码相关配置内容
|
||
service.AddCaptcha(options =>
|
||
{
|
||
options.CaptchaType = CaptchaType.DEFAULT; // 验证码类型
|
||
options.CodeLength = 4; // 验证码长度
|
||
options.ExpirySeconds = 60; // 过期时间(单位/秒)
|
||
options.IgnoreCase = true; // 比较忽略大小写
|
||
options.ImageOption.Animation = true; // 是否启用动画
|
||
options.ImageOption.Width = 130; // 验证码宽度
|
||
options.ImageOption.Height = 48; // 验证码高度
|
||
options.ImageOption.BackgroundColor = SKColors.White;
|
||
options.ImageOption.BubbleCount = 6; // 气泡数量
|
||
options.ImageOption.BubbleMinRadius = 2; // 气泡最小半径
|
||
options.ImageOption.BubbleMaxRadius = 6; // 气泡最大半径
|
||
options.ImageOption.BubbleThickness = 2; // 气泡边沿厚度
|
||
options.ImageOption.InterferenceLineCount = 2; // 干扰线数量
|
||
options.ImageOption.FontSize = 36; // 字体大小
|
||
options.ImageOption.FontFamily = DefaultFontFamilys.Instance.Kaiti; // 字体,中文使用kaiti,其他字符可根据喜好设置
|
||
});
|
||
}
|
||
```
|
||
|
||
## AutoFac配置
|
||
|
||
### 自动注入按照名称注入
|
||
|
||
**AutoFac配置详情**
|
||
|
||
```csharp
|
||
using Autofac;
|
||
using Bunny.Common.Attribute;
|
||
using Microsoft.AspNetCore.Mvc;
|
||
|
||
namespace Bunny.WebApi.Config;
|
||
|
||
public static class AddAutofacConfig
|
||
{
|
||
/// <summary>
|
||
/// AutoFac自动注入约定名称
|
||
/// 接口以Service结尾注入到IOC中
|
||
/// </summary>
|
||
/// <param name="builder"></param>
|
||
public static void BuildContainer(ContainerBuilder builder)
|
||
{
|
||
// 扫描所以前缀Bunny的命名空间
|
||
var assemblies = AppDomain.CurrentDomain.GetAssemblies().Where(t => t.FullName!.StartsWith("Bunny"));
|
||
|
||
// 遍历注入服务
|
||
foreach (var assembly in assemblies)
|
||
{
|
||
var types = assembly.GetTypes();
|
||
|
||
// 注入Service
|
||
var serviceTypes = types.Where(t => t.GetCustomAttributes(typeof(ServiceAttribute), false).Length != 0);
|
||
foreach (var serviceType in serviceTypes)
|
||
{
|
||
var interfaces = serviceType.GetInterfaces();
|
||
builder.RegisterType(serviceType).As(interfaces).InstancePerDependency();
|
||
}
|
||
|
||
// 注入数据库相关
|
||
var mapperTypes = types.Where(t => t.GetCustomAttributes(typeof(MapperAttribute), false).Length != 0);
|
||
foreach (var mapperType in mapperTypes)
|
||
{
|
||
var interfaces = mapperType.GetInterfaces();
|
||
builder.RegisterType(mapperType).As(interfaces).InstancePerDependency();
|
||
}
|
||
|
||
// 注入后台服务
|
||
var componentTypes =
|
||
types.Where(t => t.GetCustomAttributes(typeof(ComponentAttribute), false).Length != 0);
|
||
foreach (var componentType in componentTypes)
|
||
builder.RegisterType(componentType).AsSelf().As<IHostedService>().SingleInstance();
|
||
}
|
||
|
||
// 如果不在在 Controller 层写构造函数可以打开这个,自动完成注入
|
||
var controllerBaseType = typeof(ControllerBase);
|
||
builder.RegisterAssemblyTypes(typeof(Program).Assembly)
|
||
.Where(t => controllerBaseType.IsAssignableFrom(t) && t != controllerBaseType)
|
||
.PropertiesAutowired(); //支持属性注入
|
||
}
|
||
}
|
||
```
|
||
|
||
**主程序入口配置**
|
||
|
||
```csharp
|
||
// 让控制器实例由容器创建
|
||
builder.Services.Replace(ServiceDescriptor.Transient<IControllerActivator, ServiceBasedControllerActivator>());
|
||
|
||
// 如果将Service放在 WebApi同级目录下,可以完成Controller自动注入,不需要写构造函数
|
||
builder.Services.Replace(ServiceDescriptor.Transient<IControllerActivator, ServiceBasedControllerActivator>());
|
||
builder.Services.AddControllers().AddJsonOptions(options =>
|
||
options.JsonSerializerOptions.Converters.Add(new JsonDateTimeConverter()));
|
||
``` |