SocketCommand.Hosting 1.0.7

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

⚑ SocketCommand

NuGet NuGet License: MIT .NET

SocketCommand is a modern, extensible NuGet package that makes socket-based command communication simple and powerful in .NET applications.

Built on top of .NET hosting, it supports:

  • πŸ” Command-based duplex communication
  • 🧱 Entity serialization
  • πŸ”’ AES encryption
  • πŸ“¦ Compression
  • πŸ“‘ UDP-based peer discovery
  • πŸ’‰ Dependency Injection-friendly design

πŸ“¦ Installation

SocketCommand consists of two packages:

Package Purpose
SocketCommand.Abstractions 🎯 Lightweight project with interfaces and attributes. Add this wherever you define socket-bound entities.
SocketCommand.Hosting πŸš€ Core socket server/client logic. Add this to your startup project.
SocketCommand.Compression.7Zip πŸ—œοΈCompress messages using 7Zip/LZMA. Add this to your startup project.

Install via NuGet:

dotnet add package SocketCommand.Hosting
dotnet add package SocketCommand.Abstractions
dotnet add package SocketCommand.Compression.7Zip

βš™οΈ Configuration

Define your settings in appsettings.json:

{
  "SocketCommand": {
    "Port": 5000,
    "BufferSize": 1024,
    "UdpPort": 5052,
    "UdpSecret": "s3cr3t!",
    "AESKey": "jiwlQEjVDC67rIbte8n0XoZZzgXE4cSnRlj81YZaAf4=",
    "AESIV": "ivgVH9nmCwAafSK2jhyA1Q=="
  }
}
Property Description
Port TCP server port. Use 0 for a random port.
BufferSize Size of the read buffer. Default: 1024.
UdpPort Port for UDP discovery.
UdpSecret Shared secret to verify discovery packets. Must match on both ends.
AESKey / AESIV πŸ” AES encryption key and IV. Both must match across peers.

πŸ’„ Decorating Entities

Entities that are to be used as commands or responses must be decorated with the SocketCommandAttribute and OrderAttribute. The order attribute must batch on entities on both ends.

[SocketMessage]
public class TestEntity
{
    [Order(1)]
    public int Id { get; set; }

    [Order(2)]
    public string Name { get; set; }
}

πŸš€ Usage

Host Initialization

var builder = Host.CreateApplicationBuilder(args);

builder.AddSocketCommand()
    .With7ZipCompression()
    .WithAESEncryption()
    .WithCommand("ping", async (ISocketManager caller) =>
    {
        Console.WriteLine("ping");
        await caller.Send("ping");
    });

await builder.Build().StartAsync();

Connecting as Client

var connectionManager = host.Services.GetRequiredService<IConnectionManager>();
var discoveries = await connectionManager.Discover();

var found = discoveries.FirstOrDefault();
if (found != null)
{
    var connection = await connectionManager.ConnectTo(found.Address, found.Port);
    await connection.Send("testdata", new TestObject { Id = 3, Name = "Test" });
}

Full Example with DI and Logging

var builder = Host.CreateDefaultBuilder(args);

builder.AddSocketCommand(sb =>
{
    sb.WithAESEncryption()
      .With7ZipCompression()
      .WithUdpDiscovery()
      .WithCommand("ping", async (ISocketManager caller) =>
      {
          Console.WriteLine("ping");
          await caller.Send("ping");
      })
      .WithCommand("testdata", async (TestObject obj, ILogger<TestObject> logger) =>
      {
          logger.LogInformation("Received: {Id} - {Name}", obj.Id, obj.Name);
      });
});

await builder.Build().StartAsync();

🧩 Customization

You can plug in your own compression or encryption strategies:

builder.AddSocketCommand()
    .WithCompression<CustomCompressor>()
    .WithEncryption<CustomEncryptor>();

Implement:

  • ISocketMessageCompressor
  • ISocketMessageEncryption

and register them via generics or manual DI.

πŸ› οΈ Requirements

  • .NET 8
  • Works in console apps, Windows services, and ASP.NET Core projects
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 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.

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.7 144 5/29/2025
1.0.6 150 5/20/2025