spkl.CLI.IPC 2.0.0

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

// Install spkl.CLI.IPC as a Cake Tool
#tool nuget:?package=spkl.CLI.IPC&version=2.0.0                

CLI.IPC

A .NET library that helps you implement a client/server architecture for command line interface applications. Using spkl.CLI.IPC, you can let your CLI application delegate its workload to a separate server process.

How does it work? When a user starts your client application, it sends its working directory, command line arguments and process ID to the server application. While processing the request, the server application can send console outputs (standard and error) back to the client. At the end, the server sends an exit code and closes the connection.

spkl.CLI.IPC was designed for clients and servers that run on the same machine, but because it is based on sockets, network communication between different machines can also be used.

Example Usage

Server

Start a server by calling Host.Start and supplying the following information:

  • How will the connection be established (ITransport)? Any streaming socket is supported; support for TCP loopback and Unix Domain Sockets* is built-in.
  • What will happen when a client connects (IClientConnectionHandler)?

* Unix Domain Sockets are not supported for .NET Standard 2.0 or before Windows 10.

In this example, lets implement a fictional database dump command:

static void Main(string[] args)
{
    Host host = Host.Start(new UdsTransport("path/to/socket_file"), new MyClientConnectionHandler());
    // [ wait until it is time to shut down the server ]
    host.Shutdown();
    host.WaitUntilAllClientsDisconnected();
}

private class MyClientConnectionHandler : IClientConnectionHandler
{
    public void HandleCall(ClientConnection connection)
    {
        if (connection.Properties.Arguments is [_, "dump-db", string fileName])
        {
            connection.Out.WriteLine("Dumping database to file...");
            Db.DumpTo(Path.Combine(connection.Properties.CurrentDirectory, fileName));
            connection.Exit(0);
            return;
        }

        connection.Error.WriteLine("Unknown command.");
        connection.Exit(1);
    }

    // [...]
}

Client

Implementing a client can be as easy as this one-liner:

Client.Attach(
    new UdsTransport("path/to/socket_file"),
    new DefaultHostConnectionHandler());

Just like for the server, you need to specify connection and behavior information (ITransport, IHostConnectionHandler2).

For most scenarios, the built-in DefaultHostConnectionHandler can be used. It writes the received outputs to the console and exits the application with the received exit code when the server closes the connection.

Other Features

SingletonApp: Starting a Server On Demand

A client application obviously can't connect to a server when the server is not running. If the server should be started on demand, i.e. when a client wants to connect, the SingletonApp class can help.

With SingletonApp, you can ensure that only one instance of the server is started, even when multiple clients ask for it simultaneously.

Usage as a client application: Call RequestInstance to ensure that an application instance is running before connecting to it.

IStartupBehavior startupBehavior = [...];
new SingletonApp(startupBehavior).RequestInstance();

Client.Attach([...]);

Usage as a server application: Call ReportInstanceRunning when ready for incoming connections. Call ShutdownInstance before no longer accepting connections.

IStartupBehavior startupBehavior = [...];
SingletonApp singletonApp = new(startupBehavior);

Host host = Host.Start([...]);
singletonApp.ReportInstanceRunning();
// [...]
singletonApp.ShutdownInstance();
host.Shutdown();

Using the IStartupBehavior interface, you can customize the following aspects:

  • How a server instance is started. Typically, by starting a new process.
  • Which time period is used for polling whether a server is starting or running.
  • After what timeout the RequestInstance or ReportInstanceRunning methods will fail.
  • Which file path is used to determine server state (because SingletonApp uses file-based locking). This can be used to provide a server process per machine, per user, or arbitrarily.

AutoTransportSingletonApp: On Demand and Even Easier

AutoTransportSingletonApp does everything that SingletonApp does, plus it automatically connects your server and your client so you don't have to manage TCP ports or Unix Domain Socket paths.

Simply use the value of the AutoTransportSingletonApp.Transport property for the call to Host.Start and Client.Attach, and a connection can be established.

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 was computed.  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 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.
  • .NETStandard 2.0

    • No dependencies.
  • net6.0

    • No dependencies.

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
2.3.0 122 5/31/2024
2.2.0 108 5/30/2024
2.1.0 107 5/1/2024
2.0.2-alpha.0.13 41 5/1/2024
2.0.2-alpha.0.3 49 5/1/2024
2.0.1 116 4/22/2024
2.0.0 113 4/20/2024
1.2.1 113 4/17/2024
1.2.0 120 4/14/2024
1.1.0 123 2/18/2024
1.0.0 117 1/21/2024

- BREAKING: Introduced and used interfaces for all public-facing types to facilitate easier unit testing for library users. Provided public ctors for exceptions.
- BREAKING: Shortened names of SingletonApplication and SingleApplicationException to SingletonApp and SingletonAppException, respectively.
- Introduced the AutoTransportSingletonApp class: This lets you automatically use the correct ITransport objects to connect server and client.
- Introduced new overloads for the Host.WaitUntil[...] methods accepting a CancellationToken.
- Allowed usage of the Host.WaitUntilAllClientsDisconnected methods was clarified. An InvalidOperationException is now thrown when misused.