public void given_a_messaging_factory()
		{
			mf = TestConfigFactory.CreateMessagingFactory();
			var nm = TestConfigFactory.CreateNamespaceManager(mf);

			nm.TopicExists("my.topic.here").ShouldBeFalse();
		}
예제 #2
0
 public BrokeredTransport(string connectionString, string inputQueueName)
 {
     this.connectionString = connectionString;
     this.inputQueueName = inputQueueName;
     this.factory = MessagingFactory.CreateFromConnectionString(connectionString);
     this.queues = new ConcurrentDictionary<string, QueueClient>();
 }
예제 #3
0
 internal ResponseMessagePump(MessagingFactory messagingFactory, string replyQueueName, RequestResponseCorrelator requestResponseCorrelator, ILogger logger, int batchSize)
     : base(logger, batchSize)
 {
     _messagingFactory = messagingFactory;
     _replyQueueName = replyQueueName;
     _requestResponseCorrelator = requestResponseCorrelator;
 }
 public MulticastRequestMessagePump(MessagingFactory messagingFactory, IMulticastRequestBroker multicastRequestBroker, Type requestType, string applicationSharedSubscriptionName, ILogger logger, int batchSize) : base(logger, batchSize)
 {
     _messagingFactory = messagingFactory;
     _multicastRequestBroker = multicastRequestBroker;
     _requestType = requestType;
     _applicationSharedSubscriptionName = applicationSharedSubscriptionName;
 }
        public async Task<string> OpenAsync(CancellationToken cancellationToken)
        {
            var builder = new ServiceBusConnectionStringBuilder(_connectionString)
            {
                TransportType = TransportType.Amqp
            };
            _messagingFactory = MessagingFactory.CreateFromConnectionString(builder.ToString());
            _eventHubClient = _messagingFactory.CreateEventHubClient(_eventHubName);
            _consumerGroup = !string.IsNullOrEmpty(_consumerGroupName)
                ? _eventHubClient.GetConsumerGroup(_consumerGroupName)
                : _eventHubClient.GetDefaultConsumerGroup();

            _eventProcessorFactory = new EventProcessorFactory();
            _leaseRepository = new ReliableStateLeaseRepository(_reliableStateManager);
            _checkpointManager = new CheckpointManager(_leaseRepository); 

            var allocatedPartitions = await new EventHubPartitionPartitionAllocationStrategy(_serviceName, _partitionId)
                .AllocateAsync(_eventHubClient, new FabricClient());

            foreach (var partition in allocatedPartitions)
            {
                var lease = await _leaseRepository.GetOrCreateAsync(_connectionString, _consumerGroupName, _eventHubName, partition);

                await _consumerGroup.RegisterProcessorFactoryAsync(lease, _checkpointManager, _eventProcessorFactory);
            }

            return string.Concat(_eventHubName, " @ ", _connectionString);
        }
예제 #6
0
        public ServiceBusConnection(ServiceBusScaleoutConfiguration configuration, TraceSource traceSource)
        {
            _trace = traceSource;
            _connectionString = configuration.BuildConnectionString();

            try
            {
                _namespaceManager = NamespaceManager.CreateFromConnectionString(_connectionString);
                _factory = MessagingFactory.CreateFromConnectionString(_connectionString);

                if (configuration.RetryPolicy != null)
                {
                    _factory.RetryPolicy = configuration.RetryPolicy;
                }
                else
                {
                    _factory.RetryPolicy = RetryExponential.Default;
                }
            }
            catch (ConfigurationErrorsException)
            {
                _trace.TraceError("The configured Service Bus connection string contains an invalid property. Check the exception details for more information.");
                throw;
            }

            _backoffTime = configuration.BackoffTime;
            _idleSubscriptionTimeout = configuration.IdleSubscriptionTimeout;    
            _configuration = configuration;
        }
예제 #7
0
        protected static void AzureMessageConsumer(MessagingFactory f)
        {
            //use the already created messaging factory to create a msg receiver
            MessageReceiver testQueueReceiver = f.CreateMessageReceiver("colors");

            while (true)
            {
                using (BrokeredMessage retrievedMessage = testQueueReceiver.Receive())
                {

                    try
                    {
                        string msgResult = retrievedMessage.GetBody<string>();

                        //call SP to insert the data into the proper table
                        InsertSQL(msgResult);

                        Console.WriteLine("Message received: " + msgResult);
                        retrievedMessage.Complete();
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.ToString());
                        retrievedMessage.Abandon();
                    }
                }
            }
        }
예제 #8
0
        public async void Init(MessageReceived messageReceivedHandler) {
            this.random = new Random();

            //ServiceBusEnvironment.SystemConnectivity.Mode = ConnectivityMode.AutoDetect;
            
            // Tcp mode does not work when I run in a VM (VirtualBox) and the host 
            // is using a wireless connection. Hard coding to Http.
            ServiceBusEnvironment.SystemConnectivity.Mode = ConnectivityMode.Http;

            string connectionString = CloudConfigurationManager.GetSetting("Microsoft.ServiceBus.ConnectionString");
            this.factory = MessagingFactory.CreateFromConnectionString(connectionString);
            this.namespaceManager = NamespaceManager.CreateFromConnectionString(connectionString);

            if (!namespaceManager.TopicExists(topicName)) {
                namespaceManager.CreateTopic(topicName);
            }

            this.subscriptionName = Guid.NewGuid().ToString();

            // Not needed really, it's a GUID...
            if (!namespaceManager.SubscriptionExists(topicName, subscriptionName)) {
                namespaceManager.CreateSubscription(topicName, subscriptionName);
            }

            this.topicClient = factory.CreateTopicClient(topicName);

            this.subClient = factory.CreateSubscriptionClient(topicName, subscriptionName);

            while (true) {
                await ReceiveMessageTaskAsync(messageReceivedHandler);
            }
        }
예제 #9
0
        public DurableMessageSender(MessagingFactory messagingFactory, string serviceBusQueueOrTopicName)
        {
            this.messagingFactory = messagingFactory;
            this.sbusEntityName = serviceBusQueueOrTopicName;

            // Create a Service Bus queue client to send messages to the Service Bus queue.
            this.messageSender = this.messagingFactory.CreateMessageSender(this.sbusEntityName);

            // Create MSMQ queue if it doesn't exit. If it does, open the existing MSMQ queue.
            this.msmqQueueName = MsmqHelper.CreateMsmqQueueName(this.sbusEntityName, "SEND");
            this.msmqQueue = MsmqHelper.GetMsmqQueue(this.msmqQueueName);

            // Create MSMQ deadletter queue if it doesn't exit. If it does, open the existing MSMQ deadletter queue.
            this.msmqDeadletterQueueName = MsmqHelper.CreateMsmqQueueName(this.sbusEntityName, "SEND_DEADLETTER");
            this.msmqDeadletterQueue = MsmqHelper.GetMsmqQueue(this.msmqDeadletterQueueName);

            // Initialize wait time after durable client experienced a transient error.
            timerWaitTimeInMilliseconds = minTimerWaitTimeInMilliseconds;

            // FOR TESTING PURPOSE ONLY.
            this.faultInjector = new FaultInjector(enableFaultInjection);

            // Start receiving messages from the MSMQ queue.
            MsmqPeekBegin();
        }
        public void Connect()
        {
            Disconnect();

            _log.DebugFormat("Connecting '{0}'", _endpointAddress);

            if (_messagingFactory == null)
                _messagingFactory = _endpointAddress.MessagingFactoryFactory();

            // check if it's a queue or a subscription to subscribe either the queue or the subscription?
            if (_endpointAddress.QueueDescription != null)
            {
                _messageSender = _endpointAddress.CreateQueue()
                                                 .ContinueWith(t =>
                                                     {
                                                         t.Wait();
                                                         return
                                                             _messagingFactory.TryCreateMessageSender(
                                                                 _endpointAddress.QueueDescription, _prefetchCount)
                                                                              .Result;
                                                     })
                                                 .Result;
            }
            else
            {
                _messageSender = _messagingFactory.TryCreateMessageSender(_endpointAddress.TopicDescription)
                                                  .Result;
            }

            if (_messageSender == null)
                throw new TransportException(_endpointAddress.Uri,
                    "The create message sender on messaging factory returned null.");
        }
 public ServiceBusListener(MessagingFactory messagingFactory, string entityPath, ServiceBusTriggerExecutor triggerExecutor)
 {
     _messagingFactory = messagingFactory;
     _entityPath = entityPath;
     _triggerExecutor = triggerExecutor;
     _cancellationTokenSource = new CancellationTokenSource();
 }
 public ReliableClientBase(string sbNamespace, TokenProvider tokenProvider, string path, RetryPolicy<ServiceBusTransientErrorDetectionStrategy> policy)
 {
     mRetryPolicy = policy;
     Uri address = ServiceBusEnvironment.CreateServiceUri("sb", sbNamespace, string.Empty);
     mNamespaceManager = new NamespaceManager(address, tokenProvider);
     mMessagingFactory = MessagingFactory.Create(address, tokenProvider);
 }
예제 #13
0
 public RequestMessagePump(MessagingFactory messagingFactory, IRequestBroker requestBroker, Type messageType, ILogger logger)
     : base(logger)
 {
     _messagingFactory = messagingFactory;
     _requestBroker = requestBroker;
     _messageType = messageType;
 }
예제 #14
0
 static void ReceiveAllMessagesFromSubscripions(MessagingFactory messagingFactory)
 {
     // Receive message from 3 subscriptions.
     Program.ReceiveAllMessageFromSubscription(messagingFactory, Conts.SubAllMessages);
     Program.ReceiveAllMessageFromSubscription(messagingFactory, Conts.YoungHorses);
     Program.ReceiveAllMessageFromSubscription(messagingFactory, Conts.OldHorses);
 }
예제 #15
0
 public CommandMessagePump(MessagingFactory messagingFactory, ICommandBroker commandBroker, Type messageType, ILogger logger)
     : base(logger)
 {
     _messagingFactory = messagingFactory;
     _commandBroker = commandBroker;
     _messageType = messageType;
 }
예제 #16
0
 public MulticastEventMessagePump(MessagingFactory messagingFactory, IMulticastEventBroker multicastEventBroker, Type eventType, string subscriptionName, ILogger logger, int batchSize)
     : base(logger, batchSize)
 {
     _messagingFactory = messagingFactory;
     _multicastEventBroker = multicastEventBroker;
     _eventType = eventType;
     _subscriptionName = subscriptionName;
 }
예제 #17
0
 private static void CreateCommandMessagePumps(BusBuilderConfiguration configuration, MessagingFactory messagingFactory, List<IMessagePump> messagePumps)
 {
     foreach (var commandType in configuration.CommandTypes)
     {
         var pump = new CommandMessagePump(messagingFactory, configuration.CommandBroker, commandType, configuration.Logger);
         messagePumps.Add(pump);
     }
 }
예제 #18
0
 public HeaterCommunication()
 {
     var topicNameSend = "businessrulestofieldgateway";
     _topicNameReceive = "fieldgatewaytobusinessrules";
     _namespaceMgr = NamespaceManager.CreateFromConnectionString(CloudConfigurationManager.GetSetting("ServiceBusConnectionString"));
     _factory = MessagingFactory.CreateFromConnectionString(CloudConfigurationManager.GetSetting("ServiceBusConnectionString"));
     _client = _factory.CreateTopicClient(topicNameSend);
 }
 public TaskOrchestrationClient(string connectionString, string orchestrationTopicName)
 {
     this.orchestrationTopicName = orchestrationTopicName;
     this.connectionString = connectionString;
     this.messagingFactory = MessagingFactory.CreateFromConnectionString(this.connectionString);
     this.oxQueueClient = this.messagingFactory.CreateQueueClient(this.orchestrationTopicName);
     this.defaultConverter = new JsonDataConverter();
 }
 public ServiceBusListener(MessagingFactory messagingFactory, string entityPath, ServiceBusTriggerExecutor triggerExecutor, ServiceBusConfiguration config)
 {
     _messagingFactory = messagingFactory;
     _entityPath = entityPath;
     _triggerExecutor = triggerExecutor;
     _cancellationTokenSource = new CancellationTokenSource();
     _messageProcessor = config.MessagingProvider.CreateMessageProcessor(entityPath);
 }
예제 #21
0
 /// <summary>
 /// Constructor
 /// </summary>
 /// <param name="factory">Messaging factory</param>
 /// <param name="path">Path to the event hub</param>
 /// <param name="consumerName">Consumer name</param>
 /// <param name="partitionId">ID for a logical partition of an event hub</param>
 /// <param name="startingOffset">Starting offset at which to start receiving messages</param>
 internal EventHubReceiver(MessagingFactory factory, string path, string consumerName, string partitionId, string startingOffset)
 {
     this.MessagingFactory = factory;
     this.Path = path;
     this.Name = consumerName;
     this.PartitionId = partitionId;
     this.StartingOffset = startingOffset;
 }
예제 #22
0
 public CompetingEventMessagePump(MessagingFactory messagingFactory, ICompetingEventBroker multicastEventBroker, Type eventType, string subscriptionName, ILogger logger)
     : base(logger)
 {
     _messagingFactory = messagingFactory;
     _multicastEventBroker = multicastEventBroker;
     _eventType = eventType;
     _subscriptionName = subscriptionName;
 }
        public ServerQueueAgent(string entityName, string sbCs)
        {
            _entityName = entityName;
            _sbCs = sbCs;

            _msgFactory = MessagingFactory.CreateFromConnectionString(sbCs);
            _nsMgr = NamespaceManager.CreateFromConnectionString(sbCs);
        }
 public ServiceBusQueueListenerFactory(ServiceBusAccount account, string queueName, ITriggeredFunctionExecutor executor, AccessRights accessRights)
 {
     _namespaceManager = account.NamespaceManager;
     _messagingFactory = account.MessagingFactory;
     _queueName = queueName;
     _executor = executor;
     _accessRights = accessRights;
 }
예제 #25
0
 public static void Run(IPEndPoint deviceEndPoint, IPEndPoint controlEndPoint, MessagingFactory messagingFactory)
 {
     Task.WaitAll(new[]
     {
         RunControlEndpoint(controlEndPoint, messagingFactory), 
         RunDeviceEndpoint(deviceEndPoint, messagingFactory)
     });
 }
 public ServiceBusSubscriptionListenerFactory(ServiceBusAccount account, string topicName, string subscriptionName, ITriggeredFunctionExecutor executor, AccessRights accessRights)
 {
     _namespaceManager = account.NamespaceManager;
     _messagingFactory = account.MessagingFactory;
     _topicName = topicName;
     _subscriptionName = subscriptionName;
     _executor = executor;
     _accessRights = accessRights;
 }
        public static string GetNamespaceName(MessagingFactory factory)
        {
            if (factory == null)
            {
                return null;
            }

            return GetNamespaceName(factory.Address);
        }
예제 #28
0
        public EventSender(
            MessagingFactory messagingFactory,
            SimulatorConfiguration config,
            Func<object, byte[]> serializer)
        {
            this._serializer = serializer;

            this._eventHubClient = messagingFactory.CreateEventHubClient(config.EventHubPath);
        }
예제 #29
0
        public MessageSender(
            MessagingFactory messagingFactory,
            SimulatorConfiguration config,
            Func<object, byte[]> serializer,
            ISenderInstrumentationPublisher telemetryPublisher)
        {
            this._serializer = serializer;
            this._instrumentationTelemetryPublisher = telemetryPublisher;

            this._eventHubClient = messagingFactory.CreateEventHubClient(config.EventHubPath);
        }
        public HomeController()
        {
            var baseAddress = RoleEnvironment.GetConfigurationSettingValue("namespaceName");
            var issuerName = RoleEnvironment.GetConfigurationSettingValue("issuerName");
            var issuerKey = RoleEnvironment.GetConfigurationSettingValue("issuerKey");

            Uri namespaceAddress = ServiceBusEnvironment.CreateServiceUri("sb", baseAddress, string.Empty);

            this.namespaceManager = new NamespaceManager(namespaceAddress, TokenProvider.CreateSharedSecretTokenProvider(issuerName, issuerKey));
            this.messagingFactory = MessagingFactory.Create(namespaceAddress, TokenProvider.CreateSharedSecretTokenProvider(issuerName, issuerKey));
        }
예제 #31
0
 protected ServiceBusTopic(NamespaceManager namespaceManager, MessagingFactory messagingFactory) : base(namespaceManager, messagingFactory)
 {
 }
예제 #32
0
파일: Program.cs 프로젝트: Wiks00/D2.Task5
        static void Main(string[] args)
        {
            var messagingFactory =
                MessagingFactory.CreateFromConnectionString(ConfigurationManager.ConnectionStrings["AzureSB"]
                                                            .ConnectionString);

            messagingFactory.RetryPolicy = new RetryExponential(TimeSpan.FromSeconds(1), TimeSpan.FromMinutes(5), 10);

            var batchClient = messagingFactory.CreateQueueClient(ConfigurationManager.AppSettings["InQ"]);

            batchClient.RegisterSessionHandler(typeof(ScanerSessionHandler), new SessionHandlerOptions {
                AutoComplete = false
            });

            var statusClient = messagingFactory.CreateQueueClient(ConfigurationManager.AppSettings["StatusQ"]);
            var configClient = messagingFactory.CreateQueueClient(ConfigurationManager.AppSettings["OutQ"]);

            statusClient.OnMessage(message =>
            {
                if (message.ContentType.Equals("text/plain"))
                {
                    Console.WriteLine($"Service: {message.CorrelationId} now is on '{message.GetBody<string>()}' stage");
                    message.Complete();
                }

                message.DeadLetter();
            }, new OnMessageOptions {
                AutoComplete = false
            });

            var configPath = Path.Combine(Environment.CurrentDirectory,
                                          ConfigurationManager.AppSettings["ConfigFolder"]);

            if (!Directory.Exists(configPath))
            {
                Directory.CreateDirectory(configPath);
                using (var file = File.CreateText(Path.Combine(configPath, ConfigurationManager.AppSettings["ConfigFileName"])))
                {
                    file.Write("{\"timeout\": \"00:00:05\", \"stopWord\": \"ಠ_ಠ\"}");
                }
            }

            var watcher = new FileSystemWatcher(configPath, ConfigurationManager.AppSettings["ConfigFileName"])
            {
                IncludeSubdirectories = false,
                EnableRaisingEvents   = true
            };

            watcher.Changed += (sender, eventArgs) =>
            {
                Task.Delay(TimeSpan.FromSeconds(1)).Wait();
                configClient.Send(new BrokeredMessage(new MemoryStream(File.ReadAllBytes(eventArgs.FullPath)))
                {
                    ContentType = "application/json"
                });
            };

            while (true)
            {
                Task.Delay(TimeSpan.FromMilliseconds(50)).Wait();
            }
        }
예제 #33
0
 public MessageUnit Use(MessagingFactory factory)
 {
     this.factory = factory;
     return(this);
 }
예제 #34
0
        static void Main(string[] args)
        {
            var connBuilder = new ServiceBusConnectionStringBuilder();

            connBuilder.ManagementPort = SBWSHttpPort;
            connBuilder.RuntimePort    = SBWSTcpPort;
            connBuilder.Endpoints.Add(new UriBuilder()
            {
                Scheme = "sb", Host = "ERIMATBUS.redmond.corp.microsoft.com", Path = "ERIMATTEST"
            }.Uri);
            connBuilder.StsEndpoints.Add(new UriBuilder()
            {
                Scheme = "https", Host = "ERIMATBUS.redmond.corp.microsoft.com", Port = SBWSHttpPort, Path = "ERIMATTEST"
            }.Uri);
            //connBuilder.Endpoints.Add(new UriBuilder() { Scheme = "sb", Host = "TK3ECITXSWEB304.parttest.extranettest.microsoft.com", Path = "NGVL-DIT" }.Uri);
            //connBuilder.StsEndpoints.Add(new UriBuilder() { Scheme = "https", Host = "TK3ECITXSWEB304.parttest.extranettest.microsoft.com", Port = SBWSHttpPort, Path = "NGVL-DIT" }.Uri);

            Test(NamespaceManager.CreateFromConnectionString(connBuilder.ToString()), MessagingFactory.CreateFromConnectionString(connBuilder.ToString()));
        }
예제 #35
0
            public async Task Should_be_configured_and_working()
            {
                var settings = new TestAzureServiceBusAccountSettings();
                var provider = new SharedAccessKeyTokenProvider(settings);

                var tokenProvider = provider.GetTokenProvider();

                var namespaceSettings = new NamespaceManagerSettings
                {
                    TokenProvider = tokenProvider
                };

                var serviceUri = AzureServiceBusEndpointUriCreator.Create(
                    Configuration.ServiceNamespace,
                    "MassTransit.Azure.ServiceBus.Core.Tests"
                    );

                var namespaceManager = new NamespaceManager(serviceUri, namespaceSettings);

                await CreateQueue(namespaceManager, serviceUri, "TestClient");

                await CreateHostQueue(tokenProvider);

                var mfs = new MessagingFactorySettings
                {
                    TokenProvider    = tokenProvider,
                    OperationTimeout = TimeSpan.FromSeconds(30),
                    TransportType    = TransportType.Amqp
                };

                var factory = await MessagingFactory.CreateAsync(serviceUri, mfs);

                var receiver = factory.CreateQueueClient("Control");

                receiver.PrefetchCount = 100;

                var       done  = new TaskCompletionSource <bool>();
                var       count = 0;
                const int limit = 1000;

                receiver.RegisterMessageHandler(async(message, cancellationToken) =>
                {
                    await receiver.CompleteAsync(message.SystemProperties.LockToken);

                    var received = Interlocked.Increment(ref count);
                    if (received == limit)
                    {
                        done.TrySetResult(true);
                    }
                }, new MessageHandlerOptions(ExceptionReceivedHandler)
                {
                    AutoComplete         = false,
                    MaxConcurrentCalls   = 100,
                    MaxAutoRenewDuration = TimeSpan.FromSeconds(60)
                });

                var client = factory.CreateMessageSender("Control");

                var stopwatch = Stopwatch.StartNew();
                var tasks     = new Task[limit];

                for (var i = 0; i < limit; i++)
                {
                    tasks[i] = SendAMessage(client);
                }

                await done.Task;

                stopwatch.Stop();

                await Task.WhenAll(tasks);

                await receiver.CloseAsync();

                Console.WriteLine("Performance: {0:F2}/s", limit * 1000 / stopwatch.ElapsedMilliseconds);
            }
        /// <summary>
        /// Creates an EventHubReceiver from the given connection sting and partition key.
        /// The Reliable Dictionaries are used to create a receiver from wherever the service last left off,
        /// or from the current date/time if it's the first time the service is coming up.
        /// </summary>
        /// <param name="connectionString"></param>
        /// <param name="servicePartitionKey"></param>
        /// <param name="epochDictionary"></param>
        /// <param name="offsetDictionary"></param>
        /// <returns></returns>
        private async Task <Tuple <EventHubReceiver, MessagingFactory> > ConnectToIoTHubAsync(
            string connectionString,
            long servicePartitionKey,
            IReliableDictionary <string, long> epochDictionary,
            IReliableDictionary <string, string> offsetDictionary, string processOnlyFutureEvents)
        {
            // EventHubs doesn't support NetMessaging, so ensure the transport type is AMQP.
            ServiceBusConnectionStringBuilder connectionStringBuilder = new ServiceBusConnectionStringBuilder(connectionString);

            connectionStringBuilder.TransportType = TransportType.Amqp;

            ServiceEventSource.Current.ServiceMessage(
                this.Context,
                $"RouterService - {ServiceUniqueId} - ConnectToIoTHubAsync - connecting to IoT Hub at {0}",
                String.Join(",", connectionStringBuilder.Endpoints.Select(x => x.ToString())));

            // A new MessagingFactory is created here so that each partition of this service will have its own MessagingFactory.
            // This gives each partition its own dedicated TCP connection to IoT Hub.
            MessagingFactory           messagingFactory    = MessagingFactory.CreateFromConnectionString(connectionStringBuilder.ToString());
            EventHubClient             eventHubClient      = messagingFactory.CreateEventHubClient("messages/events");
            EventHubRuntimeInformation eventHubRuntimeInfo = await eventHubClient.GetRuntimeInformationAsync();

            EventHubReceiver eventHubReceiver = null;

            // Get an IoT Hub partition ID that corresponds to this partition's low key.
            // This assumes that this service has a partition count 'n' that is equal to the IoT Hub partition count and a partition range of 0..n-1.
            // For example, given an IoT Hub with 32 partitions, this service should be created with:
            // partition count = 32
            // partition range = 0..31
            string eventHubPartitionId = eventHubRuntimeInfo.PartitionIds[servicePartitionKey];

            int retryCount = 1;

            while (retryCount > 0)
            {
                try
                {
                    using (ITransaction tx = this.StateManager.CreateTransaction())
                    {
                        ConditionalValue <string> offsetResult = await offsetDictionary.TryGetValueAsync(tx, "offset", LockMode.Default);

                        ConditionalValue <long> epochResult = await epochDictionary.TryGetValueAsync(tx, "epoch", LockMode.Update);

                        long newEpoch = epochResult.HasValue
                            ? epochResult.Value + 1
                            : 0;

                        if (offsetResult.HasValue)
                        {
                            // continue where the service left off before the last failover or restart.
                            ServiceEventSource.Current.ServiceMessage(
                                this.Context,
                                $"RouterService - {ServiceUniqueId} - ConnectToIoTHubAsync -Creating EventHub listener on partition {eventHubPartitionId} with offset {offsetResult.Value}");

                            eventHubReceiver = await eventHubClient.GetDefaultConsumerGroup().CreateReceiverAsync(eventHubPartitionId, offsetResult.Value, newEpoch);
                        }
                        else
                        {
                            // first time this service is running so there is no offset value yet.
                            // start with the current time.
                            ServiceEventSource.Current.ServiceMessage(
                                this.Context,
                                $"RouterService - {ServiceUniqueId} - ConnectToIoTHubAsync - Creating EventHub listener on partition {eventHubPartitionId} with offset time now{DateTime.UtcNow} - Starting service");

                            if (processOnlyFutureEvents.Equals("yes"))
                            {
                                eventHubReceiver =
                                    await
                                    eventHubClient.GetDefaultConsumerGroup()
                                    .CreateReceiverAsync(eventHubPartitionId, DateTime.UtcNow, newEpoch);
                            }
                            else
                            {
                                eventHubReceiver =
                                    await
                                    eventHubClient.GetDefaultConsumerGroup()
                                    .CreateReceiverAsync(eventHubPartitionId, newEpoch);
                            }
                        }

                        // epoch is recorded each time the service fails over or restarts.
                        await epochDictionary.SetAsync(tx, "epoch", newEpoch);

                        await tx.CommitAsync();

                        retryCount = 0;
                    }
                }
                catch (TimeoutException te)
                {
                    // transient error. Retry.
                    ServiceEventSource.Current.ServiceMessage(this.Context, $"RouterService - {ServiceUniqueId} - ConnectToIoTHubAsync - TimeoutException Retry Count#{retryCount} : Message=[{te.ToString()}]");

                    retryCount++;
                    await Task.Delay(global::Iot.Common.Names.IoTHubRetryWaitIntervalsInMills);
                }
                catch (FabricTransientException fte)
                {
                    // transient error. Retry.
                    ServiceEventSource.Current.ServiceMessage(this.Context, $"RouterService - {ServiceUniqueId} - ConnectToIoTHubAsync - FabricTransientException : Message=[{fte.ToString()}]");

                    retryCount++;
                    await Task.Delay(global::Iot.Common.Names.IoTHubRetryWaitIntervalsInMills);
                }
                catch (FabricNotPrimaryException fnpe)
                {
                    ServiceEventSource.Current.ServiceMessage(this.Context, $"RouterService - {ServiceUniqueId} - ConnectToIoTHubAsync - FabricNotPrimaryException Exception - Message=[{fnpe}]");
                    retryCount = 0;
                }
                catch (Exception ex)
                {
                    ServiceEventSource.Current.ServiceMessage(this.Context, $"RouterService - {ServiceUniqueId} - ConnectToIoTHubAsync - General Exception - Message=[{ex}]");
                    retryCount = 0;
                }
            }

            return(new Tuple <EventHubReceiver, MessagingFactory>(eventHubReceiver, messagingFactory));
        }
예제 #37
0
        async Task ReceiveMessagesAsync(string connectionString, string queueName)
        {
            var receiverFactory = MessagingFactory.CreateFromConnectionString(connectionString);

            var receiver = await receiverFactory.CreateMessageReceiverAsync(queueName, ReceiveMode.PeekLock);

            Console.WriteLine("Receiving message from Queue...");
            while (true)
            {
                try
                {
                    //receive messages from Queue
                    var message = await receiver.ReceiveAsync(TimeSpan.FromSeconds(5));

                    if (message != null)
                    {
                        if (message.Label != null &&
                            message.ContentType != null &&
                            message.Label.Equals("Scientist", StringComparison.InvariantCultureIgnoreCase) &&
                            message.ContentType.Equals("application/json", StringComparison.InvariantCultureIgnoreCase))
                        {
                            var body = message.GetBody <Stream>();

                            dynamic scientist = JsonConvert.DeserializeObject(new StreamReader(body, true).ReadToEnd());

                            lock (Console.Out)
                            {
                                Console.ForegroundColor = ConsoleColor.Cyan;
                                Console.WriteLine(
                                    "\t\t\t\tMessage received: \n\t\t\t\t\t\tMessageId = {0}, \n\t\t\t\t\t\tSequenceNumber = {1}, \n\t\t\t\t\t\tEnqueuedTimeUtc = {2}," +
                                    "\n\t\t\t\t\t\tExpiresAtUtc = {5}, \n\t\t\t\t\t\tContentType = \"{3}\", \n\t\t\t\t\t\tSize = {4},  \n\t\t\t\t\t\tContent: [ firstName = {6}, name = {7} ]",
                                    message.MessageId,
                                    message.SequenceNumber,
                                    message.EnqueuedTimeUtc,
                                    message.ContentType,
                                    message.Size,
                                    message.ExpiresAtUtc,
                                    scientist.firstName,
                                    scientist.name);
                                Console.ResetColor();
                            }
                            await message.CompleteAsync();
                        }
                        else
                        {
                            await message.DeadLetterAsync("ProcessingError", "Don't know what to do with this message");
                        }
                    }
                    else
                    {
                        //no more messages in the queue
                        break;
                    }
                }
                catch (MessagingException e)
                {
                    if (!e.IsTransient)
                    {
                        Console.WriteLine(e.Message);
                        throw;
                    }
                }
            }
            await receiver.CloseAsync();

            await receiverFactory.CloseAsync();
        }
예제 #38
0
 public OutboundQueueSubscriber(MessagingFactory messagingFactory, NamespaceManager namespaceManager, IOutboundMessageHandler messageHandler)
     : base(Guid.Parse("700ef2ac-989c-4c37-a6bc-e70ba8e71b72"), QueueNames.Outbound, messagingFactory, namespaceManager)
 {
     _messageHandler = messageHandler;
 }
        public void ReadMessage()
        {
            MessagingFactory _messagingFactory;
            const string     RequestQueue = "testwithcheck";

            //Console.ForegroundColor = ConsoleColor.Green;
            var connectionString = ConfigurationManager.ConnectionStrings["ServiceBusConnectionString"];

            _messagingFactory = MessagingFactory.CreateFromConnectionString(connectionString.ConnectionString);


            var receiver = _messagingFactory.CreateQueueClient(RequestQueue, ReceiveMode.PeekLock);

            #region variables
            var               messageBodyStream  = new MemoryStream();
            MessageSession    receiverSession    = null;
            var               keepPolling        = true;
            var               isFirstMessage     = true;
            var               expectedNoMessages = 0;
            BrokeredMessage[] messages           = null;
            messageBodyStream = new MemoryStream();
            var    messagesReceived      = 0;
            var    responseQueue         = string.Empty;
            var    sessionId             = string.Empty;
            var    correlationId         = string.Empty;
            Stream fullMessageBodyStream = null;
            #endregion

            while (true)
            {
                Console.WriteLine("Waiting for new message");
                using (var scope = new TransactionScope())
                {
                    receiverSession = receiver.AcceptMessageSession();
                    #region variables
                    messageBodyStream  = new MemoryStream();
                    keepPolling        = true;
                    isFirstMessage     = true;
                    expectedNoMessages = 0;
                    messages           = null;
                    messagesReceived   = 0;
                    responseQueue      = string.Empty;
                    sessionId          = string.Empty;
                    correlationId      = string.Empty;
                    #endregion

                    while (keepPolling)
                    {
                        var message = receiverSession.Receive(TimeSpan.FromSeconds(10));
                        if (message == null)
                        {
                            continue;
                        }

                        if (isFirstMessage)
                        {
                            Console.WriteLine("Receiving first message");
                            expectedNoMessages = (int)message.Properties["TotalMessages"];
                            messages           = new BrokeredMessage[expectedNoMessages];
                            isFirstMessage     = false;
                            responseQueue      = message.ReplyTo;
                            sessionId          = message.SessionId;
                            correlationId      = message.CorrelationId;
                        }

                        var messageNo    = (int)message.Properties["MessageNo"];
                        var messageIndex = messageNo - 1;
                        Console.WriteLine(string.Format("Receiving message {0}", messageNo));
                        messages[messageIndex] = message;
                        messagesReceived++;

                        if (messagesReceived == expectedNoMessages)
                        {
                            keepPolling = false;
                        }
                    }

                    //Rebuild Object
                    fullMessageBodyStream = ChunkedMessageBuilder.ReconstructMessageBody(messages);

                    var completeTasks = new List <Task>();
                    foreach (var message in messages)
                    {
                        completeTasks.Add(message.CompleteAsync());
                    }

                    Task.WaitAll(completeTasks.ToArray());
                }
                var obj = SerializationHelper.Deserialize <string>(fullMessageBodyStream);

                Console.WriteLine(string.Format("Request Message is = {0}", obj));
                Console.WriteLine("");

                //var stringBuilder = new StringBuilder();
                //stringBuilder.Append('I', Convert.ToInt32(fullMessageBodyStream.Length));

                //var responseMessageBody = new Messages.VeryBigDataType();
                //responseMessageBody.BigValue = stringBuilder.ToString();

                //SendMessage(responseMessageBody, responseQueue, sessionId, correlationId);
            }

            Console.ReadLine();
        }
예제 #40
0
        /// <summary>
        /// Notifies this extension component that it has been registered in the owner's collection of extensions.
        /// </summary>
        /// <param name="owner">The extensible owner object that aggregates this extension.</param>
        public void Attach(IExtensibleCloudServiceComponent owner)
        {
            owner.Extensions.Demand <IRoleConfigurationSettingsExtension>();
            IRoleConfigurationSettingsExtension roleConfigExtension = owner.Extensions.Find <IRoleConfigurationSettingsExtension>();

            this.serviceBusEndpoint = roleConfigExtension.GetServiceBusEndpoint(WellKnownEndpointName.InterRoleCommunication);
            this.retryPolicy        = roleConfigExtension.CommunicationRetryPolicy;

            if (this.serviceBusEndpoint != null)
            {
                // Configure Service Bus credentials and entity URI.
                var credentials = TransportClientCredentialBase.CreateSharedSecretCredential(this.serviceBusEndpoint.IssuerName, this.serviceBusEndpoint.IssuerSecret);
                var address     = ServiceBusEnvironment.CreateServiceUri(WellKnownProtocolScheme.ServiceBus, this.serviceBusEndpoint.ServiceNamespace, String.Empty);

                // Configure Service Bus messaging factory and namespace client which is required for subscription management.
                this.messagingFactory = MessagingFactory.Create(address, credentials);
                this.managementClient = new ServiceBusNamespaceClient(address, credentials);

                ConfigureTopicClient();
                ConfigureSubscriptionClient(this.ircSubscription = ConfigureSubscription(String.Concat(SubscriptionNamePrefix, this.senderInstanceID)));

                // Configure event receive action.
                this.receiveAction = (() =>
                {
                    BrokeredMessage msg = null;

                    this.retryPolicy.ExecuteAction(() =>
                    {
                        // Make sure we are not told to stop receiving while we are retrying.
                        if (!cts.IsCancellationRequested)
                        {
                            if (EventReceiver.TryReceive(Settings.EventWaitTimeout, out msg))
                            {
                                try
                                {
                                    // Make sure we are not told to stop receiving while we were waiting for a new message.
                                    if (!cts.IsCancellationRequested)
                                    {
                                        // Extract the event data from brokered message.
                                        InterRoleCommunicationEvent e = msg.GetBody <InterRoleCommunicationEvent>();

                                        // Notify all registered subscribers.
                                        NotifySubscribers(e);

                                        // Mark brokered message as complete.
                                        msg.Complete(this.retryPolicy);
                                    }
                                    else
                                    {
                                        msg.Defer(this.retryPolicy);
                                    }
                                }
                                catch (Exception ex)
                                {
                                    // Abandons a brokered message and unlocks the message.
                                    msg.Abandon(this.retryPolicy);

                                    // Log an error.
                                    TraceManager.ServiceComponent.TraceError(ex);
                                }
                            }
                        }
                    });
                });

                // Configure event receive complete action.
                this.endReceive = ((ar) =>
                {
                    this.receiveAction.EndInvoke(ar);

                    if (!cts.IsCancellationRequested)
                    {
                        this.receiveHandle = this.receiveAction.BeginInvoke(this.endReceive, null);
                    }
                });

                // Configure event send action.
                this.sendAction = ((e) =>
                {
                    this.retryPolicy.ExecuteAction(() => { EventSender.Send(e); });
                });

                // Configure event send complete action.
                this.endSend = ((ar) =>
                {
                    sendAction.EndInvoke(ar);
                });
            }
            else
            {
                throw new CloudApplicationException(String.Format(CultureInfo.CurrentCulture, ExceptionMessages.SpecifiedServiceBusEndpointNotFound, WellKnownEndpointName.InterRoleCommunication, ServiceBusConfigurationSettings.SectionName));
            }
        }
예제 #41
0
 public MessageQueueSubscriptionManager(MessagingFactory messagingFactory)
 {
     _messagingFactory = messagingFactory;
     _messageSenders   = new ConcurrentDictionary <string, MessageSender>();
     _subscribers      = new List <MessageSubscriber>();
 }
예제 #42
0
        public async Task Run(string namespaceAddress, string queueName, string sendToken, string receiveToken)
        {
            Trace.Listeners.Add(new ConsoleTraceListener());

            var sendFactory = MessagingFactory.Create(namespaceAddress, TokenProvider.CreateSharedAccessSignatureTokenProvider(sendToken));

            // Create a durable sender.
            var durableSender = new DurableSender(sendFactory, queueName);

            /*
            ** Send messages.
            */

            // Example 1:
            // Send a message outside a transaction scope. If a transactional MSMQ send queue
            // is used, (Transactional = true) an internal MSMQ transaction is created.
            var nonTxMsg = CreateBrokeredMessage(1);

            Console.WriteLine("Sending message {0} outside of a transaction.", nonTxMsg.Label);
            durableSender.Send(nonTxMsg);

            // Example 2:
            // Send a message inside a transaction scope.
            var txMsg = CreateBrokeredMessage(2);

            Console.WriteLine("Sending message {0} within a transaction.", txMsg.Label);
            using (var scope = new TransactionScope())
            {
                durableSender.Send(txMsg);
                scope.Complete();
            }

            // Example 3:
            // Send two messages inside a transaction scope. If another resource manager is used
            // (e.g., SQL server), the transaction is automatically promoted to a distributed
            // transaction. If a non-transactional MSMQ send queue is used, (TransactionalSend = false),
            // sending the message is not part of the transaction.
            for (var i = 3; i <= 4; i++)
            {
                var dtcMsg = CreateBrokeredMessage(i);
                Console.WriteLine("Sending message {0} within a distributed transaction.", dtcMsg.Label);
                try
                {
                    using (var scope = new TransactionScope())
                    {
                        // SQL Server would be used here, for instance

                        // Send message.
                        durableSender.Send(dtcMsg);
                        scope.Complete();
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Sender: " + ex.Message);
                }
            }

            /*
            ** Receive messages.
            */

            var receiveFactory = MessagingFactory.Create(namespaceAddress, TokenProvider.CreateSharedAccessSignatureTokenProvider(receiveToken));
            var receiver       = receiveFactory.CreateQueueClient(queueName, ReceiveMode.ReceiveAndDelete);

            for (var i = 1; i <= 4; i++)
            {
                try
                {
                    var msg = receiver.Receive();
                    if (msg != null)
                    {
                        PrintBrokeredMessage(msg);
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Receiver: " + ex.Message);
                }
            }

            /*
            ** Cleanup
            */

            Console.WriteLine("\nPress ENTER to exit\n");
            Console.ReadLine();

            durableSender.Dispose();
            receiver.Close();
            sendFactory.Close();
        }
 public AzureServiceBusChannel(string queue)
 {
     _queue       = queue;
     _factory     = MessagingFactory.Create();
     _queueClient = _factory.CreateQueueClient(_queue);
 }
예제 #44
0
파일: Program.cs 프로젝트: stijni/ruckzuck
        static void Main(string[] args)
        {
            MessagingFactory messageFactory;
            NamespaceManager namespaceManager;

            //TopicClient myTopicClient;

            System.Net.ServicePointManager.ServerCertificateValidationCallback = (sender, certificate, chain, sslPolicyErrors) => true;
            System.Net.ServicePointManager.CheckCertificateRevocationList      = false;

            Console.Write("Connecting ServiceBus...");
            string sConnString = Properties.Settings.Default.ConnectionString;

            messageFactory   = MessagingFactory.CreateFromConnectionString(sConnString);
            namespaceManager = NamespaceManager.CreateFromConnectionString(sConnString);

            if (namespaceManager == null)
            {
                Console.WriteLine("\nUnexpected Error");
                return;
            }

            string TopicName = Properties.Settings.Default.TopicName;

            if (!namespaceManager.TopicExists(TopicName))
            {
                namespaceManager.CreateTopic(TopicName);
            }

            Console.WriteLine("... connected.");

            if (!namespaceManager.SubscriptionExists(TopicName, string.Format(Properties.Settings.Default.Filtername, Environment.MachineName)))
            {
                SqlFilter dashboardFilter = new SqlFilter(Properties.Settings.Default.SQLFilter);
                namespaceManager.CreateSubscription(TopicName, string.Format(Properties.Settings.Default.Filtername, Environment.MachineName), dashboardFilter);
            }

            var Client = messageFactory.CreateSubscriptionClient(TopicName, string.Format(Properties.Settings.Default.Filtername, Environment.MachineName), ReceiveMode.PeekLock);

            Client.OnMessage((message) =>
            {
                try
                {
                    if (message.Label.Contains(@"Feedback/failure"))
                    {
                        Console.ForegroundColor = ConsoleColor.Red;
                    }
                    if (message.Label.Contains(@"Feedback/success"))
                    {
                        Console.ForegroundColor = ConsoleColor.Green;
                    }

                    string label = message.Label.Replace("RuckZuck/WCF/", "");
                    //Console.WriteLine(message.EnqueuedTimeUtc.ToLocalTime().ToString("HH:mm") + " " + message.Properties["WorkerServiceHost"].ToString() + "(" + message.Properties["Queue"].ToString() + ") : " + message.Properties["TargetComputer"].ToString()  + " : " + message.GetBody<string>());
                    Console.WriteLine(message.EnqueuedTimeUtc.ToLocalTime().ToString("HH:mm") + " " + message.Label + " " + (message.Properties["ClientIP"] ?? "") as string);


                    Console.ResetColor();
                }
                catch { }
            });

            Console.ReadLine();
        }
 public DeadLetterQueueResendHandler(MessagingFactory messagingFactory, NamespaceManager namespaceManager)
 {
     this.messagingFactory = messagingFactory;
     this.namespaceManager = namespaceManager;
 }
        /// <summary>
        /// Creates an EventHubReceiver from the given connection sting and partition key.
        /// The Reliable Dictionaries are used to create a receiver from wherever the service last left off,
        /// or from the current date/time if it's the first time the service is coming up.
        /// </summary>
        /// <param name="connectionString"></param>
        /// <param name="servicePartitionKey"></param>
        /// <param name="epochDictionary"></param>
        /// <param name="offsetDictionary"></param>
        /// <returns></returns>
        private async Task <Tuple <EventHubReceiver, MessagingFactory> > ConnectToIoTHubAsync(
            string connectionString,
            long servicePartitionKey,
            IReliableDictionary <string, long> epochDictionary,
            IReliableDictionary <string, string> offsetDictionary)
        {
            // EventHubs doesn't support NetMessaging, so ensure the transport type is AMQP.
            ServiceBusConnectionStringBuilder connectionStringBuilder = new ServiceBusConnectionStringBuilder(connectionString);

            connectionStringBuilder.TransportType = TransportType.Amqp;

            ServiceEventSource.Current.ServiceMessage(
                this.Context,
                "RouterService connecting to IoT Hub at {0}",
                String.Join(",", connectionStringBuilder.Endpoints.Select(x => x.ToString())));

            // A new MessagingFactory is created here so that each partition of this service will have its own MessagingFactory.
            // This gives each partition its own dedicated TCP connection to IoT Hub.
            MessagingFactory           messagingFactory    = MessagingFactory.CreateFromConnectionString(connectionStringBuilder.ToString());
            EventHubClient             eventHubClient      = messagingFactory.CreateEventHubClient("messages/events");
            EventHubRuntimeInformation eventHubRuntimeInfo = await eventHubClient.GetRuntimeInformationAsync();

            EventHubReceiver eventHubReceiver;

            // Get an IoT Hub partition ID that corresponds to this partition's low key.
            // This assumes that this service has a partition count 'n' that is equal to the IoT Hub partition count and a partition range of 0..n-1.
            // For example, given an IoT Hub with 32 partitions, this service should be created with:
            // partition count = 32
            // partition range = 0..31
            string eventHubPartitionId = eventHubRuntimeInfo.PartitionIds[servicePartitionKey];

            using (ITransaction tx = this.StateManager.CreateTransaction())
            {
                ConditionalValue <string> offsetResult = await offsetDictionary.TryGetValueAsync(tx, "offset", LockMode.Default);

                ConditionalValue <long> epochResult = await epochDictionary.TryGetValueAsync(tx, "epoch", LockMode.Update);

                long newEpoch = epochResult.HasValue
                    ? epochResult.Value + 1
                    : 0;

                if (offsetResult.HasValue)
                {
                    // continue where the service left off before the last failover or restart.
                    ServiceEventSource.Current.ServiceMessage(
                        this.Context,
                        "Creating EventHub listener on partition {0} with offset {1}",
                        eventHubPartitionId,
                        offsetResult.Value);

                    eventHubReceiver = await eventHubClient.GetDefaultConsumerGroup().CreateReceiverAsync(eventHubPartitionId, offsetResult.Value, newEpoch);
                }
                else
                {
                    // first time this service is running so there is no offset value yet.
                    // start with the current time.
                    ServiceEventSource.Current.ServiceMessage(
                        this.Context,
                        "Creating EventHub listener on partition {0} with offset {1} - Starting service",
                        eventHubPartitionId,
                        DateTime.UtcNow);

                    eventHubReceiver =
                        await
                        eventHubClient.GetDefaultConsumerGroup()
                        .CreateReceiverAsync(eventHubPartitionId, DateTime.UtcNow, newEpoch);
                }

                // epoch is recorded each time the service fails over or restarts.
                await epochDictionary.SetAsync(tx, "epoch", newEpoch);

                await tx.CommitAsync();
            }

            return(new Tuple <EventHubReceiver, MessagingFactory>(eventHubReceiver, messagingFactory));
        }
        /// <summary>
        /// This is the main entry point for your service replica.
        /// This method executes when this replica of your service becomes primary and has write status.
        /// </summary>
        /// <param name="cancellationToken">Canceled when Service Fabric needs to shut down this service replica.</param>
        protected override async Task RunAsync(CancellationToken cancellationToken)
        {
            // Get the IoT Hub connection string from the Settings.xml config file
            // from a configuration package named "Config"
            string iotHubConnectionString =
                this.Context.CodePackageActivationContext
                .GetConfigurationPackageObject("Config")
                .Settings
                .Sections["IoTHubConfigInformation"]
                .Parameters["ConnectionString"]
                .Value;
            string iotHubProcessOnlyFutureEvents =
                this.Context.CodePackageActivationContext
                .GetConfigurationPackageObject("Config")
                .Settings
                .Sections["IoTHubConfigInformation"]
                .Parameters["ProcessOnlyFutureEvents"]
                .Value.ToLower();

            ServiceEventSource.Current.ServiceMessage(this.Context, $"RouterService - {ServiceUniqueId} - RunAsync - Starting service  - Process Only Future Events[{iotHubProcessOnlyFutureEvents}] - IoTHub Connection String[{iotHubConnectionString}]");

            // These Reliable Dictionaries are used to keep track of our position in IoT Hub.
            // If this service fails over, this will allow it to pick up where it left off in the event stream.
            IReliableDictionary <string, string> offsetDictionary =
                await this.StateManager.GetOrAddAsync <IReliableDictionary <string, string> >(OffsetDictionaryName);

            IReliableDictionary <string, long> epochDictionary =
                await this.StateManager.GetOrAddAsync <IReliableDictionary <string, long> >(EpochDictionaryName);

            // Each partition of this service corresponds to a partition in IoT Hub.
            // IoT Hub partitions are numbered 0..n-1, up to n = 32.
            // This service needs to use an identical partitioning scheme.
            // The low key of every partition corresponds to an IoT Hub partition.
            Int64RangePartitionInformation partitionInfo = (Int64RangePartitionInformation)this.Partition.PartitionInfo;
            long servicePartitionKey = partitionInfo.LowKey;

            EventHubReceiver eventHubReceiver = null;
            MessagingFactory messagingFactory = null;

            try
            {
                // HttpClient is designed as a shared object.
                // A single instance should be used throughout the lifetime of RunAsync.
                using (HttpClient httpClient = new HttpClient(new HttpServiceClientHandler()))
                {
                    int  offsetIteration = 0;
                    bool IsConnected     = false;

                    while (true)
                    {
                        cancellationToken.ThrowIfCancellationRequested();

                        if (!IsConnected)
                        {
                            // Get an EventHubReceiver and the MessagingFactory used to create it.
                            // The EventHubReceiver is used to get events from IoT Hub.
                            // The MessagingFactory is just saved for later so it can be closed before RunAsync exits.
                            Tuple <EventHubReceiver, MessagingFactory> iotHubInfo = await this.ConnectToIoTHubAsync(iotHubConnectionString, servicePartitionKey, epochDictionary, offsetDictionary, iotHubProcessOnlyFutureEvents);

                            eventHubReceiver = iotHubInfo.Item1;
                            messagingFactory = iotHubInfo.Item2;

                            IsConnected = true;
                        }

                        Uri postUrl = null;

                        try
                        {
                            // It's important to set a low wait time here in lieu of a cancellation token
                            // so that this doesn't block RunAsync from exiting when Service Fabric needs it to complete.
                            // ReceiveAsync is a long-poll operation, so the timeout should not be too low,
                            // yet not too high to block RunAsync from exiting within a few seconds.
                            using (EventData eventData = await eventHubReceiver.ReceiveAsync(TimeSpan.FromSeconds(5)))
                            {
                                if (eventData == null)
                                {
                                    ServiceEventSource.Current.ServiceMessage(this.Context, $"RouterService - {ServiceUniqueId} - RunAsync - No event data available on hub '{eventHubReceiver.Name}'");
                                    await Task.Delay(global::Iot.Common.Names.IoTHubRetryWaitIntervalsInMills);

                                    continue;
                                }
                                else
                                {
                                    ServiceEventSource.Current.ServiceMessage(this.Context, $"RouterService - {ServiceUniqueId} - RunAsync - Received event data from hub '{eventHubReceiver.Name}' - Enqueued Time[{eventData.EnqueuedTimeUtc}] - Partition '{eventData.PartitionKey}' Sequence # '{eventData.SequenceNumber}'");
                                }

                                string targetSite = (string)eventData.Properties[global::Iot.Common.Names.EventKeyFieldTargetSite];
                                string deviceId   = (string)eventData.Properties[global::Iot.Common.Names.EventKeyFieldDeviceId];

                                // This is the named service instance of the target site data service that the event should be sent to.
                                // The targetSite id is part of the named service instance name.
                                // The incoming device data stream specifie which target site the data belongs to.
                                string prefix                        = global::Iot.Common.Names.InsightApplicationNamePrefix;
                                string serviceName                   = global::Iot.Common.Names.InsightDataServiceName;
                                Uri    targetSiteServiceName         = new Uri($"{prefix}/{targetSite}/{serviceName}");
                                long   targetSiteServicePartitionKey = FnvHash.Hash(deviceId);

                                ServiceEventSource.Current.ServiceMessage(this.Context, $"RouterService - {ServiceUniqueId} - RunAsync - About to post data to Insight Data Service from device '{deviceId}' to target site '{targetSite}' - partitionKey '{targetSiteServicePartitionKey}' - Target Service Name '{targetSiteServiceName}'");

                                // The target site data service exposes an HTTP API.
                                // For incoming device events, the URL is /api/events/{deviceId}
                                // This sets up a URL and sends a POST request with the device JSON payload.
                                postUrl = new HttpServiceUriBuilder()
                                          .SetServiceName(targetSiteServiceName)
                                          .SetPartitionKey(targetSiteServicePartitionKey)
                                          .SetServicePathAndQuery($"/api/events/{deviceId}")
                                          .Build();

                                ServiceEventSource.Current.ServiceMessage(this.Context, $"RouterService - {ServiceUniqueId} - RunAsync - Ready to post data to Insight Data Service from device '{deviceId}' to taget site '{targetSite}' - partitionKey '{targetSiteServicePartitionKey}' - Target Service Name '{targetSiteServiceName}' - url '{postUrl.PathAndQuery}'");

                                // The device stream payload isn't deserialized and buffered in memory here.
                                // Instead, we just can just hook the incoming stream from Iot Hub right into the HTTP request stream.
                                using (Stream eventStream = eventData.GetBodyStream())
                                {
                                    using (StreamContent postContent = new StreamContent(eventStream))
                                    {
                                        postContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");

                                        HttpResponseMessage response = await httpClient.PostAsync(postUrl, postContent, cancellationToken);

                                        if (response.StatusCode == System.Net.HttpStatusCode.BadRequest)
                                        {
                                            // This service expects the receiving target site service to return HTTP 400 if the device message was malformed.
                                            // In this example, the message is simply logged.
                                            // Your application should handle all possible error status codes from the receiving service
                                            // and treat the message as a "poison" message.
                                            // Message processing should be allowed to continue after a poison message is detected.

                                            string responseContent = await response.Content.ReadAsStringAsync();

                                            ServiceEventSource.Current.ServiceMessage(
                                                this.Context,
                                                $"RouterService - {ServiceUniqueId} - RunAsync - Insight service '{targetSiteServiceName}' returned HTTP 400 due to a bad device message from device '{deviceId}'. Error message: '{responseContent}'");
                                        }

                                        ServiceEventSource.Current.ServiceMessage(
                                            this.Context,
                                            $"RouterService - {ServiceUniqueId} - RunAsync - Sent event data to Insight service '{targetSiteServiceName}' with partition key '{targetSiteServicePartitionKey}'. Result: {response.StatusCode.ToString()}");
                                    }
                                }

                                // Save the current Iot Hub data stream offset.
                                // This will allow the service to pick up from its current location if it fails over.
                                // Duplicate device messages may still be sent to the the target site service
                                // if this service fails over after the message is sent but before the offset is saved.
                                if (++offsetIteration % OffsetInterval == 0)
                                {
                                    ServiceEventSource.Current.ServiceMessage(
                                        this.Context,
                                        $"RouterService - {ServiceUniqueId} - RunAsync - Saving offset {eventData.Offset}");

                                    using (ITransaction tx = this.StateManager.CreateTransaction())
                                    {
                                        await offsetDictionary.SetAsync(tx, "offset", eventData.Offset);

                                        await tx.CommitAsync();
                                    }

                                    offsetIteration = 0;
                                }
                            }
                        }
                        catch (Microsoft.ServiceBus.Messaging.ReceiverDisconnectedException rde)
                        {
                            // transient error. Retry.
                            ServiceEventSource.Current.ServiceMessage(this.Context, $"RouterService - {ServiceUniqueId} - RunAsync - Receiver Disconnected Exception in RunAsync: {rde.ToString()}");

                            IsConnected = false;
                        }
                        catch (TimeoutException te)
                        {
                            // transient error. Retry.
                            ServiceEventSource.Current.ServiceMessage(this.Context, $"RouterService - {ServiceUniqueId} - RunAsync - TimeoutException in RunAsync: {te.ToString()}");
                        }
                        catch (FabricTransientException fte)
                        {
                            // transient error. Retry.
                            ServiceEventSource.Current.ServiceMessage(this.Context, $"RouterService - {ServiceUniqueId} - RunAsync - FabricTransientException in RunAsync: {fte.ToString()}");
                        }
                        catch (FabricNotPrimaryException fnpe)
                        {
                            ServiceEventSource.Current.ServiceMessage(this.Context, $"RouterService - {ServiceUniqueId} - RunAsync - FabricNotPrimaryException Exception - Message=[{fnpe}]");

                            // not primary any more, time to quit.
                            return;
                        }
                        catch (Exception ex)
                        {
                            IsConnected = false;
                            string url = postUrl == null ? "Url undefined" : postUrl.ToString();
                            //ServiceEventSource.Current.ServiceMessage(this.Context, $"RouterService - {ServiceUniqueId} - RunAsync - General Exception Url=[{url}]- Message=[{ex}] - Inner Exception=[{ex.InnerException.Message ?? "ex.InnerException is null"}] Call Stack=[{ex.StackTrace ?? "ex.StackTrace is null"}] - Stack trace of inner exception=[{ex.InnerException.StackTrace ?? "ex.InnerException.StackTrace is null"}]");

                            ServiceEventSource.Current.ServiceMessage(this.Context, $"RouterService - {ServiceUniqueId} - RunAsync - General Exception Message[{ex.Message}] for url[{url}]");
                        }
                    }
                }
            }
            finally
            {
                if (messagingFactory != null)
                {
                    await messagingFactory.CloseAsync();
                }
            }
        }
예제 #48
0
 public MessageSenderPool(ILog logger, MessagingFactory factory)
 {
     this.logger  = logger;
     this.factory = factory;
 }
예제 #49
0
 public QueueMessagesBuilder(MessagingFactory messagingFactory)
 {
     _messagingFactory = messagingFactory;
 }
예제 #50
0
        /// <summary>
        /// Using a <see cref="Task"/>, clears all dead letters from the topic and subscription of the
        /// provided <paramref name="topicName"/> and <paramref name="topicSubscriptionName"/>.
        /// </summary>
        /// <param name="topicName">The name of the topic.</param>
        /// <param name="topicSubscriptionName">The name of the subscription.</param>
        /// <returns></returns>
        protected virtual CancellationTokenSource CleanUpDeadLetters(string topicName, string topicSubscriptionName)
        {
            var brokeredMessageRenewCancellationTokenSource  = new CancellationTokenSource();
            IDictionary <string, string> telemetryProperties = new Dictionary <string, string> {
                { "Type", "Azure/Servicebus" }
            };
            int lockIssues = 0;

            Action <BrokeredMessage, IMessage> leaveDeadlLetterInQueue = (deadLetterBrokeredMessage, deadLetterMessage) =>
            {
                // Remove message from queue
                try
                {
                    deadLetterBrokeredMessage.Abandon();
                    lockIssues = 0;
                }
                catch (MessageLockLostException)
                {
                    lockIssues++;
                    Logger.LogWarning(string.Format("The lock supplied for abandon for the skipped dead-letter message '{0}' is invalid.", deadLetterBrokeredMessage.MessageId));
                }
                Logger.LogDebug(string.Format("A dead-letter message of type {0} arrived with the id '{1}' but left in the queue due to settings.", deadLetterMessage.GetType().FullName, deadLetterBrokeredMessage.MessageId));
            };
            Action <BrokeredMessage> removeDeadlLetterFromQueue = deadLetterBrokeredMessage =>
            {
                // Remove message from queue
                try
                {
                    deadLetterBrokeredMessage.Complete();
                    lockIssues = 0;
                }
                catch (MessageLockLostException)
                {
                    lockIssues++;
                    Logger.LogWarning(string.Format("The lock supplied for complete for the skipped dead-letter message '{0}' is invalid.", deadLetterBrokeredMessage.MessageId));
                }
                Logger.LogDebug(string.Format("A dead-letter message arrived with the id '{0}' but was removed as processing was skipped due to settings.", deadLetterBrokeredMessage.MessageId));
            };

            Task.Factory.StartNewSafely(() =>
            {
                int loop = 0;
                while (!brokeredMessageRenewCancellationTokenSource.Token.IsCancellationRequested)
                {
                    lockIssues = 0;
                    MessagingFactory factory = MessagingFactory.CreateFromConnectionString(ConnectionString);
                    string deadLetterPath    = SubscriptionClient.FormatDeadLetterPath(topicName, topicSubscriptionName);
                    MessageReceiver client   = factory.CreateMessageReceiver(deadLetterPath, ReceiveMode.PeekLock);

                    IEnumerable <BrokeredMessage> brokeredMessages = client.ReceiveBatch(1000);

                    foreach (BrokeredMessage brokeredMessage in brokeredMessages)
                    {
                        if (lockIssues > 10)
                        {
                            break;
                        }
                        try
                        {
                            Logger.LogDebug(string.Format("A dead-letter message arrived with the id '{0}'.", brokeredMessage.MessageId));
                            string messageBody = brokeredMessage.GetBody <string>();

                            // Closure protection
                            BrokeredMessage message = brokeredMessage;
                            try
                            {
                                AzureBusHelper.ReceiveEvent
                                (
                                    messageBody,
                                    @event =>
                                {
                                    bool isRequired = BusHelper.IsEventRequired(@event.GetType());
                                    if (!isRequired)
                                    {
                                        removeDeadlLetterFromQueue(message);
                                    }
                                    else
                                    {
                                        leaveDeadlLetterInQueue(message, @event);
                                    }
                                    return(true);
                                },
                                    string.Format("id '{0}'", brokeredMessage.MessageId),
                                    () =>
                                {
                                    removeDeadlLetterFromQueue(message);
                                },
                                    () => { }
                                );
                            }
                            catch
                            {
                                AzureBusHelper.ReceiveCommand
                                (
                                    messageBody,
                                    command =>
                                {
                                    bool isRequired = BusHelper.IsEventRequired(command.GetType());
                                    if (!isRequired)
                                    {
                                        removeDeadlLetterFromQueue(message);
                                    }
                                    else
                                    {
                                        leaveDeadlLetterInQueue(message, command);
                                    }
                                    return(true);
                                },
                                    string.Format("id '{0}'", brokeredMessage.MessageId),
                                    () =>
                                {
                                    removeDeadlLetterFromQueue(message);
                                },
                                    () => { }
                                );
                            }
                        }
                        catch (Exception exception)
                        {
                            TelemetryHelper.TrackException(exception, null, telemetryProperties);
                            // Indicates a problem, unlock message in queue
                            Logger.LogError(string.Format("A dead-letter message arrived with the id '{0}' but failed to be process.", brokeredMessage.MessageId), exception: exception);
                            try
                            {
                                brokeredMessage.Abandon();
                            }
                            catch (MessageLockLostException)
                            {
                                lockIssues++;
                                Logger.LogWarning(string.Format("The lock supplied for abandon for the skipped dead-letter message '{0}' is invalid.", brokeredMessage.MessageId));
                            }
                        }
                    }

                    client.Close();

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

            return(brokeredMessageRenewCancellationTokenSource);
        }
예제 #51
0
        static void Test(NamespaceManager namespaceManager, MessagingFactory messageFactory)
        {
            var q = namespaceManager.GetQueue(QueueName);
            var t = namespaceManager.GetTopic(TopicName);

            var gf = namespaceManager.GetTopics();

            if (!namespaceManager.QueueExists(QueueName))
            {
                namespaceManager.CreateQueue(QueueName);
            }


            QueueClient myQueueClient = messageFactory.CreateQueueClient(QueueName);

            try
            {
                BrokeredMessage sendMessage = new BrokeredMessage("Hello World !");
                myQueueClient.Send(sendMessage);

                //// Receive the message from the queue
                //BrokeredMessage receivedMessage = myQueueClient.Receive(TimeSpan.FromSeconds(5));

                //if (receivedMessage != null)
                //{
                //    Console.WriteLine(string.Format("Message received: {0}", receivedMessage.GetBody<string>()));
                //    receivedMessage.Complete();
                //}

                //Check for messages that are older than they should be
                int      minutesOld = 1;
                DateTime oldest     = DateTime.UtcNow.AddMinutes(-minutesOld);

                int oldMessageCount             = 0;
                List <BrokeredMessage> messages = new List <BrokeredMessage>(myQueueClient.PeekBatch(1000));

                foreach (BrokeredMessage b in messages)
                {
                    if (b.EnqueuedTimeUtc < oldest)
                    {
                        oldMessageCount++;
                    }
                }

                BrokeredMessage bd = myQueueClient.Receive();
                bd.DeadLetter();

                //check for dead letter messages
                QueueClient dlClient = messageFactory.CreateQueueClient(QueueClient.FormatDeadLetterPath(QueueName));

                List <BrokeredMessage> dlMessages = new List <BrokeredMessage>(dlClient.PeekBatch(1000));
                int i = dlMessages.Count;

                //b.DeadLetter();


                if (!namespaceManager.TopicExists(TopicName))
                {
                    namespaceManager.CreateTopic(TopicName);
                }


                if (!namespaceManager.SubscriptionExists(TopicName, SubName))
                {
                    namespaceManager.CreateSubscription(TopicName, SubName);
                }



                TopicClient topicClient = messageFactory.CreateTopicClient(TopicName);

                topicClient.Send(new BrokeredMessage("Message"));

                SubscriptionDescription s = namespaceManager.GetSubscription(TopicName, SubName);

                SubscriptionClient subClient = messageFactory.CreateSubscriptionClient(TopicName, SubName);

                List <BrokeredMessage> subMessages = new List <BrokeredMessage>(subClient.PeekBatch(1000));
                BrokeredMessage        bms         = subClient.Receive();
                bms.DeadLetter();

                foreach (BrokeredMessage b in messages)
                {
                    if (b.EnqueuedTimeUtc < oldest)
                    {
                        oldMessageCount++;
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("Unexpected exception {0}", e.ToString());
                throw;
            }
            finally
            {
                if (messageFactory != null)
                {
                    messageFactory.Close();
                }
            }
            //Send Message
        }
        static void Main(string[] args)
        {
            GetUserCredentials();
            TokenProvider credentials =
                TokenProvider.CreateSharedSecretTokenProvider(IssuerName, IssuerKey);
            Uri serviceUri = ServiceBusEnvironment.CreateServiceUri("sb", ServiceNamespace, string.Empty);

            //*****************************************************************************************************
            //                                   Management Operations
            //*****************************************************************************************************
            NamespaceManager namespaceClient = new NamespaceManager(serviceUri, credentials);

            Console.WriteLine("\nCreating Queue 'IssueTrackingQueue'...");

            // Delete if exists
            if (namespaceClient.QueueExists(queueName))
            {
                namespaceClient.DeleteQueue(queueName);
            }

            namespaceClient.CreateQueue(queueName);

            //*****************************************************************************************************
            //                                   Runtime Operations
            //*****************************************************************************************************
            MessagingFactory factory = MessagingFactory.Create(serviceUri, credentials);

            try
            {
                QueueClient myQueueClient = factory.CreateQueueClient(queueName);

                //*****************************************************************************************************
                //                                   Sending messages to a Queue
                //*****************************************************************************************************
                List <BrokeredMessage> messageList = new List <BrokeredMessage>();
                messageList.Add(CreateIssueMessage("1", "Package lost"));
                messageList.Add(CreateIssueMessage("2", "Package damaged"));
                messageList.Add(CreateIssueMessage("3", "Package defective"));

                Console.WriteLine("\nSending messages to queue...");

                foreach (BrokeredMessage message in messageList)
                {
                    myQueueClient.Send(message);
                    Console.WriteLine(
                        string.Format("Message sent: Id = {0}, Body = {1}", message.MessageId, message.GetBody <string>()));
                }

                Console.WriteLine("\nFinished sending messages, press ENTER to clean up and exit.");
                Console.ReadLine();

                // Closing factory close all entities created from the factory.
                factory.Close();
            }
            catch (Exception)
            {
                factory.Abort();
                throw;
            }

            namespaceClient.DeleteQueue(queueName);
        }
        private async void StartDevices()
        {
            // Create namespace manager
            var namespaceUri     = ServiceBusEnvironment.CreateServiceUri("sb", txtNamespace.Text, string.Empty);
            var tokenProvider    = TokenProvider.CreateSharedAccessSignatureTokenProvider(txtKeyName.Text, txtKeyValue.Text);
            var namespaceManager = new NamespaceManager(namespaceUri, tokenProvider);

            // Check if the event hub already exists, if not, create the event hub.
            if (!await namespaceManager.EventHubExistsAsync(cboEventHub.Text))
            {
                WriteToLog(string.Format(EventHubDoesNotExists, cboEventHub.Text));
                return;
            }
            var eventHubDescription = await namespaceManager.GetEventHubAsync(cboEventHub.Text);

            WriteToLog(string.Format(EventHubCreatedOrRetrieved, cboEventHub.Text));

            // Check if the SAS authorization rule used by devices to send events to the event hub already exists, if not, create the rule.
            var authorizationRule = eventHubDescription.
                                    Authorization.
                                    FirstOrDefault(r => string.Compare(r.KeyName,
                                                                       SenderSharedAccessKey,
                                                                       StringComparison.InvariantCultureIgnoreCase)
                                                   == 0) as SharedAccessAuthorizationRule;

            if (authorizationRule == null)
            {
                authorizationRule = new SharedAccessAuthorizationRule(SenderSharedAccessKey,
                                                                      SharedAccessAuthorizationRule.GenerateRandomKey(),
                                                                      new[]
                {
                    AccessRights.Send
                });
                eventHubDescription.Authorization.Add(authorizationRule);
                await namespaceManager.UpdateEventHubAsync(eventHubDescription);
            }

            cancellationTokenSource = new CancellationTokenSource();
            var serviceBusNamespace = txtNamespace.Text;
            var eventHubName        = cboEventHub.Text;
            var senderKey           = authorizationRule.PrimaryKey;
            var status            = DefaultStatus;
            var eventInterval     = txtEventIntervalInMilliseconds.IntegerValue;
            var minValue          = txtMinValue.IntegerValue;
            var maxValue          = txtMaxValue.IntegerValue;
            var minOffset         = txtMinOffset.IntegerValue;
            var maxOffset         = txtMaxOffset.IntegerValue;
            var spikePercentage   = trackbarSpikePercentage.Value;
            var cancellationToken = cancellationTokenSource.Token;

            // Create one task for each device
            for (var i = 1; i <= txtDeviceCount.IntegerValue; i++)
            {
                var deviceId = i;
#pragma warning disable 4014
#pragma warning disable 4014
                Task.Run(async() =>
#pragma warning restore 4014
                {
                    var deviceName = $"device{deviceId:000}";

                    if (radioButtonAmqp.Checked)
                    {
                        // The token has the following format:
                        // SharedAccessSignature sr={URI}&sig={HMAC_SHA256_SIGNATURE}&se={EXPIRATION_TIME}&skn={KEY_NAME}
                        var token = CreateSasTokenForAmqpSender(SenderSharedAccessKey,
                                                                senderKey,
                                                                serviceBusNamespace,
                                                                eventHubName,
                                                                deviceName,
                                                                TimeSpan.FromDays(1));
                        WriteToLog(string.Format(SasToken, deviceId));

                        var messagingFactory = MessagingFactory.Create(ServiceBusEnvironment.CreateServiceUri("sb", serviceBusNamespace, ""), new MessagingFactorySettings
                        {
                            TokenProvider = TokenProvider.CreateSharedAccessSignatureTokenProvider(token),
                            TransportType = TransportType.Amqp
                        });
                        WriteToLog(string.Format(MessagingFactoryCreated, deviceId));

                        // Each device uses a different publisher endpoint: [EventHub]/publishers/[PublisherName]
                        var eventHubClient = messagingFactory.CreateEventHubClient($"{eventHubName}/publishers/{deviceName}");
                        WriteToLog(string.Format(EventHubClientCreated, deviceId, eventHubClient.Path));

                        while (!cancellationToken.IsCancellationRequested)
                        {
                            // Create random value
                            var value     = GetValue(minValue, maxValue, minOffset, maxOffset, spikePercentage);
                            var timestamp = DateTime.Now;

                            // Create EventData object with the payload serialized in JSON format
                            var payload = new Payload
                            {
                                DeviceId  = deviceId,
                                Name      = deviceName,
                                Status    = status,
                                Value     = value,
                                Timestamp = timestamp
                            };
                            var json = JsonConvert.SerializeObject(payload);
                            using (var eventData = new EventData(Encoding.UTF8.GetBytes(json))
                            {
                                PartitionKey = deviceName
                            })
                            {
                                // Create custom properties
                                eventData.Properties.Add(DeviceId, deviceId);
                                eventData.Properties.Add(DeviceName, deviceName);
                                eventData.Properties.Add(DeviceStatus, status);
                                eventData.Properties.Add(Value, value);
                                eventData.Properties.Add(Timestamp, timestamp);

                                // Send the event to the event hub
                                await eventHubClient.SendAsync(eventData);
                                WriteToLog($"[Event] DeviceId=[{payload.DeviceId:000}] " +
                                           $"Value=[{payload.Value:000}] " +
                                           $"Timestamp=[{payload.Timestamp}]");
                            }

                            // Wait for the event time interval
                            Thread.Sleep(eventInterval);
                        }
                    }
                    else
                    {
                        // The token has the following format:
                        // SharedAccessSignature sr={URI}&sig={HMAC_SHA256_SIGNATURE}&se={EXPIRATION_TIME}&skn={KEY_NAME}
                        var token = CreateSasTokenForHttpsSender(SenderSharedAccessKey,
                                                                 senderKey,
                                                                 serviceBusNamespace,
                                                                 eventHubName,
                                                                 deviceName,
                                                                 TimeSpan.FromDays(1));
                        WriteToLog(string.Format(SasToken, deviceId));

                        // Create HttpClient object used to send events to the event hub.
                        var httpClient = new HttpClient
                        {
                            BaseAddress =
                                new Uri(string.Format(EventHubUrl,
                                                      serviceBusNamespace,
                                                      eventHubName,
                                                      deviceName).ToLower())
                        };
                        httpClient.DefaultRequestHeaders.TryAddWithoutValidation("Authorization", token);
                        httpClient.DefaultRequestHeaders.Add("ContentType",
                                                             "application/json;type=entry;charset=utf-8");
                        WriteToLog(string.Format(HttpClientCreated, deviceId, httpClient.BaseAddress));

                        while (!cancellationToken.IsCancellationRequested)
                        {
                            // Create random value
                            var value     = GetValue(minValue, maxValue, minOffset, maxOffset, spikePercentage);
                            var timestamp = DateTime.Now;

                            // Create EventData object with the payload serialized in JSON format
                            var payload = new Payload
                            {
                                DeviceId  = deviceId,
                                Name      = deviceName,
                                Status    = status,
                                Value     = value,
                                Timestamp = timestamp
                            };
                            var json = JsonConvert.SerializeObject(payload);

                            // Create HttpContent
                            var postContent = new ByteArrayContent(Encoding.UTF8.GetBytes(json));

                            // Create custom properties
                            postContent.Headers.Add(DeviceId, deviceId.ToString(CultureInfo.InvariantCulture));
                            postContent.Headers.Add(DeviceName, deviceName);
                            //postContent.Headers.Add(DeviceStatus, location);
                            postContent.Headers.Add(Value, value.ToString(CultureInfo.InvariantCulture));
                            postContent.Headers.Add(Timestamp, timestamp.ToString(CultureInfo.InvariantCulture));

                            try
                            {
                                var response =
                                    await
                                    httpClient.PostAsync(
                                        httpClient.BaseAddress + "/messages" + "?timeout=60" + ApiVersion,
                                        postContent, cancellationToken);
                                response.EnsureSuccessStatusCode();
                                WriteToLog($"[Event] DeviceId=[{payload.DeviceId:000}] " +
                                           $"Value=[{payload.Value:000}] " +
                                           $"Timestamp=[{payload.Timestamp}]");
                            }
                            catch (HttpRequestException ex)
                            {
                                WriteToLog(string.Format(SendFailed, deviceId, ex.Message));
                            }

                            // Wait for the event time interval
                            Thread.Sleep(eventInterval);
                        }
                    }
                },
                         cancellationToken).ContinueWith(t =>
#pragma warning restore 4014
#pragma warning restore 4014
                {
                    if (t.IsFaulted && t.Exception != null)
                    {
                        HandleException(t.Exception);
                    }
                }, cancellationToken);
            }
        }
 public ServiceBusMessagingFactoryContext(MessagingFactory messagingFactory, CancellationToken cancellationToken)
     : base(new PayloadCache(), cancellationToken)
 {
     _messagingFactory = messagingFactory;
 }
예제 #55
0
 public ServiceBusMessagingFactoryContext(MessagingFactory messagingFactory, CancellationToken cancellationToken)
     : base(cancellationToken)
 {
     _messagingFactory = messagingFactory;
 }
예제 #56
0
        bool Run( )
        {
            CloudWebDeployInputs inputs = null;

            if (!GetInputs(out inputs))
            {
                return(false);
            }

            Console.WriteLine("Retrieving namespace metadata...");
            // Create Namespace
            ServiceBusManagementClient sbMgmt = new ServiceBusManagementClient(inputs.Credentials);

            var    nsDescription      = sbMgmt.Namespaces.GetNamespaceDescription(inputs.SBNamespace);
            string nsConnectionString = nsDescription.NamespaceDescriptions.First(
                (d) => String.Equals(d.AuthorizationType, "SharedAccessAuthorization")
                ).ConnectionString;

            NamespaceManager nsManager = NamespaceManager.CreateFromConnectionString(nsConnectionString);

            EventHubDescription ehDevices = AzureConsoleHelper.SelectEventHub(nsManager, inputs.Credentials);

            var serviceNamespace = inputs.SBNamespace;
            var hubName          = ehDevices.Path;

            var sharedAccessAuthorizationRule = ehDevices.Authorization.FirstOrDefault((d)
                                                                                       => d.Rights.Contains(AccessRights.Listen)) as SharedAccessAuthorizationRule;

            if (sharedAccessAuthorizationRule == null)
            {
                Console.WriteLine("Cannot locate Authorization rule for WebSite key.");
                return(false);
            }

            var receiverKeyName = sharedAccessAuthorizationRule.KeyName;
            var receiverKey     = sharedAccessAuthorizationRule.PrimaryKey;
            //Console.WriteLine("Starting temperature processor with {0} partitions.", partitionCount);

            CancellationTokenSource cts = new CancellationTokenSource( );

            int            closedReceivers  = 0;
            AutoResetEvent receiversStopped = new AutoResetEvent(false);

            MessagingFactory factory = MessagingFactory.Create(ServiceBusEnvironment.CreateServiceUri("sb", serviceNamespace, ""),
                                                               new MessagingFactorySettings
            {
                TokenProvider = TokenProvider.CreateSharedAccessSignatureTokenProvider(receiverKeyName, receiverKey),
                TransportType = TransportType.Amqp
            });

            EventHubClient        eventHubClient        = factory.CreateEventHubClient(hubName);
            EventHubConsumerGroup eventHubConsumerGroup = eventHubClient.GetDefaultConsumerGroup( );

            int partitionCount = ehDevices.PartitionCount;

            for (int i = 0; i < partitionCount; i++)
            {
                Task.Factory.StartNew((state) =>
                {
                    try
                    {
                        _ConsoleBuffer.Add(string.Format("Starting worker to process partition: {0}", state));

                        var receiver = eventHubConsumerGroup.CreateReceiver(state.ToString( ), DateTime.UtcNow);

                        _ConsoleBuffer.Add(string.Format("Waiting for start receiving messages: {0} ...", state));

                        while (true)
                        {
                            // Receive could fail, I would need a retry policy etc...
                            var messages = receiver.Receive(10);
                            foreach (var message in messages)
                            {
                                //var eventBody = Newtonsoft.Json.JsonConvert.DeserializeObject<TemperatureEvent>(Encoding.Default.GetString(message.GetBytes()));
                                //Console.WriteLine("{0} [{1}] Temperature: {2}", DateTime.Now, message.PartitionKey, eventBody.Temperature);
                                _ConsoleBuffer.Add(message.PartitionKey + " sent message:" + Encoding.Default.GetString(message.GetBytes( )));
                            }

                            if (cts.IsCancellationRequested)
                            {
                                Console.WriteLine("Stopping: {0}", state);
                                receiver.Close( );
                                if (Interlocked.Increment(ref closedReceivers) >= partitionCount)
                                {
                                    receiversStopped.Set();
                                }
                                break;
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        _ConsoleBuffer.Add(ex.Message);
                    }
                }, i);
            }

            Console.ReadLine( );
            cts.Cancel( );

            //waiting for all receivers to stop
            receiversStopped.WaitOne( );

            bool saveToFile;

            for ( ;;)
            {
                Console.WriteLine("Do you want to save received data to file? (y/n)");

                string answer  = Console.ReadLine( );
                string request = "do not";

                saveToFile = false;
                if (!string.IsNullOrEmpty(answer) && answer.ToLower( ).StartsWith("y"))
                {
                    saveToFile = true;
                    request    = "";
                }
                if (ConsoleHelper.Confirm("Are you sure you " + request + " want to save received data?"))
                {
                    break;
                }
            }
            if (saveToFile)
            {
                string fileName     = inputs.SBNamespace + DateTime.UtcNow.ToString("_d_MMM_h_mm") + ".log";
                string filePath     = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
                string fileFullName = filePath + @"\" + fileName;
                if (_ConsoleBuffer.FlushToFile(fileFullName))
                {
                    Console.WriteLine("Output was saved to your desktop, at " + fileFullName + " file.");
                }
            }


            Console.WriteLine("Wait for all receivers to close and then press ENTER.");
            Console.ReadLine( );

            return(true);
        }
예제 #57
0
 public RequeueAndRemove(MessagingFactory messagingFactory, NamespaceManager namespaceManager)
 {
     _messagingFactory = messagingFactory;
     _namespaceManager = namespaceManager;
 }
 public OutgoingPipelineFactory(MessagingFactory factory, IMessageRouter router, IMessageSerializer serializer)
 {
     this.router     = router;
     this.factory    = factory;
     this.serializer = serializer;
 }
예제 #59
0
 public SolutionAvailableTopic(NamespaceManager namespaceManager, MessagingFactory messagingFactory) : base(namespaceManager, messagingFactory)
 {
 }
예제 #60
0
        async Task RunEnd2EndSerializerTests(TransportType transportType, string sbConnectionString)
        {
            string queueName = TestConstants.NonPartitionedQueueName;

            // Create a full framework MessageSender
            MessagingFactory messagingFactory          = MessagingFactory.CreateFromConnectionString(sbConnectionString);
            MessageSender    fullFrameWorkClientSender = messagingFactory.CreateMessageSender(queueName);

            // Create a .NetStandard MessageReceiver
            Core.MessageReceiver dotNetStandardMessageReceiver = new Core.MessageReceiver(TestUtility.NamespaceConnectionString, queueName, ServiceBus.ReceiveMode.ReceiveAndDelete);

            try
            {
                // Send Plain string
                string message1Body = "contosoString";
                var    message1     = new BrokeredMessage(message1Body);
                await fullFrameWorkClientSender.SendAsync(message1);

                // Receive Plain string
                var returnedMessage = await dotNetStandardMessageReceiver.ReceiveAsync();

                TestUtility.Log($"Message1 SequenceNumber: {returnedMessage.SystemProperties.SequenceNumber}");
                var returnedBody1 = returnedMessage.GetBody <string>();
                TestUtility.Log($"Message1: {returnedBody1}");
                Assert.True(string.Equals(message1Body, returnedBody1));

                // Send Custom object
                var book     = new TestBook("contosoBook", 1, 5);
                var message2 = new BrokeredMessage(book);
                await fullFrameWorkClientSender.SendAsync(message2);

                // Receive Custom object
                returnedMessage = await dotNetStandardMessageReceiver.ReceiveAsync();

                TestUtility.Log($"Message2 SequenceNumber: {returnedMessage.SystemProperties.SequenceNumber}");
                var returnedBody2 = returnedMessage.GetBody <TestBook>();
                TestUtility.Log($"Message2: {returnedBody2}");
                Assert.Equal(book, returnedBody2);

                // Send UTF8 encoded byte array object
                string message3Body = "contosoBytes";
                var    message3     = new BrokeredMessage(Encoding.UTF8.GetBytes(message3Body));
                await fullFrameWorkClientSender.SendAsync(message3);

                // Receive UTF8 encoded byte array object
                returnedMessage = await dotNetStandardMessageReceiver.ReceiveAsync();

                TestUtility.Log($"Message3 SequenceNumber: {returnedMessage.SystemProperties.SequenceNumber}");
                var returnedBody3 = Encoding.UTF8.GetString(returnedMessage.GetBody <byte[]>());
                TestUtility.Log($"Message1: {returnedBody3}");
                Assert.True(string.Equals(message3Body, returnedBody3));

                // Send Stream Object
                string message4Body = "contosoStreamObject";
                var    message4     = new BrokeredMessage(new MemoryStream(Encoding.UTF8.GetBytes(message4Body)));
                await fullFrameWorkClientSender.SendAsync(message4);

                // Receive Stream Object
                returnedMessage = await dotNetStandardMessageReceiver.ReceiveAsync();

                TestUtility.Log($"Message3 SequenceNumber: {returnedMessage.SystemProperties.SequenceNumber}");
                var returnedBody4 = Encoding.UTF8.GetString(returnedMessage.Body);
                TestUtility.Log($"Message4: {returnedBody4}");
                Assert.True(string.Equals(message4Body, returnedBody4));
            }
            finally
            {
                await dotNetStandardMessageReceiver.CloseAsync();

                await fullFrameWorkClientSender.CloseAsync();
            }
        }