async Task ProcessAttachment(TimeSpan?timeToBeReceived, SqlConnection connection, SqlTransaction transaction, string messageId, OutgoingStream outgoingStream, string name)
    {
        var outgoingStreamTimeToKeep = outgoingStream.TimeToKeep ?? endpointTimeToKeep;
        var timeToKeep = outgoingStreamTimeToKeep(timeToBeReceived);
        var expiry     = DateTime.UtcNow.Add(timeToKeep);

        try
        {
            await Process(connection, transaction, messageId, outgoingStream, name, expiry);
        }
        finally
        {
            outgoingStream.Cleanup?.Invoke();
        }
    }
    async Task Process(SqlConnection connection, SqlTransaction transaction, string messageId, OutgoingStream outgoingStream, string name, DateTime expiry)
    {
        if (outgoingStream.AsyncStreamFactory != null)
        {
            var stream = await outgoingStream.AsyncStreamFactory().ConfigureAwait(false);
            await ProcessStream(connection, transaction, messageId, name, expiry, stream);

            return;
        }

        if (outgoingStream.StreamFactory != null)
        {
            await ProcessStream(connection, transaction, messageId, name, expiry, outgoingStream.StreamFactory());

            return;
        }

        if (outgoingStream.StreamInstance != null)
        {
            await ProcessStream(connection, transaction, messageId, name, expiry, outgoingStream.StreamInstance);

            return;
        }
        if (outgoingStream.AsyncBytesFactory != null)
        {
            var bytes = await outgoingStream.AsyncBytesFactory().ConfigureAwait(false);

            await persister.SaveBytes(connection, transaction, messageId, name, expiry, bytes)
            .ConfigureAwait(false);

            return;
        }

        if (outgoingStream.BytesFactory != null)
        {
            await persister.SaveBytes(connection, transaction, messageId, name, expiry, outgoingStream.BytesFactory())
            .ConfigureAwait(false);

            return;
        }

        if (outgoingStream.BytesInstance != null)
        {
            await persister.SaveBytes(connection, transaction, messageId, name, expiry, outgoingStream.BytesInstance)
            .ConfigureAwait(false);

            return;
        }
        throw new Exception("No matching way to handle outgoingStream.");
    }