H.Formatters 2.0.51

There is a newer version of this package available.
See the version list below for details.
dotnet add package H.Formatters --version 2.0.51                
NuGet\Install-Package H.Formatters -Version 2.0.51                
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="H.Formatters" Version="2.0.51" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add H.Formatters --version 2.0.51                
#r "nuget: H.Formatters, 2.0.51"                
#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.
// Install H.Formatters as a Cake Addin
#addin nuget:?package=H.Formatters&version=2.0.51

// Install H.Formatters as a Cake Tool
#tool nuget:?package=H.Formatters&version=2.0.51                

Async Named Pipe Wrapper for .NET Standard 2.0

Language License Requirements Build Status

A simple, easy to use, strongly-typed, async wrapper around .NET named pipes.

Features

  • Create named pipe servers that can handle multiple client connections simultaneously.
  • Send strongly-typed messages between clients and servers: any serializable .NET object can be sent over a pipe and will be automatically serialized/deserialized, including cyclical references and complex object graphs.
  • Async
  • Requires .NET Standard 2.0
  • Supports large messages - up to 300 MiB.
  • Server restart automatically
  • Automatically wait for the release of the pipe for the server, if it is already in use
  • Automatically waiting for a server pipe creating when client connecting
  • Automatic reconnect with a given interval and at each client.WriteAsync, if necessary
  • Supports variable formatters, default - BinaryFormatter which uses System.Runtime.Serialization.BinaryFormatter inside
  • Also available ready formatters in separate nuget packages: H.Formatters.Newtonsoft.Json, H.Formatters.System.Text.Json and H.Formatters.Ceras
  • Supports PipeAccessRule's(see H.Pipes.AccessControl nuget package) or more complex code to access using the PipeServer.PipeStreamInitializeAction property

Nuget

NuGet NuGet NuGet NuGet NuGet

// All clients and servers that do not need support AccessControl.
Install-Package H.Pipes

// Servers that need support AccessControl.
Install-Package H.Pipes.AccessControl

// If you want to transfer any data that can be serialized/deserialized in json using Newtonsoft.Json.
Install-Package H.Formatters.Newtonsoft.Json

// If you want to transfer any data that can be serialized/deserialized in json using System.Text.Json.
Install-Package H.Formatters.System.Text.Json

// If you want to transfer any data that can be serialized/deserialized in binary using Ceras.
Install-Package H.Formatters.Ceras

Usage

Server:

await using var server = new PipeServer<MyMessage>(pipeName);
server.ClientConnected += async (o, args) =>
{
    Console.WriteLine($"Client {args.Connection.PipeName} is now connected!");

    await args.Connection.WriteAsync(new MyMessage
    {
        Text = "Welcome!"
    });
};
server.ClientDisconnected += (o, args) =>
{
    Console.WriteLine($"Client {args.Connection.PipeName} disconnected");
};
server.MessageReceived += (sender, args) =>
{
    Console.WriteLine($"Client {args.Connection.PipeName} says: {args.Message}");
};
server.ExceptionOccurred += (o, args) => OnExceptionOccurred(args.Exception);

await server.StartAsync();

await Task.Delay(Timeout.InfiniteTimeSpan);

Client:

await using var client = new PipeClient<MyMessage>(pipeName);
client.MessageReceived += (o, args) => Console.WriteLine("MessageReceived: " + args.Message);
client.Disconnected += (o, args) => Console.WriteLine("Disconnected from server");
client.Connected += (o, args) => Console.WriteLine("Connected to server");
client.ExceptionOccurred += (o, args) => OnExceptionOccurred(args.Exception);

await client.ConnectAsync();

await client.WriteAsync(new MyMessage
{
    Text = "Hello!",
});

await Task.Delay(Timeout.InfiniteTimeSpan);

Notes:

  • To use the server inside the WinForms/WPF/Other UI application, use Task.Run() or any alternative.
  • Be careful and call Dispose before closing the program/after the end of use. Pipes are system resources and you might have problems restarting the server if you don't properly clean up the resources.

Custom Formatters

Since BinaryFormatter is used by default, you should check out this article: https://docs.microsoft.com/en-us/dotnet/standard/serialization/binaryformatter-security-guide

Install-Package H.Formatters.Newtonsoft.Json
Install-Package H.Formatters.System.Text.Json
Install-Package H.Formatters.Ceras
using H.Formatters;

await using var server = new PipeServer<MyMessage>(pipeName, formatter: new NewtonsoftJsonFormatter());
await using var client = new PipeClient<MyMessage>(pipeName, formatter: new NewtonsoftJsonFormatter());

Access Control

⚠️ Note: this is only available for the Windows platform

Install-Package H.Pipes.AccessControl
using System.IO.Pipes;
using H.Pipes.AccessControl;

await using var server = new PipeServer<string>(pipeName);

// You can set PipeSecurity
var pipeSecurity = new PipeSecurity();
pipeSecurity.AddAccessRule(new PipeAccessRule(new SecurityIdentifier(WellKnownSidType.BuiltinUsersSid, null), PipeAccessRights.ReadWrite, AccessControlType.Allow));

server.SetPipeSecurity(pipeSecurity);

// or just add AccessRule's (Please be careful, the server will only consider AccessRules from the last call AddAccessRules())
server.AddAccessRules(new PipeAccessRule(new SecurityIdentifier(WellKnownSidType.BuiltinUsersSid, null), PipeAccessRights.ReadWrite, AccessControlType.Allow));

// or just
server.AllowUsersReadWrite();

Encryption

⚠️ Note: this is only available for the Windows platform

Install-Package H.Formatters.Inferno
using H.Formatters;

await using var server = new PipeServer<MyMessage>(pipeName, formatter: new SystemTextJsonFormatter());
server.EnableEncryption();

await using var client = new PipeClient<MyMessage>(pipeName, formatter: new SystemTextJsonFormatter());
client.EnableEncryption();

await client.ConnectAsync(source.Token).ConfigureAwait(false);
// Waits for key exchange.
await client.Connection!.WaitExchangeAsync();

server.ClientConnected += async (_, args) =>
{
    // Waits for key exchange.
    await args.Connection.WaitExchangeAsync();

    await args.Connection.WriteAsync(new MyMessage
    {
        Text = "Welcome!"
    }, source.Token).ConfigureAwait(false);
};

GetImpersonationUserName

server.ClientConnected += async (o, args) =>
{
    var name = args.Connection.GetImpersonationUserName();

    Console.WriteLine($"Client {name} is now connected!");
};

Inter-process communication

I recommend that you take a look at my other library if you plan on doing IPC. This is a SourceGenerator that will generate client and server code based on the presented interface.

Contacts

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 is compatible.  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 is compatible.  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 was computed.  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. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net451 is compatible.  net452 was computed.  net46 was computed.  net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  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.
  • .NETFramework 4.5.1

    • No dependencies.
  • .NETStandard 2.0

    • No dependencies.
  • net6.0

    • No dependencies.
  • net7.0

    • No dependencies.

NuGet packages (8)

Showing the top 5 NuGet packages that depend on H.Formatters:

Package Downloads
H.Formatters.BinaryFormatter

This package adds BinaryFormatter(based on System.Runtime.Serialization.Formatters.Binary.BinaryFormatter)

H.Formatters.Newtonsoft.Json

This package adds NewtonsoftJsonFormatter(based on Newtonsoft.Json)

H.Formatters.System.Text.Json

This package adds SystemTextJsonFormatter(based on System.Text.Json)

H.Formatters.MessagePack

This package adds MessagePack (based on neuecc/MessagePack-CSharp)

H.Formatters.Json

This package adds JsonFormatter(based on Newtonsoft.Json)

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
14.0.0 4,583 10/21/2024
2.2.2-dev.2 47 10/21/2024
2.2.1 893 10/19/2024
2.2.1-dev.2 72 10/19/2024
2.1.0-dev.322 763 1/20/2024
2.1.0-dev.318 152 12/18/2023
2.0.59 75,408 12/8/2023
2.0.56 3,050 11/27/2023
2.0.53 24,032 7/24/2023
2.0.51 12,364 5/25/2023
2.0.47 17,359 2/27/2023
2.0.46 1,863 2/26/2023
2.0.45 8,682 2/2/2023
2.0.44 10,106 1/10/2023
2.0.43 3,040 1/5/2023
2.0.42 37,249 9/14/2022
2.0.41 3,629 9/9/2022
2.0.40 5,468 8/26/2022
2.0.39 3,081 8/26/2022
2.0.38 13,901 6/2/2022
2.0.37 5,680 4/21/2022
2.0.35 74,366 3/17/2022
2.0.34 3,399 3/12/2022
2.0.33 3,315 3/12/2022
2.0.32 3,227 3/12/2022
2.0.31 4,180 3/11/2022
2.0.30 3,183 3/11/2022
2.0.29 3,203 3/11/2022
2.0.26 3,367 3/7/2022
2.0.25 3,262 3/6/2022
2.0.23 10,320 12/21/2021
2.0.22 1,908 12/21/2021
2.0.21 7,930 12/20/2021
2.0.20 1,930 12/20/2021
2.0.19 1,968 12/20/2021
2.0.18 1,977 12/20/2021
2.0.17 1,968 12/20/2021
2.0.16 1,913 12/20/2021
1.0.0.8 10,278 11/25/2021
1.0.0.7 8,895 11/24/2021
1.0.0.6 22,586 1/4/2021
1.0.0.5 14,032 12/9/2020
1.0.0.4 1,943 12/9/2020
1.0.0.3 6,205 11/22/2020
1.0.0.2 5,132 8/4/2020
1.0.0.1 10,523 1/28/2020
1.0.0 6,569 1/10/2020

⭐ Last 10 features:
- feat: Added PipeApplication.StartAsync(args). 2023-02-27
- feat: Added IPipeClient.CreatePipeStreamFunc. Closes #29. 2023-02-02
- feat: Added ability to set up CerasFormatter.InternalFormatter. 2023-01-10
- feat: To net7.0. Updated NuGet packages. 2023-01-05
- feat: Updated H.Pipes.AccessControl to use official net5/net6 PipeSecurity implementation. 2022-09-09
- feat: Added BinaryFormatter.InternalFormatter as public property. 2022-03-18
- feat(AccessControl): Enabled PipeApplication.StartAsync for non-windows system without AllowUsersReadWrite. 2022-03-12
- feat(Inferno): Added net5/net6 targets. Added SupportedOSPlatform attributes. 2022-03-12
- feat(Inferno): Added EnableEncryption optional exception action. 2022-03-12
- feat(AccessControl): Added SupportedOSPlatform attribute to SetPipeSecurity. 2022-03-11

🐞 Last 10 bug fixes:
- fix: Fixed bug with PipeStream.ReadAsync on non windows systems. 2023-05-26
- fix: Now EventExtensions is internal class. 2023-02-26
- fix: Fixed Connection.Disconnected unhandled exception bug. 2022-09-14
- fix: Fixed SemaphoreSlim usage. Fixes #24. 2022-08-26
- fix: Fixes #24. 2022-08-26
- fix: Added IOException ignoring for FlushAsync/WaitForPipeDrain. 2022-06-02
- fix: Fixed missing PipeStreamReader read check. Refactored. 2022-04-21
- fix(AccessControl): Fixed NET461 library to use Net Framework NamedPipeServerStream ctor. 2022-03-11
- fix: Fixed ValidatePublicKey. 2022-03-07
- fix: Fixed PipeClient.Connection set access. 2021-12-21