Vane.AspNetCore 1.0.0-preview.2

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

Vane.AspNetCore

把 Vane.NET 的 Host 作为 IHostedService 接入 ASP.NET Core / Generic Host。两者在同一个进程里并跑:Kestrel 继续处理 HTTP,Vane 继续处理自己的 TCP / WebSocket / QUIC / UDP 连接,共享 DI、日志和配置。

它不是 ASP.NET Core 的 IServer 实现,不替换 Kestrel,也不把 Vane 的传输层伪装成 HTTP 请求管线。需要 HTTP 的地方交给 ASP.NET Core;需要长连接、自定义帧、MQTT/RPC 的地方交给 Vane。

安装

dotnet add package Vane.AspNetCore --version 1.0.0-preview.1

net10.0 / net11.0,依赖 Microsoft.AspNetCore.App 框架引用与 Vane.NET。

最小用法

using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;
using Vane.AspNetCore;

WebApplicationBuilder builder = WebApplication.CreateBuilder(args);

builder.Services.AddVaneServer(options => options
    .ConfigureHost(host => host.ListenLocalhost(7000))
    .UseApplication(async connection =>
    {
        var input = connection.Transport.Input;
        var output = connection.Transport.Output;
        while (true)
        {
            var result = await input.ReadAsync();
            foreach (ReadOnlyMemory<byte> segment in result.Buffer)
                await output.WriteAsync(segment);
            input.AdvanceTo(result.Buffer.End);
            if (result.IsCompleted || result.IsCanceled) break;
        }
        await output.CompleteAsync();
    }));

WebApplication app = builder.Build();
app.MapGet("/", () => "Kestrel is still here");
app.Run();

app.Run() 启动 Kestrel,同时由 Generic Host 启动 Vane;app.StopAsync() / 进程退出时 Vane 会被停止并释放。

可运行示例

仓库内的 samples/Vane.Samples 提供 S20:Kestrel 与 Vane TCP 同进程、共享 DI、连接级 scope。

dotnet run --project samples/Vane.Samples -- S20

示例先请求 Kestrel 的 / 得到 kestrel-ok,再连接 Vane 的动态 TCP 端口,读回 shared-di:<scope-id>。源码见 samples/Vane.Samples/Examples/S20_AspNetCoreIntegration.cs。

Generic Host

using Vane.AspNetCore;

// 这是 Microsoft.Extensions.Hosting.Host,不是 Vane.Hosting.Host
Microsoft.Extensions.Hosting.Host.CreateDefaultBuilder(args)
    .ConfigureServices((context, services) => services.AddVaneServer(options => options
        .ConfigureHost(host => host.ListenAnyIP(7001))
        .UseApplication(async connection => { /* ... */ })))
    .Build()
    .Run();

如果你已经 using Vane.Hosting,Host.CreateDefaultBuilder 会解析到 Microsoft 的 Host 还是 Vane 的 Host 取决于命名空间导入顺序;写成完全限定名最稳。

DI 与作用域

UseApplication(ConnectionDelegate) 适合不需要 DI 的纯管线;UseApplication(Func<IServiceProvider, ConnectionDelegate>) 在启动时拿到根容器,适合解析单例或在连接处理函数内部按连接创建作用域:

options.UseApplication(provider =>
{
    var singleton = provider.GetRequiredService<MySingleton>();
    var scopes = provider.GetRequiredService<IServiceScopeFactory>();

    return async connection =>
    {
        using var scope = scopes.CreateScope();
        var scoped = scope.ServiceProvider.GetRequiredService<MyScopedService>();
        // ...
    };
});

不要把 scoped 服务捕获进单例连接委托;连接生命周期通常远长于一次请求。

日志

默认把 Vane 的 ILogSink 桥接到 ILogger,字段以结构化 state 输出。要接 Serilog / NLog,直接给 LogSink,它会优先于内置桥:

options.LogSink = myVaneSink;
options.EnableLoggerBridge = false;

生命周期语义

阶段 行为
AddVaneServer 创建 ServerOptions,执行 ConfigureHost;此时只配置,不绑定端口
StartAsync 解析连接应用;桥接日志;调用 Host.StartAsync
StopAsync 调用 Host.StopAsync;重复调用无副作用
DisposeAsync 若未正常停止则兜底停止,然后释放 Host

StartAsync 重复调用、Application 与 ApplicationFactory 同时设置、未设置连接应用,都会抛出明确异常,而不是启动一个半配置的服务器。

边界

  • 不做 ASP.NET Core IServer 适配;不要指望 UseKestrel() 被 Vane 替换。
  • 不做 HTTP/SSE MCP 传输;MCP 的 HTTP 传输应放在 ASP.NET Core 侧,见 Vane.Mcp。
  • 一个 IServiceCollection 只注册一个 Vane 宿主;多个宿主请显式创建多个 Host。
Product Compatible and additional computed target framework versions.
.NET 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.  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
1.0.0-preview.2 64 11/21/2026
1.0.0-preview.1 64 11/19/2026

1.0.0-preview.2 预发布:仅支持 net10.0/net11.0;本版修复帧定界、管道、会话与传输层边界缺陷,公开 API 无变更;完整发布说明见包内 RELEASE_NOTES.md。