Lord.Service 6.0.8

There is a newer version of this package available.
See the version list below for details.
dotnet add package Lord.Service --version 6.0.8
                    
NuGet\Install-Package Lord.Service -Version 6.0.8
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Lord.Service" Version="6.0.8" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Lord.Service" Version="6.0.8" />
                    
Directory.Packages.props
<PackageReference Include="Lord.Service" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Lord.Service --version 6.0.8
                    
#r "nuget: Lord.Service, 6.0.8"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Lord.Service@6.0.8
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Lord.Service&version=6.0.8
                    
Install as a Cake Addin
#tool nuget:?package=Lord.Service&version=6.0.8
                    
Install as a Cake Tool

LordService 使用说明(自用模板)

支持多渠道消息推送、MQ、缓存、加密、日志等的组件化服务。

目录


项目简介

支持微信、钉钉、飞书等多种推送方式的消息推送服务。 支持企业微信应用推送、钉钉 APP 应用推送。 支持多种日志记录方式。 支持多种加密方式。 支持 RabbitMQ 消息队列,支持加密传输。 支持 Redis 缓存。 封装 RestSharp HTTP 请求组件,实现重试等功能。

开发目的

用于解决单位现有“低代码平台”导致的消息推送不稳定问题。由于钉钉或微信群消息有限制,引入 MQ 作为缓冲中间层,实现解耦与限流控制,将事件推送到钉钉或企业微信群。

环境依赖

  1. Visual Studio 2022
  2. .NET Core 3.1 / .NET 5(建议升级到 .NET 6+)
  3. RabbitMQ(如使用 MQ 功能)
  4. Redis(如使用分布式缓存)

使用框架说明

  • RabbitMQ:推送到消费端(解耦消息生产与消费)。
  • HttpPush:推送到钉钉群 / 企业微信群 / 飞书群等。
  • 可选死信队列(DLX)。

部署步骤

  1. 添加引用 / 拷贝项目。
  2. 配置 appsettings.*.json
  3. 编写 Worker / 控制台 / WebHost,注入并运行服务。

依赖注入示例

services.AddLordService(builder =>
{
    builder
        .UseHttp() // 使用默认 RestSharp 实现,可自定义超时
        .UseLogging(s => s.UseNLog()) // NLog / Log4Net / Serilog 三选一
        .UseCache(s => s.UseCustom(provider =>
        {
            var config = provider.GetRequiredService<IConfiguration>();
            var prefix = config.GetValue<string>("Redis:Prefix");
            var redisString = config.GetValue<string>("Redis:Connection");
            var csRedis = new CSRedisClient($"{redisString},prefix={prefix}");
            RedisHelper.Initialization(csRedis);
            return new RedisCahce(); // 自定义缓存实现
        }))
        // .UseCache(s => s.UseRedis("localhost:6379")) // 内置 Redis 示例
        .UseEncryption(s => s.UseDES(m => m.FromConfiguration("Encryption")))
        .UseMQ(s => s.UseRabbitMQ(m => m.FromConfiguration("RabbitMQ"))
            .UseDeadLetterExchange(m => m.FromConfiguration("DlxConfig")))
        // 推送用 Add,因为可能需要添加多个类型(钉钉 / 微信 / 飞书 / 应用等)
        .UsePush(s => s.AddDingTalk(config => config.FromConfiguration("CommonPushApi")));
});

使用方式示例

public class ApiMsgService
{
    private readonly IMQFactory factory; // MQ 工厂
    private readonly IDingTalkApiFactory apiFactory; // 钉钉,可换微信
    private readonly ICache cache; // 缓存
    private readonly ILogger<ApiMsgService> logger; // 日志
    private readonly IEncryptProvider encryptProvider; // 加密
    private readonly IJsonFormat jsonFormat; // JSON 序列化
    private readonly IConfiguration configuration; // 配置

    public ApiMsgService(
        IMQFactory factory,
        IDingTalkApiFactory apiFactory,
        ICache cache,
        ILogger<ApiMsgService> logger,
        IEncryptProvider encryptProvider,
        IJsonFormat jsonFormat,
        IConfiguration configuration)
    {
        this.factory = factory;
        this.apiFactory = apiFactory;
        this.cache = cache;
        this.logger = logger;
        this.encryptProvider = encryptProvider;
        this.jsonFormat = jsonFormat;
        this.configuration = configuration;
    }

    public async Task RunServiceAsync(CancellationToken cancellationToken = default)
    {
        try
        {
            var hostName = System.Net.Dns.GetHostName();
            var settings = configuration.GetSection("CommonPushApi")
                .Get<List<DingTalkSettings>>()!;

            // 生产端(推送端)
            var producer = await factory.GetPushServiceAsync("RabbitMQ");
            var initEntity = new ApiEntity($"这条信息来自于 [{hostName}]", new Exception("服务启动时预推送数据"));
            producer.Publish(initEntity);

            // 消费端(接收端)
            var receiver = await factory.GetReceiveServiceAsync<ApiEntity>();
            foreach (var setting in settings)
            {
                var push = apiFactory.GetPushService(setting.Alias, setting); // 钉钉
                await receiver.ReceiveAsync<ApiEntity>(apiEntity =>
                {
                    try
                    {
                        return push.Push(s => s.Format(apiEntity));
                    }
                    catch (Exception ex)
                    {
                        logger.LogError(ex,
                            "标题:{title}, 内容:{content}",
                            apiEntity.Title,
                            jsonFormat.Serialize(apiEntity.MsgBodies));
                    }
                    return true;
                }, () => Thread.Sleep(4000)); // 钉钉限流简单控制
            }
        }
        catch (Exception ex)
        {
            logger.LogError(ex, "{Service} 消费端发生未处理异常", nameof(ApiMsgService));
        }
    }
}

目录结构描述

│  注入
└─ README.md

关于作者

suiye007

更新日志

  1. 钉钉推送加入重试并可以群消息回调
  2. 更新定时任务
  3. 加入 NLog 和 Log4Net
  4. 基于 RestSharp 重新封装 HTTP 请求组件,适应现有项目
  5. 对引用组件进行升级

Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 is compatible.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
7.10.8 41 8/11/2026
7.10.7 94 8/6/2026
7.10.6 95 8/5/2026
7.10.5 93 8/4/2026
7.10.1 103 8/1/2026
7.10.0 100 7/30/2026
7.0.8 114 7/29/2026
7.0.7 104 7/29/2026
7.0.6 104 7/16/2026
7.0.5 119 4/24/2026
7.0.3 145 2/10/2026
7.0.2 137 2/6/2026
7.0.1 139 2/4/2026
7.0.0 150 1/26/2026
6.2.0 146 1/23/2026
6.0.9 142 1/12/2026
6.0.8 314 11/30/2025
Loading failed

升级为NET6/NET8/NET9多目标支持,加入Serilog/NLog/Log4Net官方扩展