CJF.NamedPipe
1.0.10
There is a newer version of this package available.
See the version list below for details.
See the version list below for details.
dotnet add package CJF.NamedPipe --version 1.0.10
NuGet\Install-Package CJF.NamedPipe -Version 1.0.10
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="CJF.NamedPipe" Version="1.0.10" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="CJF.NamedPipe" Version="1.0.10" />
<PackageReference Include="CJF.NamedPipe" />
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 CJF.NamedPipe --version 1.0.10
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: CJF.NamedPipe, 1.0.10"
#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 CJF.NamedPipe@1.0.10
#: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=CJF.NamedPipe&version=1.0.10
#tool nuget:?package=CJF.NamedPipe&version=1.0.10
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
CJF.NamedPipe
一個基於 ASP.NET Core 依賴注入 (DI) 模式的命名管道 (Named Pipe) 通信庫,支援一般命令和串流命令處理。
功能特色
- ✅ ASP.NET Core DI 支援: 完全整合 Microsoft.Extensions.DependencyInjection
- ✅ 雙管道架構: 分別處理一般命令和串流命令
- ✅ 配置選項: 支援 IOptions 模式進行配置
- ✅ 日誌記錄: 整合 Microsoft.Extensions.Logging
- ✅ 後台服務: 支援 IHostedService 模式
- ✅ 錯誤處理: 完善的異常處理和超時機制
- ✅ 測試支援: 包含完整的單元測試和整合測試
快速開始
1. 安裝套件
dotnet add package CJF.NamedPipe
2. 註冊服務
using CJF.NamedPipe;
var builder = Host.CreateDefaultBuilder(args);
builder.ConfigureServices(services =>
{
// 註冊命名管道服務
services.AddPipeLineService(CommandHandler, StreamHandler);
// 或者使用配置選項
services.AddPipeLineService(options =>
{
options.CommandPipeName = "MyApp.Command.Pipe";
options.StreamPipeName = "MyApp.Stream.Pipe";
options.MaxClients = 10;
}, CommandHandler, StreamHandler);
});
var host = builder.Build();
await host.RunAsync();
3. 實作命令處理器
// 一般命令處理器
static async Task<string> CommandHandler(string command, string[] args)
{
return command switch
{
"hello" => "Hello, World!",
"time" => DateTime.Now.ToString(),
"echo" => string.Join(" ", args),
_ => $"未知命令: {command}"
};
}
// 串流命令處理器
static async Task StreamHandler(string command, string[] args,
Func<StreamMessage, Task<bool>> streamWriter, CancellationToken cancellationToken)
{
await streamWriter(new StreamMessage($"開始處理: {command}", StreamMessageTypes.Info));
// 模擬處理過程
for (int i = 1; i <= 5; i++)
{
await streamWriter(new StreamMessage($"進度: {i}/5", StreamMessageTypes.Info));
await Task.Delay(1000, cancellationToken);
}
await streamWriter(new StreamMessage("處理完成", StreamMessageTypes.Success, true));
}
4. 客戶端使用
using CJF.NamedPipe;
using Microsoft.Extensions.DependencyInjection;
var services = new ServiceCollection();
services.AddLogging();
services.Configure<PipeLineOptions>(options =>
{
options.CommandPipeName = "MyApp.Command.Pipe";
options.StreamPipeName = "MyApp.Stream.Pipe";
});
services.AddSingleton<IPipeLineProvider, PipeLineProvider>();
var serviceProvider = services.BuildServiceProvider();
var provider = serviceProvider.GetRequiredService<IPipeLineProvider>();
var client = provider.CreateClient();
// 發送一般命令
var (result, message) = await client.SendCommandAsync("hello");
Console.WriteLine($"結果: {result}, 訊息: {message}");
// 發送串流命令
await client.SendStreamCommandAsync("process", streamMessage =>
{
Console.WriteLine($"[{streamMessage.Type}] {streamMessage.Content}");
}, "arg1", "arg2");
配置選項
public class PipeLineOptions
{
/// <summary>一般命令管道名稱</summary>
public string CommandPipeName { get; set; } = "CJF.NamedPipe.Command.Pipe";
/// <summary>串流命令管道名稱</summary>
public string StreamPipeName { get; set; } = "CJF.NamedPipe.Stream.Pipe";
/// <summary>最大客戶端數量</summary>
public int MaxClients { get; set; } = -1; // -1 表示無限制
/// <summary>連接超時時間(毫秒)</summary>
public int ConnectionTimeoutMs { get; set; } = 5000;
/// <summary>讀寫超時時間(毫秒)</summary>
public int ReadWriteTimeoutMs { get; set; } = 5000;
/// <summary>服務標誌文件路徑</summary>
public string ServiceFlagFilePath { get; set; } = "...";
/// <summary>應用程式資料路徑</summary>
public string ApplicationDataPath { get; set; } = "...";
}
使用 appsettings.json 配置
{
"PipeLine": {
"CommandPipeName": "MyApp.Command.Pipe",
"StreamPipeName": "MyApp.Stream.Pipe",
"MaxClients": 10,
"ConnectionTimeoutMs": 5000,
"ReadWriteTimeoutMs": 5000
}
}
builder.ConfigureServices((context, services) =>
{
// 從配置文件綁定選項
services.Configure<PipeLineOptions>(context.Configuration.GetSection("PipeLine"));
services.AddPipeLineService(CommandHandler, StreamHandler);
});
擴展方法
IServiceCollection 擴展
// 基本註冊
services.AddPipeLineService();
// 註冊命令處理器
services.AddPipeLineService(commandHandler, streamHandler);
// 配置選項 + 命令處理器
services.AddPipeLineService(options =>
{
options.CommandPipeName = "Custom.Pipe";
}, commandHandler, streamHandler);
IHostBuilder 擴展
// 使用 HostBuilder
hostBuilder.UsePipeLineService(options =>
{
options.CommandPipeName = "MyApp.Command.Pipe";
options.StreamPipeName = "MyApp.Stream.Pipe";
});
錯誤處理
var (result, message) = await client.SendCommandAsync("test");
switch (result)
{
case PipeResults.Success:
Console.WriteLine($"成功: {message}");
break;
case PipeResults.ServiceNotRunning:
Console.WriteLine("服務未運行");
break;
case PipeResults.ConnectionError:
Console.WriteLine("連接錯誤");
break;
case PipeResults.Timeout:
Console.WriteLine("操作超時");
break;
case PipeResults.CommandError:
Console.WriteLine($"命令錯誤: {message}");
break;
default:
Console.WriteLine($"未知錯誤: {message}");
break;
}
測試
專案包含完整的測試套件:
# 運行所有測試
dotnet test CJF.NamedPipe.Tests
# 運行特定測試
dotnet test CJF.NamedPipe.Tests --filter "TestMethod=PipeLineProvider_ShouldCreateClientAndServer"
示例專案
查看 CJF.NamedPipe.Example
專案以獲得完整的使用示例:
# 運行服務器
cd CJF.NamedPipe.Example
dotnet run
# 在另一個終端運行客戶端
dotnet run client
架構說明
核心組件
- IPipeLineProvider: 命名管道提供者介面
- PipeLineProvider: 命名管道提供者實作
- PipeServer: 命名管道服務器
- PipeClient: 命名管道客戶端
- PipeLineService: 後台服務實作
- PipeLineOptions: 配置選項類別
訊息類型
- CommandMessage: 命令訊息
- StreamMessage: 串流訊息
- StreamMessageTypes: 串流訊息類型枚舉
委託類型
- PipeCommandHandler: 一般命令處理器委託
- PipeStreamCommandHandler: 串流命令處理器委託
版本歷史
v1.00.10 (2025-01-18)
- 🎉 初始版本發布
- ✅ 支援 ASP.NET Core DI 模式
- ✅ 雙管道架構實作
- ✅ 完整的測試覆蓋
- ✅ 程式碼優化和重構
- ✅ 現代化的錯誤處理機制
授權
MIT License
貢獻
歡迎提交 Issue 和 Pull Request!
支援
如有問題,請在 GitHub 上提交 Issue。
Product | Versions 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 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.
-
net8.0
- Microsoft.Extensions.DependencyInjection (>= 9.0.7)
- Microsoft.Extensions.Hosting (>= 9.0.7)
- Microsoft.Extensions.Logging (>= 9.0.7)
- Microsoft.Extensions.Options (>= 9.0.7)
- Microsoft.Extensions.Options.ConfigurationExtensions (>= 9.0.7)
- System.IO.Pipes (>= 4.3.0)
NuGet packages (1)
Showing the top 1 NuGet packages that depend on CJF.NamedPipe:
Package | Downloads |
---|---|
CJF.NamedPipe.Logging
NamedPipe Services |
GitHub repositories
This package is not used by any popular GitHub repositories.
First Release