Bitzsoft.Integrations.AI.AspNetCore 1.0.5

There is a newer version of this package available.
See the version list below for details.
dotnet add package Bitzsoft.Integrations.AI.AspNetCore --version 1.0.5
                    
NuGet\Install-Package Bitzsoft.Integrations.AI.AspNetCore -Version 1.0.5
                    
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="Bitzsoft.Integrations.AI.AspNetCore" Version="1.0.5" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Bitzsoft.Integrations.AI.AspNetCore" Version="1.0.5" />
                    
Directory.Packages.props
<PackageReference Include="Bitzsoft.Integrations.AI.AspNetCore" />
                    
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 Bitzsoft.Integrations.AI.AspNetCore --version 1.0.5
                    
#r "nuget: Bitzsoft.Integrations.AI.AspNetCore, 1.0.5"
                    
#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 Bitzsoft.Integrations.AI.AspNetCore@1.0.5
                    
#: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=Bitzsoft.Integrations.AI.AspNetCore&version=1.0.5
                    
Install as a Cake Addin
#tool nuget:?package=Bitzsoft.Integrations.AI.AspNetCore&version=1.0.5
                    
Install as a Cake Tool

Bitzsoft.Integrations.AI.AspNetCore

面向 ASP.NET Core 宿主的 AI 流式响应协议写出器连接器包。将 Bitzsoft.Integrations.AI.Conversation 产出的 IAsyncEnumerable<AIStreamFrame> 安全写出至 HttpContext,内置 SSE 与 NDJSON 双协议协商、Nginx/网关反向代理防缓冲响应头、UTF-8 中文零转义序列化、客户端主动断连静默收敛与未处理异常强脱敏兜底。

版本与兼容性

dotnet add package Bitzsoft.Integrations.AI.AspNetCore
  • 目标框架:net8.0;net10.0;
  • 框架引用:Microsoft.AspNetCore.App(零额外第三方 NuGet 包依赖);
  • 项目依赖:Bitzsoft.Integrations.AI.Conversation。

核心行为契约

  1. 双协议内容协商(NegotiateFormat):
    • 查询参数优先:?format=ndjson 协商为 application/x-ndjson; charset=utf-8(每帧 {json}\n);?format=sse 协商为 text/event-stream; charset=utf-8(默认每帧 data: {json}\n\n,开启 EmitSseEventField = true 时输出 event: {type}\ndata: {json}\n\n);
    • 请求头次之:解析 Accept 请求头中 application/x-ndjson 与 text/event-stream 的优先级,缺省回退至 DefaultFormat(默认 SSE)。
  2. 反向代理零缓冲响应头:
    • 自动调用 IHttpResponseBodyFeature.DisableBuffering() 关闭传输层响应缓冲;
    • 自动写入 Cache-Control: no-cache, no-transform、X-Accel-Buffering: no 与 Pragma: no-cache,防止 Nginx、云负载均衡或浏览器代理攒批阻塞首字到达时间(TTFT)。
  3. 中文与符号零 \uXXXX 转义:
    • 固定使用 JsonNamingPolicy.CamelCase 与 JavaScriptEncoder.UnsafeRelaxedJsonEscaping(无 BOM UTF-8),中文字符以原生 UTF-8 字节直出,显著降低流式带宽并方便前端与抓包排障。
  4. 客户端断连与服务端异常严格区分:
    • 自动联动 HttpContext.RequestAborted 与调用方传入的 CancellationToken:当用户关闭页面或前端主动 AbortController.abort() 时,写出器静默干净退出,绝不补写多余的 error 帧;
    • 当上游模型网关超时(非客户端触发的 TaskCanceledException / TimeoutException)或服务端抛出未处理异常时,真实堆栈仅写入服务端 ILogger,向客户端下发经过敏感路径、密钥字面量与堆栈正则二次清洗的终端 AIStreamFrame.Error(附带 traceId 供运维关联排障)。

完整最小示例

以下示例演示在 ASP.NET Core 请求上下文中注入 IAIStreamResponseWriter 并按协商的 NDJSON 格式实时写出中文流式事件帧:

using System.Text;
using Bitzsoft.Integrations.AI.AspNetCore;
using Bitzsoft.Integrations.AI.Conversation;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;

var services = new ServiceCollection();
services.AddLogging();
services.AddBitzsoftAIStreamWriter();

await using var provider = services.BuildServiceProvider(
    new ServiceProviderOptions { ValidateOnBuild = true, ValidateScopes = true });

var writer = provider.GetRequiredService<IAIStreamResponseWriter>();

var httpContext = new DefaultHttpContext();
httpContext.Request.Headers.Accept = "application/x-ndjson";
using var responseStream = new MemoryStream();
httpContext.Response.Body = responseStream;

static async IAsyncEnumerable<AIStreamFrame> ProduceFramesAsync()
{
    yield return AIStreamFrame.Start(conversationId: "conv-1001", requestId: "req-01", modelId: "qwen-max");
    yield return AIStreamFrame.Message("已完成合同条款比对,未发现高风险冲突。");
    yield return AIStreamFrame.Done(conversationId: "conv-1001", requestId: "req-01", latencyMs: 128);
    await Task.CompletedTask;
}

await writer.WriteFramesAsync(httpContext, ProduceFramesAsync());

string output = Encoding.UTF8.GetString(responseStream.ToArray());
Console.WriteLine($"协商类型:{httpContext.Response.ContentType},输出帧行数:{output.Split('\n', StringSplitOptions.RemoveEmptyEntries).Length}");
Product Compatible and additional computed target framework versions.
.NET 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 is compatible.  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 (1)

Showing the top 1 NuGet packages that depend on Bitzsoft.Integrations.AI.AspNetCore:

Package Downloads
Bitzsoft.Integrations.AI.All

Bitzsoft AI 套件 1.2.0 独立运行期聚合元包 — 一键引入全部 11 个活跃 Bitzsoft.Integrations.AI.* 运行期包(不含已弃用旧包、不含 Ocr 与 AI.Testing)

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.2.0 0 11/21/2026
1.1.0 97 11/14/2026
1.0.5 105 11/7/2026