コード例 #1
0
ファイル: AzureServiceBus.cs プロジェクト: quanliang2000/CQRS
        /// <summary>
        /// Extract any telemetry properties from the provided <paramref name="message"/>.
        /// </summary>
        protected virtual IDictionary <string, string> ExtractTelemetryProperties(BrokeredMessage message, string baseCommunicationType)
        {
            IDictionary <string, string> telemetryProperties = new Dictionary <string, string> {
                { "Type", baseCommunicationType }
            };
            object value;

            if (message.TryGetUserPropertyValue("Type", out value))
            {
                telemetryProperties.Add("MessageType", value.ToString());
            }
            if (message.TryGetUserPropertyValue("Source", out value))
            {
                telemetryProperties.Add("MessageSource", value.ToString());
            }
            if (message.TryGetUserPropertyValue("Source-Method", out value))
            {
                telemetryProperties.Add("MessageSourceMethod", value.ToString());
            }
            if (message.TryGetUserPropertyValue("CorrelationId", out value) && !telemetryProperties.ContainsKey("CorrelationId"))
            {
                telemetryProperties.Add("CorrelationId", value.ToString());
            }

            return(telemetryProperties);
        }
コード例 #2
0
ファイル: AzureServiceBus.cs プロジェクト: quanliang2000/CQRS
        /// <summary>
        /// Extract the signature from the provided <paramref name="message"/>.
        /// </summary>
        protected virtual string ExtractSignature(BrokeredMessage message)
        {
            object value;

            if (message.TryGetUserPropertyValue("Signature", out value))
            {
                return(value.ToString());
            }
            return(null);
        }
コード例 #3
0
ファイル: AzureBusHelper.cs プロジェクト: quanliang2000/CQRS
        public virtual void RefreshLock(IMessageReceiver client, CancellationTokenSource brokeredMessageRenewCancellationTokenSource, BrokeredMessage message, string type = "message")
#endif
        {
            Task.Factory.StartNewSafely(() =>
            {
                // The capturing of ObjectDisposedException is because even the properties can throw it.
                try
                {
                    object value;
                    string typeName = null;
                    if (message.TryGetUserPropertyValue("Type", out value))
                    {
                        typeName = value.ToString();
                    }

                    long loop = long.MinValue;
                    while (!brokeredMessageRenewCancellationTokenSource.Token.IsCancellationRequested)
                    {
                        // Based on LockedUntilUtc property to determine if the lock expires soon
                        // We lock for 45 seconds to ensure any thread based issues are mitigated.
#if NET452
                        if (DateTime.UtcNow > message.LockedUntilUtc.AddSeconds(-45))
#endif
#if NETSTANDARD2_0
                        if (DateTime.UtcNow > message.ExpiresAtUtc.AddSeconds(-45))
#endif
                        {
                            // If so, renew the lock
                            for (int i = 0; i < 10; i++)
                            {
                                try
                                {
                                    if (brokeredMessageRenewCancellationTokenSource.Token.IsCancellationRequested)
                                    {
                                        return;
                                    }
#if NET452
                                    message.RenewLock();
#endif
#if NETSTANDARD2_0
                                    client.RenewLockAsync(message).Wait(1500);
#endif
                                    try
                                    {
                                        Logger.LogDebug(string.Format("Renewed the {2} lock on {1} '{0}'.", message.MessageId, type, typeName));
                                    }
                                    catch
                                    {
                                        Trace.TraceError("Renewed the {2} lock on {1} '{0}'.", message.MessageId, type, typeName);
                                    }

                                    break;
                                }
                                catch (ObjectDisposedException)
                                {
                                    return;
                                }
                                catch (MessageLockLostException exception)
                                {
                                    try
                                    {
                                        Logger.LogWarning(string.Format("Renewing the {2} lock on {1} '{0}' failed as the message lock was lost.", message.MessageId, type, typeName), exception: exception);
                                    }
                                    catch
                                    {
                                        Trace.TraceError("Renewing the {2} lock on {1} '{0}' failed as the message lock was lost.\r\n{3}", message.MessageId, type, typeName, exception.Message);
                                    }
                                    return;
                                }
                                catch (Exception exception)
                                {
                                    try
                                    {
                                        Logger.LogWarning(string.Format("Renewing the {2} lock on {1} '{0}' failed.", message.MessageId, type, typeName), exception: exception);
                                    }
                                    catch
                                    {
                                        Trace.TraceError("Renewing the {2} lock on {1} '{0}' failed.\r\n{3}", message.MessageId, type, typeName, exception.Message);
                                    }
                                    if (i == 9)
                                    {
                                        return;
                                    }
                                }
                            }
                        }

                        if (loop++ % 5 == 0)
                        {
                            Thread.Yield();
                        }
                        else
                        {
                            Thread.Sleep(500);
                        }
                        if (loop == long.MaxValue)
                        {
                            loop = long.MinValue;
                        }
                    }
                    try
                    {
                        brokeredMessageRenewCancellationTokenSource.Dispose();
                    }
                    catch (ObjectDisposedException) { }
                }
                catch (ObjectDisposedException) { }
            }, brokeredMessageRenewCancellationTokenSource.Token);
        }