DingTalkStream.Core 2026.7.21

Suggested Alternatives

Qishao.DingTalkStream.Core

Additional Details

已弃用,不在维护

The owner has unlisted this package. This could mean that the package is deprecated, has security vulnerabilities or shouldn't be used anymore.
dotnet add package DingTalkStream.Core --version 2026.7.21
                    
NuGet\Install-Package DingTalkStream.Core -Version 2026.7.21
                    
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="DingTalkStream.Core" Version="2026.7.21" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="DingTalkStream.Core" Version="2026.7.21" />
                    
Directory.Packages.props
<PackageReference Include="DingTalkStream.Core" />
                    
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 DingTalkStream.Core --version 2026.7.21
                    
#r "nuget: DingTalkStream.Core, 2026.7.21"
                    
#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 DingTalkStream.Core@2026.7.21
                    
#: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=DingTalkStream.Core&version=2026.7.21
                    
Install as a Cake Addin
#tool nuget:?package=DingTalkStream.Core&version=2026.7.21
                    
Install as a Cake Tool

DingTalkStream.Core

DingTalkStream.Core 是钉钉 Stream 模式的基础库,负责注册订阅、建立长连接、接收推送、排队去重、并发调度消息处理器,以及生成推送响应数据。

安装

dotnet add package DingTalkStream.Core

目标框架

  • net8.0
  • netstandard2.1

当前项目直接引用 Microsoft.Extensions.*。netstandard2.1 额外引用 System.Text.Json。

引用方式

当前仓库内使用项目引用:

<ProjectReference Include="..\DingTalkStream.Core\DingTalkStream.Core.csproj" />

快速开始

在 Generic Host 中注册 Stream 客户端、订阅和消息处理器:

using DingTalkStream.Core;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;

var host = Host.CreateDefaultBuilder(args)
    .ConfigureServices((context, services) =>
    {
        services.AddDingtalkStream(options =>
        {
            options.ClientId = context.Configuration["ClientId"];
            options.ClientSecret = context.Configuration["ClientSecret"];
            options.AutoReplySystemMessage = true;
            options.MaxTaskCount = Environment.ProcessorCount;
        })
        .RegisterEventSubscription()
        .RegisterCardInstanceCallback()
        .AddMessageHandler<DefaultStreamMessageHandler>()
        .AddHostServices();
    })
    .Build();

await host.RunAsync();

消息处理器实现 IDingTalkStreamMessageHandler:

using DingTalkStream.Core;

public sealed class DefaultStreamMessageHandler : IDingTalkStreamMessageHandler
{
    public async Task HandleMessage(MessageEventHanderArgs e)
    {
        if (e.Type != SubscriptionType.EVENT)
        {
            return;
        }

        var data = await DingTalkStreamUtilities.CreateReply_EventSuccess_MessageData("OK");
        var reply = await DingTalkStreamUtilities.CreateReplyMessage(e.Headers.MessageId, data);
        await e.Reply(reply);
    }
}

配置方式

推荐使用委托方式配置 ClientId 和 ClientSecret,因为当前 AddDingtalkStream(IConfiguration) 重载读取的是 ClientScript 键:

{
  "ClientId": "your-client-id",
  "ClientScript": "your-client-secret",
  "UA": "your-app/1.0.0",
  "AutoReplySystemMessage": "true",
  "Subscriptions": [
    {
      "Type": "EVENT",
      "Topic": "*"
    },
    {
      "Type": "CALLBACK",
      "Topic": "/v1.0/card/instances/callback"
    }
  ]
}

如果配置文件使用 ClientSecret,请使用 AddDingtalkStream(options => ...)。

注册 API

API 作用
AddDingtalkStream(Action<DingTalkStreamOptions>) 注册 DingTalkStreamOptions 并返回构建器。
AddDingtalkStream(IConfiguration) 从配置对象读取 ClientId、ClientScript、UA、AutoReplySystemMessage 和 Subscriptions。
RegisterSubscription(type, topic) 注册自定义订阅,自动去重相同 type + topic。
RegisterEventSubscription() 注册事件推送订阅:EVENT + *。
RegisterCardInstanceCallback() 注册卡片回调订阅:CALLBACK + /v1.0/card/instances/callback。
AddMessageHandler<T>() 以 transient 生命周期注册消息处理器。
AddHostServices() 注册 DingTalkStreamClientWorker,Host 启动后自动连接 Stream。

运行机制

  1. DingTalkStreamClientWorker 延迟 1 秒启动。
  2. Worker 创建 DingTalkStreamClient,并订阅客户端的 OnMessage 事件。
  3. Client 使用 ClientId、ClientSecret 和订阅列表请求钉钉 Stream 网关连接信息。
  4. Client 通过 ClientWebSocket 建立长连接并接收文本消息。
  5. 开启 AutoReplySystemMessage 后,Client 自动处理 SYSTEM 类型的 ping 和 disconnect。
  6. 非系统消息进入 Worker 队列,Worker 按 Headers.MessageId 做队列内和执行中去重。
  7. Worker 每次从 DI scope 中解析一个 IDingTalkStreamMessageHandler 来处理消息。

主要模型

类型 说明
DingTalkStreamOptions Stream 客户端配置,包含凭证、UA、订阅列表和并发参数。
Subscription 单条订阅配置,包含 Type 和 Topic。
DingTalkStreamClient 请求网关、建立 WebSocket、接收消息、重连和回复系统消息。
MessageEventHanderArgs 推送消息参数,继承 DingTalkStreamDataPackage,并提供 Reply(byte[])。
DingTalkStreamDataPackage 包装原始推送 JSON,提供 SpecVersion、Type、Headers、Data。
DingTalkStreamDataHeaders 包装 headers,提供 AppId、ConnectionId、Topic、MessageId、ContentType、Time。
DingTalkStreamEventDataHeaders 事件推送 headers 扩展模型,可由 ToEventDataHeaders() 转换。

回复工具

DingTalkStreamUtilities 提供以下辅助方法:

方法 作用
CreateReplyMessage(messageId, data) 构造发送给 Stream 服务端的完整响应。
CreateReply_EventSuccess_MessageData(customMessage) 构造事件消费成功数据。当前返回字段为 status = "SUCESS"。
CreateReply_EventFaild_MessageData(customMessage) 构造事件稍后重试数据,返回 status = "LATER"。
CreateReply_Callback_MessageData(responseJson) 将回调业务响应包装为 {"response": ...}。

并发和去重

  • MaxTaskCount 控制同时处理消息的任务数,默认是 Environment.ProcessorCount。
  • Worker 使用 ConcurrentQueue<MessageEventHanderArgs> 保存待处理消息。
  • Worker 使用 ConcurrentDictionary<string, MessageEventHanderArgs> 记录执行中的消息。
  • 相同 MessageId 已在队列中或正在执行时,新推送会被忽略。
  • SingleExecuteTimeOut 只用于记录超时警告,不会取消处理器。
  • RecentExecutionTimeCount 控制最近执行耗时的滑动窗口大小。
  • MaxQueueCount 和 TimeInterval 当前只在 options 中定义,Worker 当前没有按这两个值限制队列或轮询。

注意点

  • MessageEventHanderArgs、OnStoped、CreateReply_EventFaild_MessageData 等名称是当前公开 API,拼写保持兼容。
  • AddDingtalkStream(IConfiguration) 使用 ClientScript 键读取密钥。
  • Reply(byte[]) 应尽快调用。注释中说明 5 秒内未回复时,服务端可能重发。
  • 事件订阅会在请求网关时合并为 EVENT + *。
  • 非 EVENT 类型会按实际 Topic 写入订阅列表。

构建验证

dotnet build .\DingTalkStream.Core\DingTalkStream.Core.csproj --no-restore
Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  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 was computed.  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. 
.NET Core netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.1 is compatible. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos 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