Service Bus Explorer

The Service Bus Explorer is a tool that you can use to manage and test the entities contained in an Azure Service Bus namespace.

C# (9.0 MB)
 
 
 
 
 
4.8 Star
(75)
88,497 times
Add to favorites
6/21/2017
E-mail Twitter del.icio.us Digg Facebook

Solution explorer

C#
#region Copyright
//=======================================================================================
// Microsoft Azure Customer Advisory Team 
//
// This sample is supplemental to the technical guidance published on my personal
// blog at http://blogs.msdn.com/b/paolos/. 
// 
// Author: Paolo Salvatori
//=======================================================================================
// Copyright (c) Microsoft Corporation. All rights reserved.
// 
// LICENSED UNDER THE APACHE LICENSE, VERSION 2.0 (THE "LICENSE"); YOU MAY NOT USE THESE 
// FILES EXCEPT IN COMPLIANCE WITH THE LICENSE. YOU MAY OBTAIN A COPY OF THE LICENSE AT 
// http://www.apache.org/licenses/LICENSE-2.0
// UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING, SOFTWARE DISTRIBUTED UNDER THE 
// LICENSE IS DISTRIBUTED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 
// KIND, EITHER EXPRESS OR IMPLIED. SEE THE LICENSE FOR THE SPECIFIC LANGUAGE GOVERNING 
// PERMISSIONS AND LIMITATIONS UNDER THE LICENSE.
//=======================================================================================
#endregion

#region Using Directives

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Globalization;
using System.Runtime.Serialization;
using System.Xml.Serialization;
using Microsoft.ServiceBus.Messaging;
using Newtonsoft.Json;
#endregion

namespace Microsoft.WindowsAzure.CAT.ServiceBusExplorer
{
    [Serializable]
    public enum OnOff
    {
        [XmlEnum("0")]
        Off,
        [XmlEnum("1")]
        On
    }

    [Serializable]
    [XmlType(TypeName = "event", Namespace = "http://schemas.microsoft.com/servicebusexplorer")]
    [XmlRoot(ElementName = "event", Namespace = "http://schemas.microsoft.com/servicebusexplorer", IsNullable = false)]
    [DataContract(Name = "event", Namespace = "http://schemas.microsoft.com/servicebusexplorer")]
    public class OnOffDeviceEvent
    {
        /// <summary>
        /// Gets or sets the device id.
        /// </summary>
        [XmlElement(ElementName = "eventId", Namespace = "http://schemas.microsoft.com/servicebusexplorer")]
        [JsonProperty(PropertyName = "eventId", Order = 1)]
        public int EventId { get; set; }

        /// <summary>
        /// Gets or sets the device id.
        /// </summary>
        [XmlElement(ElementName = "deviceId", Namespace = "http://schemas.microsoft.com/servicebusexplorer")]
        [JsonProperty(PropertyName = "deviceId", Order = 2)]
        public int DeviceId { get; set; }

        /// <summary>
        /// Gets or sets the device value.
        /// </summary>
        [XmlElement(ElementName = "value", Namespace = "http://schemas.microsoft.com/servicebusexplorer")]
        [JsonProperty(PropertyName = "value", Order = 3)]
        public OnOff Value { get; set; }

        /// <summary>
        /// Gets or sets the event timestamp.
        /// </summary>
        [XmlElement(ElementName = "timestamp", Namespace = "http://schemas.microsoft.com/servicebusexplorer")]
        [JsonProperty(PropertyName = "timestamp", Order = 4)]
        public DateTime Timestamp { get; set; }
    }

    public class OnOffDeviceEventDataGenerator : IEventDataGenerator, IDisposable
    {
        #region Public Constants
        //***************************
        // Constants
        //***************************
        private const string GeneratorProperties = "Generator Properties";
        private const string CustomProperties = "Custom Properties";
        private const string MinDeviceIdDescription = "Gets or sets the minimum device id.";
        private const string MaxDeviceIdDescription = "Gets or sets the maximum device id.";
        private const string AlertPercentageDescription = "Gets or sets the percentage of alert states generated by the component.";
        private const string AlertStateDescription = "Gets or sets the alert state.";
        private const string MessageFormatDescription = "Gets or sets the message format: Json or Xml.";
        private const string CityDescription = "Gets or sets the city.";
        private const string CountryDescription = "Gets or sets the country.";

        //***************************
        // Formats
        //***************************
        private const string ExceptionFormat = "Exception: {0}";
        private const string InnerExceptionFormat = "InnerException: {0}";
        private const string EventDataCreatedFormat = "[ThresholdDeviceEventDataGenerator] {0} objects have been successfully created.";
        #endregion

        #region Public Static Fields
        public static int EventId;
        #endregion

        #region Public Constructor
        public OnOffDeviceEventDataGenerator()
        {
            MinDeviceId = 1;
            MaxDeviceId = 100;
            AlertPercentage = 10;
            AlertState = (int)OnOff.Off;
            City = "Milan";
            Country = "Italy";
        } 
        #endregion

        #region IEventDataGenerator Methods
        public IEnumerable<EventData> GenerateEventDataCollection(int eventDataCount, WriteToLogDelegate writeToLog)
        {
            if (eventDataCount < 0)
            {
                return null;
            }
            var random = new Random();
            var list = new List<EventData>();
            for (var i = 0; i < eventDataCount; i++)
            {
                try
                {
                    var normalState = AlertState == (int)OnOff.Off ? OnOff.On : OnOff.Off;
                    var alertState = AlertState == (int)OnOff.Off ? OnOff.Off : OnOff.On;
                    var payload = new OnOffDeviceEvent
                    {
                        EventId = EventId++,
                        DeviceId = random.Next(MinDeviceId, MaxDeviceId + 1),
                        Value = random.Next(1, 101) <= AlertPercentage ? alertState : normalState,
                        Timestamp = DateTime.UtcNow
                    };
                    var eventData = new EventData((MessageFormat == MessageFormat.Json
                        ? JsonSerializerHelper.Serialize(payload)
                        : XmlSerializerHelper.Serialize(payload)).ToMemoryStream())
                    {
                        PartitionKey = payload.DeviceId.ToString(CultureInfo.InvariantCulture),

                    };
                    eventData.Properties.Add("eventId", payload.EventId);
                    eventData.Properties.Add("deviceId", payload.DeviceId);
                    eventData.Properties.Add("value", (int)payload.Value);
                    eventData.Properties.Add("time", DateTime.UtcNow.Ticks);
                    eventData.Properties.Add("city", City);
                    eventData.Properties.Add("country", Country);
                    list.Add(eventData);
                }
                catch (Exception ex)
                {
                    if (!string.IsNullOrWhiteSpace(ex.Message))
                    {
                        writeToLog(string.Format(CultureInfo.CurrentCulture, ExceptionFormat, ex.Message));
                    }
                    if (ex.InnerException != null && !string.IsNullOrWhiteSpace(ex.InnerException.Message))
                    {
                        writeToLog(string.Format(CultureInfo.CurrentCulture, InnerExceptionFormat, ex.InnerException.Message));
                    }
                }
            }
            if (writeToLog != null)
            {
                writeToLog(string.Format(EventDataCreatedFormat, list.Count));
            }
            return list;
        } 
        #endregion

        #region IDisposable Methods
        public void Dispose()
        {
        }
        #endregion

        #region Public Properties
        [Category(GeneratorProperties)]
        [Description(MinDeviceIdDescription)]
        [DefaultValue(1)]
        public int MinDeviceId { get; set; }

        [Category(GeneratorProperties)]
        [Description(MaxDeviceIdDescription)]
        [DefaultValue(100)]
        public int MaxDeviceId { get; set; }

        [Category(GeneratorProperties)]
        [Description(AlertPercentageDescription)]
        public int AlertPercentage { get; set; }

        [Category(GeneratorProperties)]
        [Description(AlertStateDescription)]
        public int AlertState { get; set; }

        [Category(GeneratorProperties)]
        [Description(MessageFormatDescription)]
        public MessageFormat MessageFormat { get; set; }

        [Category(CustomProperties)]
        [Description(CityDescription)]
        public string City { get; set; }

        [Category(CustomProperties)]
        [Description(CountryDescription)]
        public string Country { get; set; }
        #endregion
    }
}