Skip to content

Client

The library provides a TCP/UDP client that can be used to communicate with a server. The client automatically establishes a transport connection and creates a channel instance for it. Connection drops are automatically handled and the client will attempt to reconnect using an exponential backoff strategy. When a reconnect occurs, a new channel instance is created.

Clients use the same channel configurations as listeners but require additional registration.

IServiceCollection services = ...;

/*
this registers a default client with the default channel configuration
*/
services.AddChannelsClient( "tcp://example.host:8080" );

/*
we could also register the default client with a named channel configuration
*/
// services.AddChannelsClient( "channel1", "tcp://example.host:8080" );

/*
inject the `IChannelFactory` interface to create a client instance.
*/
public class MyClient
{
    private readonly IChannelsClient client;

    public MyClient( IChannelFactory channelFactory )
    {
        client = channelFactory.CreateClient();
    }

    public Task ExecuteAsync()
    {
        // ...
    }
}

If you need multiple clients with different channel configurations, register them as named clients.

IServiceCollection services = ...;

/*
this registers a named client (client1) with the default channel configuration
*/
services.AddChannelsNamedClient( "client1", "tcp://example.host:8080" );

/*
this registers a named client (client2) with a named channel configuration (channel1)
*/
services.AddChannelsNamedClient( "client2", "channel1", "tcp://example.host:8080" );

// ...

public class MyClient
{
    private readonly IChannelsClient client1;

    public MyClient( IChannelFactory channelFactory )
    {
        client1 = channelFactory.CreateClient( "client1" );
    }

    public Task ExecuteAsync()
    {
        // ...
    }
}

Note

Channel name and client name are different concepts. The channel name identifies the channel configuration, while the client name identifies a registered client instance.