예제 #1
0
    public override async Task Invoke(IInvokeHandlerContext context, Func <Task> next)
    {
        #endregion

        #region write-stream-properties-back

        var message          = context.MessageBeingHandled;
        var streamsToCleanUp = new List <FileStream>();
        foreach (var property in StreamStorageHelper
                 .GetStreamProperties(message))
        {
            var    headerKey = StreamStorageHelper.GetHeaderKey(message, property);
            string dataBusKey;
            // only attempt to process properties that have an associated header
            var key = $"NServiceBus.PropertyStream.{headerKey}";
            if (!context.Headers.TryGetValue(key, out dataBusKey))
            {
                continue;
            }

            var filePath = Path.Combine(location, dataBusKey);

            // If the file doesn't exist then something has gone wrong with the file share.
            // Perhaps the file has been manually deleted.
            // For safety send the message to the error queue
            if (!File.Exists(filePath))
            {
                var format = $"Expected a file to exist in '{filePath}'. It is possible the file has been prematurely cleaned up.";
                throw new Exception(format);
            }
            var fileStream = File.OpenRead(filePath);
            property.SetValue(message, fileStream);
            streamsToCleanUp.Add(fileStream);
        }

        #endregion

        #region cleanup-after-nested-action

        await next()
        .ConfigureAwait(false);

        // Clean up all the temporary streams after handler processing
        // via the "next()" delegate has occurred
        foreach (var fileStream in streamsToCleanUp)
        {
            fileStream.Dispose();
        }

        #endregion
    }
    public override async Task Invoke(IOutgoingLogicalMessageContext context, Func <Task> next)
    {
        #endregion
        #region copy-stream-properties-to-disk
        var timeToBeReceived = TimeSpan.MaxValue;
        DiscardIfNotReceivedBefore constraint;

        if (context.Extensions.TryGetDeliveryConstraint(out constraint))
        {
            timeToBeReceived = constraint.MaxTime;
        }

        var message = context.Message.Instance;

        foreach (var property in StreamStorageHelper.GetStreamProperties(message))
        {
            var sourceStream = (Stream)property.GetValue(message, null);

            // Ignore null stream properties
            if (sourceStream == null)
            {
                continue;
            }
            var fileKey = GenerateKey(timeToBeReceived);

            var filePath = Path.Combine(location, fileKey);
            Directory.CreateDirectory(Path.GetDirectoryName(filePath));

            using (var target = File.OpenWrite(filePath))
            {
                await sourceStream.CopyToAsync(target)
                .ConfigureAwait(false);
            }

            // Reset the property to null so no other serializer attempts to use the property
            property.SetValue(message, null);

            // Dispose of the stream
            sourceStream.Dispose();

            // Store the header so on the receiving endpoint the file name is known
            var headerKey = StreamStorageHelper.GetHeaderKey(message, property);
            context.Headers[$"NServiceBus.PropertyStream.{headerKey}"] = fileKey;
        }

        await next()
        .ConfigureAwait(false);

        #endregion
    }
예제 #3
0
    public void Invoke(IncomingContext context, Action next)
    {
        #endregion
        #region write-stream-properties-back
        object            message          = context.IncomingLogicalMessage.Instance;
        List <FileStream> streamsToCleanUp = new List <FileStream>();
        foreach (PropertyInfo property in StreamStorageHelper
                 .GetStreamProperties(message))
        {
            string headerKey = StreamStorageHelper.GetHeaderKey(message, property);
            string dataBusKey;
            //only attempt to process properties that have an associated header
            string key = "NServiceBus.PropertyStream." + headerKey;
            if (!context.IncomingLogicalMessage.Headers.TryGetValue(key, out dataBusKey))
            {
                continue;
            }

            string filePath = Path.Combine(location, dataBusKey);

            // If the file doesnt exist then something has gone wrong with the file share.
            // Perhaps he file has been manually deleted.
            // For safety send the message to the error queue
            if (!File.Exists(filePath))
            {
                string format = string.Format("Expected a file to exist in '{0}'. It is possible the file has been prematurely cleaned up.", filePath);
                throw new Exception(format);
            }
            FileStream fileStream = File.OpenRead(filePath);
            property.SetValue(message, fileStream);
            streamsToCleanUp.Add(fileStream);
        }
        #endregion

        #region cleanup-after-nested-action
        next();
        // Clean up all the temporary streams after handler processing
        // via the "next()" delegate has occurred
        foreach (FileStream fileStream in streamsToCleanUp)
        {
            fileStream.Dispose();
        }
        #endregion
    }
예제 #4
0
    public void Invoke(SendLogicalMessageContext context, Action next)
    {
        #endregion
        #region copy-stream-properties-to-disk
        LogicalMessage logicalMessage   = context.MessageToSend;
        TimeSpan       timeToBeReceived = logicalMessage.Metadata.TimeToBeReceived;

        object message = logicalMessage.Instance;

        foreach (PropertyInfo property in StreamStorageHelper.GetStreamProperties(message))
        {
            Stream sourceStream = (Stream)property.GetValue(message, null);

            //Ignore null stream properties
            if (sourceStream == null)
            {
                continue;
            }
            string fileKey = GenerateKey(timeToBeReceived);

            string filePath = Path.Combine(location, fileKey);
            Directory.CreateDirectory(Path.GetDirectoryName(filePath));

            using (FileStream target = File.OpenWrite(filePath))
            {
                sourceStream.CopyTo(target);
            }

            //Reset the property to null so no other serializer attempts to use the property
            property.SetValue(message, null);

            //Dispose of the stream
            sourceStream.Dispose();

            //Store the header so on the receiving endpoint the file name is known
            string headerKey = StreamStorageHelper.GetHeaderKey(message, property);
            logicalMessage.Headers["NServiceBus.PropertyStream." + headerKey] = fileKey;
        }

        next();
        #endregion
    }