FileShare Attachments

Introduction

Uses the FileShare Attachments extension to store attachments for messages.

Code Walk-through

Add to EndpointConfiguration

Enable attachments for an endpoint. A directory (or network share_ and a time to keep are required.

endpointConfiguration.EnableInstallers();
endpointConfiguration.EnableAttachments(
    fileShare: @"..\..\..\..\storage",
    timeToKeep: TimeToKeep.Default);

Send a message with an attachment

var sendOptions = new SendOptions();
sendOptions.RouteToThisEndpoint();
var attachments = sendOptions.Attachments();
attachments.Add(
    name: "attachmentName",
    streamFactory: () =>
    {
        return File.OpenRead("fileToStream.txt");
    });

await endpoint.Send(new MyMessage(), sendOptions)
    .ConfigureAwait(false);

Read an attachment for a received message

public class MyHandler :
    IHandleMessages<MyMessage>
{
    static ILog log = LogManager.GetLogger<MyHandler>();

    public Task Handle(MyMessage message, IMessageHandlerContext context)
    {
        log.Info("MyMessage received. Attachment will be streamed to the console.");
        var attachments = context.Attachments();
        return attachments.ProcessStream(
            name: "attachmentName",
            action: async stream =>
            {
                using (var reader = new StreamReader(stream))
                {
                    while (true)
                    {
                        var line = await reader.ReadLineAsync()
                            .ConfigureAwait(false);
                        if (line == null)
                        {
                            break;
                        }

                        Console.WriteLine(line);
                    }
                }
            });
    }
}

Last modified