Sparkdo.Messaging.Serialization.Json 3.0.0-preview.1

This is a prerelease version of Sparkdo.Messaging.Serialization.Json.
dotnet add package Sparkdo.Messaging.Serialization.Json --version 3.0.0-preview.1
                    
NuGet\Install-Package Sparkdo.Messaging.Serialization.Json -Version 3.0.0-preview.1
                    
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="Sparkdo.Messaging.Serialization.Json" Version="3.0.0-preview.1" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Sparkdo.Messaging.Serialization.Json" Version="3.0.0-preview.1" />
                    
Directory.Packages.props
<PackageReference Include="Sparkdo.Messaging.Serialization.Json" />
                    
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 Sparkdo.Messaging.Serialization.Json --version 3.0.0-preview.1
                    
#r "nuget: Sparkdo.Messaging.Serialization.Json, 3.0.0-preview.1"
                    
#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 Sparkdo.Messaging.Serialization.Json@3.0.0-preview.1
                    
#: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=Sparkdo.Messaging.Serialization.Json&version=3.0.0-preview.1&prerelease
                    
Install as a Cake Addin
#tool nuget:?package=Sparkdo.Messaging.Serialization.Json&version=3.0.0-preview.1&prerelease
                    
Install as a Cake Tool

Sparkdo.Messaging.Serialization.Json

把 Sparkdo.Json 的 IJsonSerializer 适配成 messaging 的 IMessageSerializer——让载荷走 application/json 这一档。整包只有一个类型。

项 值
包 Sparkdo.Messaging.Serialization.Json
交付框架 net11.0
外部 NuGet 依赖 无(只项目引用 Sparkdo.Messaging.Abstractions 与 Sparkdo.Json.Abstractions)
类型 JsonMessageSerializer
DI 扩展 没有——接线在内核包的 AddSparkdoMessaging 那侧
AOT / 裁剪 兼容

刻意不引 System.Text.Json:序列化路径必须只有一条,全部经 Sparkdo.Json 的门面。 类型发现、resolver 组合、AOT 闭包、命名 options 都在 src/json 那一侧,本包一个都不做。

<PackageReference Include="Sparkdo.Messaging" Version="1.0.0" />
<PackageReference Include="Sparkdo.Messaging.Serialization.Json" Version="1.0.0" />
<PackageReference Include="Sparkdo.Json" Version="1.0.0" />

接线(标准宿主,一条链)

JsonMessageSerializer 要 IJsonSerializer,而后者只能由容器给(AddSparkdoJsonCore 按类型登记)。 所以注册表用工厂那条重载——它在第一次要用构建器时才跑:

var builder = WebApplication.CreateBuilder(args);

builder.Services
    .AddSparkdoJson()                                    // 生成器在应用程序集里发这个入口
    .AddSparkdoMessaging(
        provider => new MessageSerializerRegistry()
            .Add(new JsonMessageSerializer(provider.GetRequiredService<IJsonSerializer>())),
        bus => bus.AddTransport(new RabbitMQTransport(options), TransportDefault.Default))
    .AddSparkdoMessagingHosting();

手上已经有 IServiceProvider 的场合(控制台、测试夹具、非 MS DI 的宿主)走实例那条:

var serializers = new MessageSerializerRegistry()
    .Add(new JsonMessageSerializer(provider.GetRequiredService<IJsonSerializer>()));

services.AddSparkdoMessaging(serializers, bus => bus.AddTransport(transport, TransportDefault.Default));

两条别混着用:实例那条把传进来的那一份交给图,工厂那条把容器里的那一份交给图。 容器里同时存在两份注册表时,派发用一份、响应的序列化与反序列化用另一份——同一件事两个答案。

你的消息类型要进 JSON 闭包

这是本包唯一"额外的活儿",而且不在 messaging 侧:消息类型所属的程序集要声明闭包。

[SparkdoJsonContext]                          // 或 [SparkdoJsonContext("orders", Priority = 20)]
[JsonSerializable(typeof(OrderPlaced))]
[JsonSerializable(typeof(List<OrderItem>))]   // 容器类型要显式声明
internal sealed partial class OrdersJsonContext : JsonSerializerContext;
规则 后果
每个会上线的 CLR 类型都要出现在某个 [JsonSerializable] 上 没接线就是编译失败或启动即抛——不退回反射(与 messaging 侧同取向)
嵌套/基类/集合的元素类型也要显式 List<OrderItem> 声明了但 OrderItem 没声明,一样解不出
宿主不是可执行文件时,AddSparkdoJson() 要在宿主里调一次 不调就聚合不到你那个 contributor;Sparkdo.Messaging.Scheduling.Scheduler 自带它需要的 context,你的消息类型仍要自己声明

这个桥做了什么、没做什么

面 口径
写 SerializeToUtf8Bytes → 直接写进 IBufferWriter<byte>。用例钉住的是选对出口:不退化成"先转 string 再编码"(Write_UsesUtf8Facade_NotStringFacade)
读 Deserialize<T>(ReadOnlySpan<byte>)——零拷贝那条签名
内容类型 恒为 MessageContentType.Json(application/json,驻留实例)
泛型 Write<TMessage> / Deserialize<TMessage> 原样转发,不看类型是什么
每消息换内容类型 本包只提供一种;[Message(ContentType = "application/protobuf")] 要另配一个实现该 MIME 的 IMessageSerializer 并 Add 进同一个注册表,否则建图期报"没有可用的读写器"
SendOptions.ContentType 只改信封上的标签,不改写 bytes 的那个实例。改错了接收侧就按错标签去解
多态 / 类型名写进 JSON 不做。路由靠信封上的 MessageIdentity,不靠载荷里的 $type
版本兼容 不归本包:Sparkdo.Json 侧是 source-generated resolver,加字段可行、改类型要看 json 域口径

边界与缺口

项 现状
载荷是 JSON null IJsonSerializer.Deserialize<T> 返回 T?,本桥按 IMessageSerializer 的非空签名交出(!)。别把"可能是 null 的正文"投进要求非空参数的记录类型
真 IJsonSerializer 的端到端 本域用例全用替身(RecordingJson)证明"选对出口"与往返;真的 source-generated 端到端在 src/json 那侧出证。这里没有"消息进 JSON 闭包 → 上线 → 解回来"的合并用例
null 之外的畸形载荷 由 Sparkdo.Json 抛,本桥不翻译、不吞

换掉它

想用自己的序列化(MessagePack、Protobuf、压缩后的 JSON):实现 IMessageSerializer 三件(ContentType / Write / Deserialize)并 Add 进注册表即可,本包不是必经之路。它存在的意义只是: Sparkdo.Json 已经是仓库里唯一的 JSON 出口,别再开第二条。

Product Compatible and additional computed target framework versions.
.NET net11.0 is compatible. 
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
3.0.0-preview.1 53 10/30/2026