Custom DataBus implementation

Component: NServiceBus
NuGet Package NServiceBus (6.x)

Endpoints support sending and receiving large chunks of data via the DataBus.

It is possible to create a custom DataBus implementation. This is done by making use of the Features extension.

Implement the IDataBus interface.

This new class will provide the custom implementations for the Get and Put methods for the DataBus.

class CustomDataBus :
    IDataBus
{
    public Task<Stream> Get(string key)
    {
        Stream stream = File.OpenRead("blob.dat");
        return Task.FromResult(stream);
    }

    public async Task<string> Put(Stream stream, TimeSpan timeToBeReceived)
    {
        using (var destination = File.OpenWrite("blob.dat"))
        {
            await stream.CopyToAsync(destination)
                .ConfigureAwait(false);
        }
        return "the-key-of-the-stored-file-such-as-the-full-path";
    }

    public Task Start()
    {
        return Task.CompletedTask;
    }
}

This new implementation needs to be registered as a new feature.

Define a feature

Define a new feature that registers the custom DataBus implementation class.

class CustomDatabusFeature : Feature
{
    public CustomDatabusFeature()
    {
        DependsOn<DataBus>();
    }

    protected override void Setup(FeatureConfigurationContext context)
    {
        var dataBus = new CustomDataBus();
        context.Container.ConfigureComponent(b => dataBus, DependencyLifecycle.SingleInstance);
    }
}

Define a DataBusDefinition

Define a new class which inherits from the DataBusDefinition class.

class CustomDatabusDefinition : DataBusDefinition
{
    protected override Type ProvidedByFeature()
    {
        return typeof(CustomDatabusFeature);
    }
}

Configure the endpoint

Configure the endpoint to use the custom DataBus implementation instead of the default DataBus:

endpointConfiguration.UseDataBus(typeof(CustomDataBus));

Samples


Last modified