Example #1
0
        public override bool OnStart()
        {
            ServicePointManager.DefaultConnectionLimit = 12;

            var connectionString = CloudConfigurationManager.GetSetting("topicConnectionString");
            var topicName = CloudConfigurationManager.GetSetting("topicName");

            _nsMgr = NamespaceManager.CreateFromConnectionString(connectionString);

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

            if (!_nsMgr.SubscriptionExists(topicName, "audit"))
            {
                _nsMgr.CreateSubscription(topicName, "audit");
            }

            _client = SubscriptionClient.CreateFromConnectionString(connectionString, topicName, "audit", ReceiveMode.ReceiveAndDelete);

            var result = base.OnStart();

            Trace.TraceInformation("NTM.Auditing has been started");

            return result;
        }
 public static void Initialize()
 {
     if (null == OrdersTopicClient)
     {
         try
         {
             string connectionString = CloudConfigurationManager.GetSetting("Microsoft.ServiceBusTopics.ConnectionString");
             // string connectionString = RoleEnvironment.GetConfigurationSettingValue("Microsoft.ServiceBusTopics.ConnectionString");
             var namespaceManager = NamespaceManager.CreateFromConnectionString(connectionString);
             if (!namespaceManager.TopicExists("OrderTopic"))
             {
                 namespaceManager.CreateTopic("OrderTopic");
             }
             if (!namespaceManager.SubscriptionExists("OrderTopic", "OrderMessages"))
             {
                 namespaceManager.CreateSubscription("OrderTopic", "OrderMessages");
             }
             // Initialize the connection to Service Bus Topics
             OrdersTopicClient = TopicClient.CreateFromConnectionString(connectionString, "OrderTopic");
             SubClient = SubscriptionClient.CreateFromConnectionString(connectionString, "OrderTopic", "OrderMessages");
            
         }
         catch (Exception ex)
         {
             string str = ex.Message;
         }
     }
 }
        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);
            }
        }
Example #4
0
        public void InitialiseTopic()
        {
            ServicePointManager.DefaultConnectionLimit = 12;

            string connectionString = CloudConfigurationManager.GetSetting("ServiceBus.TopicConnectionString");
            _topicClient = SubscriptionClient.CreateFromConnectionString(connectionString, "recreateindex", "WebRoles");

            Task.Run(() =>
            {
                _topicClient.OnMessage(async receivedMessage =>
                {
                    var sequenceNumber = receivedMessage.SequenceNumber;
                    try
                    {
                        await _throttling.Execute(async () => ReCreateSearcher());
                    }
                    catch (Exception ex)
                    {
                        //no idea why it does not work but well, log it
                        Trace.TraceWarning("Exception occurred during the read of message '" + sequenceNumber + "': " + ex.Message);
                    }
                }, new OnMessageOptions {
                    AutoComplete = true
                });

                _completedEvent.WaitOne();
            });
        }
 public ReliableSubscriptionClient(string sbNamespace, TokenProvider tokenProvider, string path, string name, RetryPolicy<ServiceBusTransientErrorDetectionStrategy> policy)
     : base(sbNamespace, tokenProvider, path, policy)
 {
     //create the queue if it doesn't exist
     bool needsCreation = false;
     try
     {
         needsCreation = !mRetryPolicy.ExecuteAction<bool>(() => mNamespaceManager.SubscriptionExists(path,name));
     }
     catch (MessagingEntityNotFoundException)
     {
         needsCreation = true;
     }
     if (needsCreation)
     {
         try
         {
             mRetryPolicy.ExecuteAction<SubscriptionDescription>(() => mNamespaceManager.CreateSubscription(path,name));
         }
         catch (MessagingEntityAlreadyExistsException)
         {
             //ignore this exception because queue already exists
         }
     }
     mRetryPolicy.ExecuteAction(() => mSubscriptionClient = mMessagingFactory.CreateSubscriptionClient(path,name));
 }
Example #6
0
        private void InitServiceBusConnection()
        {
            string connectionString = CloudConfigurationManager.GetSetting("Microsoft.ServiceBus.ConnectionString");

            // Initialize clients
            _client = SubscriptionClient.CreateFromConnectionString(connectionString, "channel_sava", "twitter_follower_email");
        }
        private async Task<SubscriptionClient> GetSubscriptionClient()
        {
            if (_subscriptionClient != null) return _subscriptionClient;

            _subscriptionClient = await _queueManager.CreateSubscriptionReceiver(_topicPath, _subscriptionName);
            _subscriptionClient.PrefetchCount = _prefetchCountSetting;
            return _subscriptionClient;
        }
        private async Task<SubscriptionClient> GetSubscriptionClient()
        {
            if (_subscriptionClient != null) return _subscriptionClient;

            _subscriptionClient = await _queueManager.CreateSubscriptionReceiver(_topicPath, _subscriptionName);
            _subscriptionClient.PrefetchCount = ConcurrentHandlerLimit;
            return _subscriptionClient;
        }
		public SubscriptionObserver Create(IServiceBus bus, SubscriptionRouter router)
		{
			string path = bus.ControlBus.Endpoint.Address.Uri.AbsolutePath;


			var client = new SubscriptionClient(bus, router, _subscriptionServiceUri, _timeout);

			return client;
		}
        public void CanUnregisterProviderIfRegistered()
        {
            var mockHandler = CreateMockHandler(() => CreateResponseMessageWithStatus(HttpStatusCode.OK));

            ISubscriptionClient client = new SubscriptionClient(subscriptionData, mockHandler);
            bool worked = client.UnregisterResourceType("someResource");

            Assert.IsTrue(worked);
        }
        public async Task Subscribe(Subscriber subscriber, MessageType messageType, ContextBag context)
        {
            //When the subscriber is running V6 and UseLegacyMessageDrivenSubscriptionMode is enabled at the subscriber the 'subcriber.Endpoint' value is null
            var endpoint = subscriber.Endpoint ?? subscriber.TransportAddress.Split('@').First();
            var subscriptionClient = new SubscriptionClient { TransportAddress = subscriber.TransportAddress, Endpoint = endpoint };

            var attempts = 0;

            //note: since we have a design that can run into concurrency exceptions we perform a few retries
            // we should redesign this in the future to use a separate doc per subscriber and message type
            do
            {
                try
                {
                    using (var session = OpenAsyncSession())
                    {
                        var subscriptionDocId = Subscription.FormatId(messageType);

                        var subscription = await session.LoadAsync<Subscription>(subscriptionDocId).ConfigureAwait(false);

                        if (subscription == null)
                        {
                            subscription = new Subscription
                            {
                                Id = subscriptionDocId,
                                MessageType = messageType,
                                Subscribers = new List<SubscriptionClient>()
                            };

                            await session.StoreAsync(subscription).ConfigureAwait(false);
                        }

                        if (!subscription.Subscribers.Contains(subscriptionClient))
                        {
                            subscription.Subscribers.Add(subscriptionClient);
                        }
                        else
                        {
                            var savedSubscription = subscription.Subscribers.Single(s => s.Equals(subscriptionClient));
                            if (savedSubscription.Endpoint != subscriber.Endpoint)
                            {
                                savedSubscription.Endpoint = subscriber.Endpoint;
                            }
                        }

                        await session.SaveChangesAsync().ConfigureAwait(false);
                    }

                    return;
                }
                catch (ConcurrencyException)
                {
                    attempts++;
                }
            } while (attempts < 5);
        }
        private void DiscardSubscriptionClient()
        {
            var subscriptionClient = _subscriptionClient;
            _subscriptionClient = null;

            if (subscriptionClient == null) return;
            if (subscriptionClient.IsClosed) return;

            subscriptionClient.Close();
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="address"></param>
        /// <param name="tryProcessMessage"></param>
        public void Start(Address address, Action<BrokeredMessage> tryProcessMessage)
        {
            _cancelRequested = false;

            _tryProcessMessage = tryProcessMessage;

            _subscriptionClient = SubscriptionClientCreator.Create(address, EventType);

            if (_subscriptionClient != null) _subscriptionClient.BeginReceiveBatch(BatchSize, TimeSpan.FromSeconds(ServerWaitTime), OnMessage, null);
        }
        public void Stop()
        {
            lock (_mutex)
            {
                var subscriptionClient = _subscriptionClient;
                if (subscriptionClient == null) return;

                subscriptionClient.Close();
                _subscriptionClient = null;
            }
        }
        public AzureServiceBusEventDispatcherReceiver(string connectionString, IEventDispatcher innerEventDispatcher, IEventStore eventStore, string topicName, string subscriptionName)
        {
            _innerEventDispatcher = innerEventDispatcher;
            _eventStore = eventStore;

            AzureHelpers.EnsureTopicExists(connectionString, topicName);
            AzureHelpers.EnsureSubscriptionExists(connectionString, topicName, subscriptionName);

            _subscriptionClient = SubscriptionClient.CreateFromConnectionString(connectionString, topicName, subscriptionName);

            _workerThread = new Thread(DoWork);
        }
        public void CanGetListOfRegisteredProviders()
        {
            string[] knownResourceTypes = {"website", "mobileservice"};

            var mockHandler = CreateMockHandler(() => CreateListResourcesResponseMessage(
                new ProviderResource {Type = "Website", State = "Unregistered"},
                new ProviderResource {Type = "Mobileservice", State = "Registered"}
                ));

            ISubscriptionClient client = new SubscriptionClient(subscriptionData, mockHandler);
            IEnumerable<ProviderResource> actualResourceTypes = client.ListResources(knownResourceTypes);

            CollectionAssert.AreEquivalent(knownResourceTypes, actualResourceTypes.Select(rt => rt.Type.ToLower()).ToList());
        }
        public given_a_topic_sender()
        {
            this.sut = new TestableTopicSender(this.Settings, this.Topic, new Incremental(1, TimeSpan.Zero, TimeSpan.Zero));

            var tokenProvider = TokenProvider.CreateSharedSecretTokenProvider(this.Settings.TokenIssuer, this.Settings.TokenAccessKey);
            var serviceUri = ServiceBusEnvironment.CreateServiceUri(this.Settings.ServiceUriScheme, this.Settings.ServiceNamespace, this.Settings.ServicePath);

            var manager = new NamespaceManager(serviceUri, tokenProvider);
            manager.CreateSubscription(this.Topic, "Test");

            var messagingFactory = MessagingFactory.Create(serviceUri, tokenProvider);
            this.subscriptionClient = messagingFactory.CreateSubscriptionClient(this.Topic, "Test");

        }
        //private SubscriptionClient _subscriptionClient = null;
        //private string _entityPath = null;
        //public QueueCacheDependency(string key, string guid, TokenProvider tokenProvider, Uri uri, string queueName)
        //{
        //    _key = key;
        //    _guid = guid;
        //    //_tokenProvider = tokenProvider;
        //    //_uri = uri;
        //    //_entityPath = entityPath;
        //    var factory = MessagingFactory.Create(uri, tokenProvider);
        //    _receiver = factory.CreateMessageReceiver(queueName);
        //    WaitCallback callback = new WaitCallback(WaitForMessage);
        //    ThreadPool.QueueUserWorkItem(callback);
        //}
        public QueueCacheDependency(string key, SubscriptionClient subscriptionClient, string crc)
        {
            Trace.TraceInformation("QueueCacheDependency created: {0}", key);
            //_subscriptionClient = subscriptionClient;
            //_guid = guid;
            _key = key;
            _crc = crc;

            subscriptionClient.BeginReceive(
                TimeSpan.MaxValue,
                ReceiveDone,
                subscriptionClient
            );
        }
        public EventTopicSubscriber(string connectionString, string topicName, string subscriptionName, 
            OnMessageOptions options, IEventDispatcher eventDispatcher, ILogger logger)
        {
            var namespaceManager = NamespaceManager.CreateFromConnectionString(connectionString);

            if (!namespaceManager.SubscriptionExists(topicName, subscriptionName))
                namespaceManager.CreateSubscription(topicName, subscriptionName);

            _client = SubscriptionClient.CreateFromConnectionString(connectionString, topicName, subscriptionName);
            _options = options;
            _eventDispatcher = eventDispatcher;
            _logger = logger;
            _stopEvent = new ManualResetEvent(false);
        }
Example #20
0
        public ServiceBusMessageBus(string connectionString, string topicName, ISerializer serializer = null) {
            _topicName = topicName;
            _serializer = serializer ?? new JsonNetSerializer();
            _subscriptionName = "MessageBus";
            _namespaceManager = NamespaceManager.CreateFromConnectionString(connectionString);
            if (!_namespaceManager.TopicExists(_topicName))
                _namespaceManager.CreateTopic(_topicName);

            _topicClient = TopicClient.CreateFromConnectionString(connectionString, _topicName);
            if (!_namespaceManager.SubscriptionExists(_topicName, _subscriptionName))
                _namespaceManager.CreateSubscription(_topicName, _subscriptionName);

            _subscriptionClient = SubscriptionClient.CreateFromConnectionString(connectionString, _topicName, _subscriptionName, ReceiveMode.ReceiveAndDelete);
            _subscriptionClient.OnMessageAsync(OnMessageAsync, new OnMessageOptions { AutoComplete = true });
        }
        public void Start(Func<BrokeredMessage, Task> callback)
        {
            lock (_mutex)
            {
                if (_subscriptionClient != null) throw new InvalidOperationException("Already started!");
                _subscriptionClient = _queueManager.CreateSubscriptionReceiver(_topicPath, _subscriptionName);

                _subscriptionClient.OnMessageAsync(callback,
                                                   new OnMessageOptions
                                                   {
                                                       AutoComplete = false,
                                                       MaxConcurrentCalls = _concurrentHandlerLimit,
                                                   });
            }
        }
        public AzureServiceBusMessageBus(string connectionString, string topicName, ISerializer serializer = null, ILoggerFactory loggerFactory = null) : base(loggerFactory) {
            _serializer = serializer ?? new JsonNetSerializer();

            var namespaceManager = NamespaceManager.CreateFromConnectionString(connectionString);
            if (!namespaceManager.TopicExists(topicName))
                namespaceManager.CreateTopic(topicName);

            _topicClient = TopicClient.CreateFromConnectionString(connectionString, topicName);

            const string subscriptionName = "MessageBus";
            if (!namespaceManager.SubscriptionExists(topicName, subscriptionName))
                namespaceManager.CreateSubscription(topicName, subscriptionName);

            _subscriptionClient = SubscriptionClient.CreateFromConnectionString(connectionString, topicName, subscriptionName, ReceiveMode.ReceiveAndDelete);
            _subscriptionClient.OnMessageAsync(OnMessageAsync, new OnMessageOptions { AutoComplete = true });
        }
 static void ReceiveAllMessagesFromSubscription(SubscriptionClient myTopicClient)
 {
     while (true)
     {
         var message = myTopicClient.Receive(TimeSpan.FromSeconds(5));
         if (message != null)
         {
             Console.WriteLine("Message received. Id: {0} Body: {1}", message.MessageId, message.GetBody<string>());
         }
         else
         {
             Console.WriteLine("No more messages");
             return;
         }
     }
 }
        /// <summary>
        /// Initializes a new instance of the <see cref="SubscriptionReceiver"/> class, 
        /// automatically creating the topic and subscription if they don't exist.
        /// </summary>
        protected SubscriptionReceiver(ServiceBusSettings settings, string topic, string subscription, bool processInParallel, RetryStrategy backgroundRetryStrategy)
        {
            this.settings = settings;
            this.topic = topic;
            this.subscription = subscription;
            this.processInParallel = processInParallel;

            this.tokenProvider = TokenProvider.CreateSharedSecretTokenProvider(settings.TokenIssuer, settings.TokenAccessKey);
            this.serviceUri = ServiceBusEnvironment.CreateServiceUri(settings.ServiceUriScheme, settings.ServiceNamespace, settings.ServicePath);

            var messagingFactory = MessagingFactory.Create(this.serviceUri, tokenProvider);
            this.client = messagingFactory.CreateSubscriptionClient(topic, subscription);
            if (this.processInParallel)
            {
                this.client.PrefetchCount = 18;
            }
            else
            {
                this.client.PrefetchCount = 14;
            }

            this.dynamicThrottling =
                new DynamicThrottling(
                    maxDegreeOfParallelism: 100,
                    minDegreeOfParallelism: 50,
                    penaltyAmount: 3,
                    workFailedPenaltyAmount: 5,
                    workCompletedParallelismGain: 1,
                    intervalForRestoringDegreeOfParallelism: 8000);
            this.receiveRetryPolicy = new RetryPolicy<ServiceBusTransientErrorDetectionStrategy>(backgroundRetryStrategy);
            this.receiveRetryPolicy.Retrying += (s, e) =>
            {
                this.dynamicThrottling.Penalize();
                Trace.TraceWarning(
                    "An error occurred in attempt number {1} to receive a message from subscription {2}: {0}",
                    e.LastException.Message,
                    e.CurrentRetryCount,
                    this.subscription);
            };

            var namespaceManager = new NamespaceManager(this.serviceUri, this.tokenProvider);

            if (!namespaceManager.SubscriptionExists(topic, subscription))
            {
                namespaceManager.CreateSubscription(topic, subscription);
            }
        }
		/// <summary>
		/// This method causes the communication listener to be opened. Once the Open
		///             completes, the communication listener becomes usable - accepts and sends messages.
		/// </summary>
		/// <param name="cancellationToken">Cancellation token</param>
		/// <returns>
		/// A <see cref="T:System.Threading.Tasks.Task">Task</see> that represents outstanding operation. The result of the Task is
		///             the endpoint string.
		/// </returns>
		public override Task<string> OpenAsync(CancellationToken cancellationToken)
		{
			//use receive url:
			_serviceBusClient = SubscriptionClient.CreateFromConnectionString(ServiceBusReceiveConnectionString, ServiceBusTopicName,
				ServiceBusSubscriptionName, ServiceBusReceiveMode);
			if (ServiceBusMessagePrefetchCount > 0)
			{
				_serviceBusClient.PrefetchCount = ServiceBusMessagePrefetchCount;
			}

			ListenForMessages();
			Thread.Yield();

			//create send url:
			string uri = ServiceBusSendConnectionString;
			return Task.FromResult(uri);
		}
        protected override async Task StartAsync()
        {
            InfoLogging(string.Format("{0} - Processing", SubscriptionName));

            _subClient = await _clientFactory.CreateSubscriptionClientAsync(TopicName, SubscriptionName).ConfigureAwait(false);
            
            _retryStrategy = CreateRetryPolicy(MessageRepostMaxCount);
            var stopWatch = new Stopwatch();

            while (!Token.IsCancellationRequested)
            {
                var message = await _subClient.ReceiveAsync(new TimeSpan(0, 0, 10)).ConfigureAwait(false);
                if (message == null) continue;

                var messageBody = message.GetBody<string>();
                if (String.IsNullOrEmpty(messageBody)) continue;

                var messageId = message.MessageId;
                message.Complete();

                DebugLogging(string.Format("{0} - Received new message", SubscriptionName), messageId);
                var failed = false;
                stopWatch.Restart();

                try
                {
                    await _retryStrategy.ExecuteAsync(() =>
                        Do(messageBody), Token).ConfigureAwait(false);

                    stopWatch.Stop();
                    var timeSpan = stopWatch.Elapsed;
                    DebugLogging(string.Format("{0} - Processed message",
                        SubscriptionName), messageId,
                        timeSpan.TotalSeconds);
                }
                catch (Exception exception)
                {
                    ErrorLogging("Message processing failed after retry, posting as failed message.", messageId,
                        exception);
                    failed = true;
                }

                if (failed)
                    await HandleFailedMessageAsync(messageBody).ConfigureAwait(false);
            }
        }
Example #27
0
        public override void Run()
        {
            string connectionString = CloudConfigurationManager.GetSetting("Microsoft.ServiceBus.ConnectionString");
            Client = SubscriptionClient.CreateFromConnectionString(connectionString, TopicName, "OrderProcessingSubscription");
            BrokeredMessage message = null;

            while (!IsStopped)
            {
                try
                {
                    // Receive the message
                    BrokeredMessage receivedMessage = null;
                    receivedMessage = Client.Receive();

                    if (receivedMessage != null)
                    {
                        // Process the message
                        Trace.WriteLine("Processing", receivedMessage.SequenceNumber.ToString());

                        OnlineOrder order = receivedMessage.GetBody<OnlineOrder>    ();
                        Trace.WriteLine(order.Customer + ": " + order.Product, "ProcessingMessage");

                        receivedMessage.Complete();
                    }
                }
                catch (MessagingException e)
                {
                    if (!e.IsTransient)
                    {
                        Trace.WriteLine(e.Message);
                        throw;
                    }

                    Thread.Sleep(10000);
                }
                catch (OperationCanceledException e)
                {
                    if (!IsStopped)
                    {
                        Trace.WriteLine(e.Message);
                        throw;
                    }
                }
            }
        }
        public AzureServiceBusMessageQueue(string connectionString, string inputQueue)
        {
            try
            {
                log.Info("Initializing Azure Service Bus transport with logical input queue '{0}'", inputQueue);

                namespaceManager = NamespaceManager.CreateFromConnectionString(connectionString);

                InputQueue = inputQueue;

                log.Info("Ensuring that topic '{0}' exists", TopicName);
                if (!namespaceManager.TopicExists(TopicName))
                {
                    try
                    {
                        namespaceManager.CreateTopic(TopicName);
                    }
                    catch
                    {
                        // just assume the call failed because the topic already exists - if GetTopic below
                        // fails, then something must be wrong, and then we just want to fail immediately
                    }
                }

                topicDescription = namespaceManager.GetTopic(TopicName);

                log.Info("Creating topic client");
                topicClient = TopicClient.CreateFromConnectionString(connectionString, topicDescription.Path);

                // if we're in one-way mode, just quit here
                if (inputQueue == null) return;

                GetOrCreateSubscription(InputQueue);

                log.Info("Creating subscription client");
                subscriptionClient = SubscriptionClient.CreateFromConnectionString(connectionString, TopicName, InputQueue, ReceiveMode.PeekLock);
            }
            catch (Exception e)
            {
                throw new ApplicationException(
                    string.Format(
                        "An error occurred while initializing Azure Service Bus with logical input queue '{0}'",
                        inputQueue), e);
            }
        }
Example #29
0
        private void InitServiceBusConnection()
        {
            // Create a token provider with the relevant credentials.
            TokenProvider credentials =
                TokenProvider.CreateSharedSecretTokenProvider(ConfigurationManager.AppSettings["ServiceBusIssuer"],
                                                              ConfigurationManager.AppSettings["ServiceBusKey"]);
            // Create a URI for the serivce bus.
            Uri serviceBusUri = ServiceBusEnvironment.CreateServiceUri
                ("sb", ConfigurationManager.AppSettings["ServiceBusNamespace"], string.Empty);

            // Create a message factory for the service bus URI using the credentials
            MessagingFactory factory = MessagingFactory.Create
            (serviceBusUri, credentials);

            // Initialize clients
            _savaClient = factory.CreateSubscriptionClient("channel_sava", "twitter_follower_client");
            _plamenClient = factory.CreateSubscriptionClient("channel_plamen", "twitter_follower_client");
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="SubscriptionReceiver"/> class, 
        /// automatically creating the topic and subscription if they don't exist.
        /// </summary>
        protected SubscriptionReceiver(ServiceBusSettings settings, string topic, string subscription, RetryStrategy backgroundRetryStrategy)
        {
            this.settings = settings;
            this.topic = topic;
            this.subscription = subscription;

            this.tokenProvider = TokenProvider.CreateSharedSecretTokenProvider(settings.TokenIssuer, settings.TokenAccessKey);
            this.serviceUri = ServiceBusEnvironment.CreateServiceUri(settings.ServiceUriScheme, settings.ServiceNamespace, settings.ServicePath);

            var messagingFactory = MessagingFactory.Create(this.serviceUri, tokenProvider);
            this.client = messagingFactory.CreateSubscriptionClient(topic, subscription);

            this.receiveRetryPolicy = new RetryPolicy<ServiceBusTransientErrorDetectionStrategy>(backgroundRetryStrategy);
            this.receiveRetryPolicy.Retrying +=
                (s, e) =>
                {
                    Trace.TraceError("An error occurred in attempt number {1} to receive a message: {0}", e.LastException.Message, e.CurrentRetryCount);
                };
        }
        public SubscriptionClient GetSubscriptionClient(string subscriptionName)
        {
            var client = SubscriptionClient.Create(TOPIC_NAME, subscriptionName, ReceiveMode.ReceiveAndDelete);

            return(client);
        }
Example #32
0
 public SubscriptionReceiver(string connectionString, string topicPath, string subscriptionName)
 {
     m_SubscriptionClient = new SubscriptionClient(connectionString, topicPath, subscriptionName);
 }
Example #33
0
        public void RegisterForEvents()
        {
            var c = new BusSubscriptionClient(SubscriptionClient.CreateFromConnectionString(connection, this.topic.Name, "testing"));

            c.OnMessage((BrokeredMessage msg) => { return(Task.Run(() => { })); }, new OnMessageOptions());
        }
Example #34
0
 protected void SetupManagementClients(MockContext context)
 {
     ResourceGraphClient = GetResourceGraphClient(context);
     SubscriptionClient  = GetSubscriptionClient(context);
     _helper.SetupManagementClients(ResourceGraphClient, SubscriptionClient);
 }
        private async Task OnSessionTestAsync(bool partitioned, bool sessionEnabled, int maxConcurrentCalls, ReceiveMode mode, bool autoComplete)
        {
            await ServiceBusScope.UsingTopicAsync(partitioned, sessionEnabled, async (topicName, subscriptionName) =>
            {
                TestUtility.Log($"Topic: {topicName}, MaxConcurrentCalls: {maxConcurrentCalls}, Receive Mode: {mode.ToString()}, AutoComplete: {autoComplete}");
                var topicClient        = new TopicClient(TestUtility.NamespaceConnectionString, topicName);
                var subscriptionClient = new SubscriptionClient(
                    TestUtility.NamespaceConnectionString,
                    topicClient.TopicName,
                    subscriptionName,
                    ReceiveMode.PeekLock);

                try
                {
                    var sessionHandlerOptions =
                        new SessionHandlerOptions(ExceptionReceivedHandler)
                    {
                        MaxConcurrentSessions = maxConcurrentCalls,
                        MessageWaitTimeout    = TimeSpan.FromSeconds(5),
                        AutoComplete          = true
                    };

                    var testSessionHandler = new TestSessionHandler(
                        subscriptionClient.ReceiveMode,
                        sessionHandlerOptions,
                        topicClient.InnerSender,
                        subscriptionClient.SessionPumpHost);

                    // Send messages to Session
                    await testSessionHandler.SendSessionMessages();

                    // Register handler
                    testSessionHandler.RegisterSessionHandler(sessionHandlerOptions);

                    // Verify messages were received.
                    await testSessionHandler.VerifyRun();

                    testSessionHandler.ClearData();
                }
                finally
                {
                    await subscriptionClient.CloseAsync();
                    await topicClient.CloseAsync();
                }
            });

            // test UnregisterSessionHandler can wait for message handling upto the timeout user defined.
            await ServiceBusScope.UsingTopicAsync(partitioned, sessionEnabled, async (topicName, subscriptionName) =>
            {
                TestUtility.Log($"Topic: {topicName}, MaxConcurrentCalls: {maxConcurrentCalls}, Receive Mode: {mode.ToString()}, AutoComplete: {autoComplete}");
                var topicClient        = new TopicClient(TestUtility.NamespaceConnectionString, topicName);
                var subscriptionClient = new SubscriptionClient(
                    TestUtility.NamespaceConnectionString,
                    topicClient.TopicName,
                    subscriptionName,
                    ReceiveMode.PeekLock);

                try
                {
                    var sessionHandlerOptions =
                        new SessionHandlerOptions(ExceptionReceivedHandler)
                    {
                        MaxConcurrentSessions = maxConcurrentCalls,
                        MessageWaitTimeout    = TimeSpan.FromSeconds(5),
                        AutoComplete          = true
                    };

                    var testSessionHandler = new TestSessionHandler(
                        subscriptionClient.ReceiveMode,
                        sessionHandlerOptions,
                        topicClient.InnerSender,
                        subscriptionClient.SessionPumpHost);

                    // Send messages to Session
                    await testSessionHandler.SendSessionMessages();

                    // Register handler
                    testSessionHandler.RegisterSessionHandlerAndRecordReceivedMessageCount(subscriptionClient.ReceiveMode == ReceiveMode.PeekLock, 8);

                    // Session handler set up has greater latency than message handler. Delay here to enable some processing time of the session pump.
                    await Task.Delay(TimeSpan.FromSeconds(2));

                    // UnregisterSessionHandler should wait up to the provided timeout to finish the message handling tasks
                    await testSessionHandler.UnregisterSessionHandler(TimeSpan.FromSeconds(10));
                    Assert.True(testSessionHandler.ReceivedMessageCount == maxConcurrentCalls);

                    // Reregister won't have any problems
                    testSessionHandler.RegisterSessionHandlerAndRecordReceivedMessageCount(subscriptionClient.ReceiveMode == ReceiveMode.PeekLock, 0);
                    await testSessionHandler.WaitForAllMessagesReceived(ExpectedMessageCount);
                    Assert.True(testSessionHandler.ReceivedMessageCount == ExpectedMessageCount);

                    testSessionHandler.ClearData();
                }
                finally
                {
                    await subscriptionClient.CloseAsync();
                    await topicClient.CloseAsync();
                }
            });

            // test UnregisterSessionHandler can close down in time when message handling takes longer than wait timeout user defined.
            await ServiceBusScope.UsingTopicAsync(partitioned, sessionEnabled, async (topicName, subscriptionName) =>
            {
                TestUtility.Log($"Topic: {topicName}, MaxConcurrentCalls: {maxConcurrentCalls}, Receive Mode: {mode.ToString()}, AutoComplete: {autoComplete}");
                var topicClient        = new TopicClient(TestUtility.NamespaceConnectionString, topicName);
                var subscriptionClient = new SubscriptionClient(
                    TestUtility.NamespaceConnectionString,
                    topicClient.TopicName,
                    subscriptionName,
                    ReceiveMode.PeekLock);

                try
                {
                    var sessionHandlerOptions =
                        new SessionHandlerOptions(ExceptionReceivedHandler)
                    {
                        MaxConcurrentSessions = maxConcurrentCalls,
                        MessageWaitTimeout    = TimeSpan.FromSeconds(5),
                        AutoComplete          = true
                    };

                    var testSessionHandler = new TestSessionHandler(
                        subscriptionClient.ReceiveMode,
                        sessionHandlerOptions,
                        topicClient.InnerSender,
                        subscriptionClient.SessionPumpHost);

                    // Send messages to Session
                    await testSessionHandler.SendSessionMessages();

                    // Register handler
                    testSessionHandler.RegisterSessionHandlerAndRecordReceivedMessageCount(subscriptionClient.ReceiveMode == ReceiveMode.PeekLock, 8);

                    // UnregisterSessionHandler should wait up to the provided timeout to finish the message handling tasks
                    await testSessionHandler.UnregisterSessionHandler(TimeSpan.FromSeconds(2));
                    Assert.True(testSessionHandler.ReceivedMessageCount == 0);

                    // Reregister won't have any problems
                    testSessionHandler.RegisterSessionHandlerAndRecordReceivedMessageCount(subscriptionClient.ReceiveMode == ReceiveMode.PeekLock, 0);
                    await testSessionHandler.WaitForAllMessagesReceived(ExpectedMessageCount);
                    Assert.True(testSessionHandler.ReceivedMessageCount == ExpectedMessageCount);

                    testSessionHandler.ClearData();
                }
                finally
                {
                    await subscriptionClient.CloseAsync();
                    await topicClient.CloseAsync();
                }
            });
        }
 public TopicServiceBusSubscriber(string connectionString, string topicPath, string subscriptionName)
 {
     _subscriptionClient = SubscriptionClient.CreateFromConnectionString(connectionString, topicPath, subscriptionName);
 }
Example #37
0
        public SubscriptionObserver Create(IServiceBus bus, SubscriptionRouter router)
        {
            var client = new SubscriptionClient(bus, router, _subscriptionServiceUri, _timeout);

            return(client);
        }
Example #38
0
        public override void ReceiveMessages(CancellationToken token)
        {
            SubscriptionClients = new List <SubscriptionClient>(MaximumConcurrency);
            List <Task> receivers = new List <Task>(MaximumConcurrency);

            for (int i = 0; i < MaximumConcurrency; i++)
            {
                Task receiver = Task.Factory.StartNew(() =>
                {
                    var subscriptionClient = SubscriptionClient.CreateFromConnectionString(WasServiceBusConnectionString, WasTopicPath, WasSubscriptionName);
                    SubscriptionClients.Add(subscriptionClient);

                    subscriptionClient.OnMessage(brokeredMessage =>
                    {
                        try
                        {
                            var message = brokeredMessage.GetBody <VariantCopyCompletedV1>();
                            var variantCopyCompletedV1Handler =
                                new VariantCopyCompletedV1Handler(StockQuantityAggregateStore, TelemetryClient);
                            variantCopyCompletedV1Handler.OnMessage(message);

                            brokeredMessage.Complete();
                        }
                        catch (OptimisticConcurrencyException ex)
                        {
                            string err = ex.Message;
                            if (ex.InnerException != null)
                            {
                                err += " Inner Exception: " + ex.InnerException.Message;
                            }
                            Trace.TraceError(err, ex);

                            //Retry transisent exceptions By Abandoning it
                            brokeredMessage.Abandon();
                            TelemetryClient.TrackEvent("OptimisticConcurrencyAbandonedForRetryEvents");
                        }
                        catch (ResourceNotFoundException ex)
                        {
                            string err = ex.Message;
                            if (ex.InnerException != null)
                            {
                                err += " Inner Exception: " + ex.InnerException.Message;
                            }
                            Trace.TraceError(err, ex);

                            //Retry transisent exceptions By Abandoning it
                            brokeredMessage.Abandon();
                            TelemetryClient.TrackEvent("ResourceNotFoundAbandonedForRetryEvents");
                        }
                        catch (Exception ex)
                        {
                            string err = ex.Message;
                            if (ex.InnerException != null)
                            {
                                err += " Inner Exception: " + ex.InnerException.Message;
                            }
                            Trace.TraceError(err, ex);
                            brokeredMessage.DeadLetter();
                            TelemetryClient.TrackEvent("CriticallyFailedEvents");
                        }
                    });
                }, token, TaskCreationOptions.LongRunning, TaskScheduler.Default);

                receivers.Add(receiver);
            }

            Task.WhenAll(receivers).Wait(token);
        }
        private static void ReceiveMessages()
        {
            // For PeekLock mode (default) where applications require "at least once" delivery of messages
            SubscriptionClient agentSubscriptionClient = SubscriptionClient.Create(TopicName, "AgentSubscription");
            BrokeredMessage    message = null;

            while (true)
            {
                try
                {
                    //receive messages from Agent Subscription
                    message = agentSubscriptionClient.Receive(TimeSpan.FromSeconds(5));
                    if (message != null)
                    {
                        Console.WriteLine("\nReceiving message from AgentSubscription...");
                        Console.WriteLine(string.Format("Message received: Id = {0}, Body = {1}", message.MessageId, message.GetBody <string>()));
                        // Further custom message processing could go here...
                        message.Complete();
                    }
                    else
                    {
                        //no more messages in the subscription
                        break;
                    }
                }
                catch (MessagingException e)
                {
                    if (!e.IsTransient)
                    {
                        Console.WriteLine(e.Message);
                        throw;
                    }
                    else
                    {
                        HandleTransientErrors(e);
                    }
                }
            }

            // For ReceiveAndDelete mode, where applications require "best effort" delivery of messages
            SubscriptionClient auditSubscriptionClient = SubscriptionClient.Create(TopicName, "AuditSubscription", ReceiveMode.ReceiveAndDelete);

            while (true)
            {
                try
                {
                    message = auditSubscriptionClient.Receive(TimeSpan.FromSeconds(5));
                    if (message != null)
                    {
                        Console.WriteLine("\nReceiving message from AuditSubscription...");
                        Console.WriteLine(string.Format("Message received: Id = {0}, Body = {1}", message.MessageId, message.GetBody <string>()));
                        // Further custom message processing could go here...
                    }
                    else
                    {
                        //no more messages in the subscription
                        break;
                    }
                }
                catch (MessagingException e)
                {
                    if (!e.IsTransient)
                    {
                        Console.WriteLine(e.Message);
                        throw;
                    }
                }
            }

            agentSubscriptionClient.Close();
            auditSubscriptionClient.Close();
        }
 public SubscriptionSessionMessagePump(SubscriptionClient client, OnSessionMessageOptions options = null)
     : base(client, client.Mode, client.MessagingFactory.GetShortNamespaceName(), client.TopicPath + "/" + client.Name, options)
 {
     _client = client;
 }
Example #41
0
        public ISubscriptionClient CreateFromConnectionString(string connectionString, string topicPath, string name, ReceiveMode mode)
        {
            var subscriptionClient = SubscriptionClient.CreateFromConnectionString(connectionString, topicPath, name, mode);

            return(new SubscriptionClientWrapper(subscriptionClient));
        }
Example #42
0
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddDataProtection().PersistKeysToFileSystem(new DirectoryInfo(@"/var/lib/dataprotected/"));

            services.AddDbContext <NotifyContext>(options =>
                                                  options.UseSqlServer(Configuration["ConnectionString"],
                                                                       sqlServerOptionsAction: sqlOptions =>
            {
                sqlOptions.EnableRetryOnFailure(
                    maxRetryCount: 10,
                    maxRetryDelay: TimeSpan.FromSeconds(60),
                    errorNumbersToAdd: null
                    );
            }
                                                                       ));

            services.AddDbContext <LogContext>(options =>
                                               options.UseSqlServer(Configuration["ConnectionString"]));

            services.Configure <NotifySettings>(Configuration);

            services.AddSingleton <IHttpContextAccessor, HttpContextAccessor>();
            services.AddTransient <IIdentityService, IdentityService>();

            services.AddAuthentication(options =>
            {
                options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
                options.DefaultChallengeScheme    = JwtBearerDefaults.AuthenticationScheme;
            })
            .AddJwtBearer(options =>
            {
                options.Audience  = "sigecoservices";
                options.Authority = Configuration["IdentityUrl"];

                options.RequireHttpsMetadata = true;
            });

            services.AddCors(options =>
            {
                options.AddPolicy("CorsPolicy",
                                  cors => cors.WithOrigins("http://*****:*****@x-pertec.com", Url = new Uri("https://x-pertec.com")
                    }
                });

                var basePath = PlatformServices.Default.Application.ApplicationBasePath;
                var xmlPath  = Path.Combine(basePath, "notifyapi.xml");
                options.IncludeXmlComments(xmlPath);
            });

            services.AddSignalR();

            services.AddControllers();

            //configure servicebus
            services.AddTransient <Func <DbConnection, IIntegrationEventLogService> >(
                sp => (DbConnection c) => new IntegrationEventLogService(c)
                );

            services.AddSingleton <IServiceBusPersisterConnection>(sp =>
            {
                var serviceBusConnection        = new ServiceBusConnectionStringBuilder(Configuration["EventBusConnection"]);
                serviceBusConnection.EntityPath = Configuration["TopicName"];

                return(new DefaultServiceBusPersisterConnection(serviceBusConnection));
            });

            services.AddSingleton <IEventBus, EventBusServiceBus>(sp =>
            {
                var serviceBusPersisterConnection = sp.GetRequiredService <IServiceBusPersisterConnection>();
                var subscriptionClientName        = Configuration["SubscriptionClientName"];
                var subscriptionClient            = new SubscriptionClient(serviceBusPersisterConnection.ServiceBusConnectionStringBuilder, subscriptionClientName);
                var eventBusSubcriptionsManager   = sp.GetRequiredService <IEventBusSubscriptionsManager>();
                var iLifetimeScope = sp.GetRequiredService <ILifetimeScope>();

                return(new EventBusServiceBus(serviceBusPersisterConnection, eventBusSubcriptionsManager, subscriptionClient, iLifetimeScope));
            });

            services.AddTransient <UsuarioCreadoIntegrationEventHandler>();
            services.AddTransient <UsuarioEliminadoIntegrationEventHandler>();
            services.AddTransient <UsuarioModificadoIntegrationEventHandler>();
            services.AddSingleton <IEventBusSubscriptionsManager, InMemoryEventBusSubscriptionsManager>();
        }
Example #43
0
        private async void buttonPickupAccount_Click(object sender, EventArgs e)
        {
            AddAMSAccount1 addaccount1 = new AddAMSAccount1();

            if (addaccount1.ShowDialog() == DialogResult.OK)
            {
                if (addaccount1.SelectedMode == AddAccountMode.BrowseSubscriptions)
                {
                    Cursor = Cursors.WaitCursor;

                    environment = addaccount1.GetEnvironment();

                    AuthenticationContext authContext = new AuthenticationContext(
                        // authority:  environment.Authority,
                        authority: environment.AADSettings.AuthenticationEndpoint.ToString() + "common",
                        validateAuthority: true
                        );

                    AuthenticationResult accessToken;
                    try
                    {
                        accessToken = await authContext.AcquireTokenAsync(
                            resource : environment.AADSettings.TokenAudience.ToString(),
                            clientId : environment.ClientApplicationId,
                            redirectUri : new Uri("urn:ietf:wg:oauth:2.0:oob"),
                            parameters : new PlatformParameters(addaccount1.SelectUser ? PromptBehavior.SelectAccount : PromptBehavior.Auto)
                            );
                    }
                    catch (Exception ex)
                    {
                        Cursor = Cursors.Default;
                        MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        return;
                    }

                    TokenCredentials credentials = new TokenCredentials(accessToken.AccessToken, "Bearer");

                    SubscriptionClient subscriptionClient = new SubscriptionClient(environment.ArmEndpoint, credentials);

                    // Subcriptions listing
                    List <Subscription>  subscriptions     = new List <Subscription>();
                    IPage <Subscription> subscriptionsPage = subscriptionClient.Subscriptions.List();
                    while (subscriptionsPage != null)
                    {
                        subscriptions.AddRange(subscriptionsPage);
                        if (subscriptionsPage.NextPageLink != null)
                        {
                            subscriptionsPage = subscriptionClient.Subscriptions.ListNext(subscriptionsPage.NextPageLink);
                        }
                        else
                        {
                            subscriptionsPage = null;
                        }
                    }

                    // Tenants listing
                    List <TenantIdDescription>  tenants     = new List <TenantIdDescription>();
                    IPage <TenantIdDescription> tenantsPage = subscriptionClient.Tenants.List();
                    while (tenantsPage != null)
                    {
                        tenants.AddRange(tenantsPage);
                        if (tenantsPage.NextPageLink != null)
                        {
                            tenantsPage = subscriptionClient.Tenants.ListNext(tenantsPage.NextPageLink);
                        }
                        else
                        {
                            tenantsPage = null;
                        }
                    }

                    /*
                     * // Tenants browsing
                     * myTenants tenants = new myTenants();
                     * string URL = environment.ArmEndpoint + "tenants?api-version=2020-01-01";
                     *
                     * HttpClient client = new HttpClient();
                     * client.DefaultRequestHeaders.Remove("Authorization");
                     * client.DefaultRequestHeaders.Add("Authorization", "Bearer " + accessToken.AccessToken);
                     * HttpResponseMessage response = await client.GetAsync(URL);
                     * if (response.IsSuccessStatusCode)
                     * {
                     *  string str = await response.Content.ReadAsStringAsync();
                     *  tenants = (myTenants)JsonConvert.DeserializeObject(str, typeof(myTenants));
                     * }
                     */
                    Cursor = Cursors.Default;

                    AddAMSAccount2Browse addaccount2 = new AddAMSAccount2Browse(credentials, subscriptions, environment, tenants, new PlatformParameters(addaccount1.SelectUser ? PromptBehavior.SelectAccount : PromptBehavior.Auto));

                    if (addaccount2.ShowDialog() == DialogResult.OK)
                    {
                        // Getting Media Services accounts...
                        AzureMediaServicesClient mediaServicesClient = new AzureMediaServicesClient(environment.ArmEndpoint, credentials);

                        CredentialsEntryV3 entry = new CredentialsEntryV3(addaccount2.selectedAccount,
                                                                          environment,
                                                                          addaccount1.SelectUser ? PromptBehavior.SelectAccount : PromptBehavior.Auto,
                                                                          false,
                                                                          addaccount2.selectedTenantId,
                                                                          false
                                                                          );
                        CredentialList.MediaServicesAccounts.Add(entry);
                        AddItemToListviewAccounts(entry);

                        SaveCredentialsToSettings();
                    }
                    else
                    {
                        return;
                    }
                }


                // Get info from the Portal or Azure CLI JSON
                else if (addaccount1.SelectedMode == AddAccountMode.FromAzureCliOrPortalJson)
                {
                    string        example = @"{
  ""AadClientId"": ""00000000-0000-0000-0000-000000000000"",
  ""AadEndpoint"": ""https://login.microsoftonline.com"",
  ""AadSecret"": ""00000000-0000-0000-0000-000000000000"",
  ""AadTenantId"": ""00000000-0000-0000-0000-000000000000"",
  ""AccountName"": ""amsaccount"",
  ""ArmAadAudience"": ""https://management.core.windows.net/"",
  ""ArmEndpoint"": ""https://management.azure.com/"",
  ""Region"": ""West Europe"",
  ""ResourceGroup"": ""amsResourceGroup"",
  ""SubscriptionId"": ""00000000-0000-0000-0000-000000000000""
}";
                    EditorXMLJSON form    = new EditorXMLJSON("Enter the JSON output of the Azure Portal or Azure Cli Service Principal creation", example, true, false, true, "The Service Principal secret is stored encrypted in the application settings.");

                    if (form.ShowDialog() == DialogResult.OK)
                    {
                        JsonFromAzureCliOrPortal json = null;
                        try
                        {
                            json = (JsonFromAzureCliOrPortal)JsonConvert.DeserializeObject(form.TextData, typeof(JsonFromAzureCliOrPortal));
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show(ex.Message, "Error reading the json", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            return;
                        }
                        string resourceId  = string.Format("/subscriptions/{0}/resourceGroups/{1}/providers/Microsoft.Media/mediaservices/{2}", json.SubscriptionId, json.ResourceGroup, json.AccountName);
                        string AADtenantId = json.AadTenantId;

                        // environment
                        ActiveDirectoryServiceSettings aadSettings = new ActiveDirectoryServiceSettings()
                        {
                            AuthenticationEndpoint = json.AadEndpoint ?? addaccount1.GetEnvironment().AADSettings.AuthenticationEndpoint,
                            TokenAudience          = json.ArmAadAudience,
                            ValidateAuthority      = true
                        };

                        AzureEnvironment env = new AzureEnvironment(AzureEnvType.Custom)
                        {
                            AADSettings = aadSettings, ArmEndpoint = json.ArmEndpoint
                        };

                        CredentialsEntryV3 entry = new CredentialsEntryV3(
                            new SubscriptionMediaService(resourceId, json.AccountName, null, null, json.Location ?? json.Region),
                            env,
                            PromptBehavior.Auto,
                            true,
                            AADtenantId,
                            false,
                            json.AadClientId,
                            json.AadSecret
                            );

                        CredentialList.MediaServicesAccounts.Add(entry);
                        AddItemToListviewAccounts(entry);

                        SaveCredentialsToSettings();
                    }
                    else
                    {
                        return;
                    }
                }
                else if (addaccount1.SelectedMode == AddAccountMode.ManualEntry)
                {
                    AddAMSAccount2Manual form = new AddAMSAccount2Manual();
                    if (form.ShowDialog() == DialogResult.OK)
                    {
                        string accountnamecc = form.textBoxAMSResourceId.Text.Split('/').Last();

                        CredentialsEntryV3 entry = new CredentialsEntryV3(
                            new SubscriptionMediaService(form.textBoxAMSResourceId.Text, accountnamecc, null, null, form.textBoxLocation.Text),
                            addaccount1.GetEnvironment(),
                            PromptBehavior.Auto,
                            form.radioButtonAADServicePrincipal.Checked,
                            form.textBoxAADtenantId.Text,
                            true
                            );

                        CredentialList.MediaServicesAccounts.Add(entry);
                        AddItemToListviewAccounts(entry);

                        SaveCredentialsToSettings();
                    }
                    else
                    {
                        return;
                    }
                }
            }
        }
        async Task GetAndSetSessionStateTest(string topicName)
        {
            var entityConnectionString = TestUtility.GetEntityConnectionString(topicName);
            var topicClient            = TopicClient.CreateFromConnectionString(entityConnectionString);
            var subscriptionClient     = SubscriptionClient.CreateFromConnectionString(entityConnectionString, this.SubscriptionName);

            try
            {
                var messageId = "test-message1";
                var sessionId = Guid.NewGuid().ToString();
                await topicClient.SendAsync(new BrokeredMessage()
                {
                    MessageId = messageId, SessionId = sessionId
                });

                TestUtility.Log($"Sent Message: {messageId} to Session: {sessionId}");

                var sessionReceiver = await subscriptionClient.AcceptMessageSessionAsync(sessionId);

                Assert.NotNull((object)sessionReceiver);
                var message = await sessionReceiver.ReceiveAsync();

                TestUtility.Log($"Received Message: {message.MessageId} from Session: {sessionReceiver.SessionId}");
                Assert.True(message.MessageId == messageId);

                var sessionStateString = "Received Message From Session!";
                var sessionState       = new MemoryStream(Encoding.UTF8.GetBytes(sessionStateString));
                await sessionReceiver.SetStateAsync(sessionState);

                TestUtility.Log($"Set Session State: {sessionStateString} for Session: {sessionReceiver.SessionId}");

                var returnedSessionState = await sessionReceiver.GetStateAsync();

                using (var reader = new StreamReader(returnedSessionState, Encoding.UTF8))
                {
                    var returnedSessionStateString = await reader.ReadToEndAsync();

                    TestUtility.Log($"Get Session State Returned: {returnedSessionStateString} for Session: {sessionReceiver.SessionId}");
                    Assert.Equal(sessionStateString, returnedSessionStateString);
                }

                // Complete message using Session Receiver
                await sessionReceiver.CompleteAsync(new Guid[] { message.LockToken });

                TestUtility.Log($"Completed Message: {message.MessageId} for Session: {sessionReceiver.SessionId}");

                sessionStateString = "Completed Message On Session!";
                sessionState       = new MemoryStream(Encoding.UTF8.GetBytes(sessionStateString));
                await sessionReceiver.SetStateAsync(sessionState);

                TestUtility.Log($"Set Session State: {sessionStateString} for Session: {sessionReceiver.SessionId}");

                returnedSessionState = await sessionReceiver.GetStateAsync();

                using (var reader = new StreamReader(returnedSessionState, Encoding.UTF8))
                {
                    var returnedSessionStateString = await reader.ReadToEndAsync();

                    TestUtility.Log($"Get Session State Returned: {returnedSessionStateString} for Session: {sessionReceiver.SessionId}");
                    Assert.Equal(sessionStateString, returnedSessionStateString);
                }

                await sessionReceiver.CloseAsync();
            }
            finally
            {
                subscriptionClient.Close();
                topicClient.Close();
            }
        }
Example #45
0
        /// <summary>
        /// Calls <see cref="AzureServiceBus{TAuthenticationToken}.InstantiateReceiving()"/>
        /// then uses a <see cref="Task"/> to apply the <see cref="FilterKey"/> as a <see cref="RuleDescription"/>
        /// to the <see cref="SubscriptionClient"/> instances in <paramref name="serviceBusReceivers"/>.
        /// </summary>
        /// <param name="namespaceManager">The <see cref="NamespaceManager"/>.</param>
        /// <param name="serviceBusReceivers">The receivers collection to place <see cref="SubscriptionClient"/> instances into.</param>
        /// <param name="topicName">The topic name.</param>
        /// <param name="topicSubscriptionName">The topic subscription name.</param>
        protected override void InstantiateReceiving(NamespaceManager namespaceManager, IDictionary <int, SubscriptionClient> serviceBusReceivers, string topicName, string topicSubscriptionName)
        {
            base.InstantiateReceiving(namespaceManager, serviceBusReceivers, topicName, topicSubscriptionName);

            Task.Factory.StartNewSafely
                (() =>
            {
                // Because refreshing the rule can take a while, we only want to do this when the value changes
                string filter;
                if (!ConfigurationManager.TryGetSetting(FilterKeyConfigurationKey, out filter))
                {
                    return;
                }
                if (FilterKey == filter)
                {
                    return;
                }
                FilterKey = filter;

                // https://docs.microsoft.com/en-us/azure/application-insights/app-insights-analytics-reference#summarize-operator
                // http://www.summa.com/blog/business-blog/everything-you-need-to-know-about-azure-service-bus-brokered-messaging-part-2#rulesfiltersactions
                // https://github.com/Azure-Samples/azure-servicebus-messaging-samples/tree/master/TopicFilters
                SubscriptionClient client = serviceBusReceivers[0];
                bool reAddRule            = false;
                try
                {
                    IEnumerable <RuleDescription> rules = namespaceManager.GetRules(client.TopicPath, client.Name).ToList();
                    RuleDescription ruleDescription     = rules.SingleOrDefault(rule => rule.Name == "CqrsConfiguredFilter");
                    if (ruleDescription != null)
                    {
                        var sqlFilter = ruleDescription.Filter as SqlFilter;
                        if (sqlFilter == null && !string.IsNullOrWhiteSpace(filter))
                        {
                            reAddRule = true;
                        }
                        else if (sqlFilter != null && sqlFilter.SqlExpression != filter)
                        {
                            reAddRule = true;
                        }
                        if (sqlFilter != null && reAddRule)
                        {
                            client.RemoveRule("CqrsConfiguredFilter");
                        }
                    }
                    else if (!string.IsNullOrWhiteSpace(filter))
                    {
                        reAddRule = true;
                    }

                    ruleDescription = rules.SingleOrDefault(rule => rule.Name == "$Default");
                    // If there is a default rule and we have a rule, it will cause issues
                    if (!string.IsNullOrWhiteSpace(filter) && ruleDescription != null)
                    {
                        client.RemoveRule("$Default");
                    }
                    // If we don't have a rule and there is no longer a default rule, it will cause issues
                    else if (string.IsNullOrWhiteSpace(filter) && !rules.Any())
                    {
                        ruleDescription = new RuleDescription
                                          (
                            "$Default",
                            new SqlFilter("1=1")
                                          );
                        client.AddRule(ruleDescription);
                    }
                }
                catch (MessagingEntityNotFoundException)
                {
                }

                if (!reAddRule)
                {
                    return;
                }

                int loopCounter = 0;
                while (loopCounter < 10)
                {
                    try
                    {
                        RuleDescription ruleDescription = new RuleDescription
                                                          (
                            "CqrsConfiguredFilter",
                            new SqlFilter(filter)
                                                          );
                        client.AddRule(ruleDescription);
                        break;
                    }
                    catch (MessagingEntityAlreadyExistsException exception)
                    {
                        loopCounter++;
                        // Still waiting for the delete to complete
                        Thread.Sleep(1000);
                        if (loopCounter == 9)
                        {
                            Logger.LogError("Setting the filter failed as it already exists.", exception: exception);
                            TelemetryHelper.TrackException(exception);
                        }
                    }
                    catch (Exception exception)
                    {
                        Logger.LogError("Setting the filter failed.", exception: exception);
                        TelemetryHelper.TrackException(exception);
                        break;
                    }
                }
            });
        }
 public SubscriptionClientContext(SubscriptionClient client, Uri inputAddress, ClientSettings settings)
 {
     _client      = client;
     _settings    = settings;
     InputAddress = inputAddress;
 }
 private void Inicializar()
 {
     _endpointServiceBus           = _configuration.GetConnectionString("EndpointServiceBusConnection");
     _serviceBusClientProdutoVenda = new SubscriptionClient(_endpointServiceBus, "produtovendido", "produtovendidosubscricao");
 }
Example #48
0
        private List <AzureTenant> ListAccountTenants(
            IAzureAccount account,
            IAzureEnvironment environment,
            SecureString password,
            string promptBehavior,
            Action <string> promptAction)
        {
            List <AzureTenant> result = new List <AzureTenant>();
            var commonTenant          = account.GetCommonTenant();

            try
            {
                var commonTenantToken = AcquireAccessToken(
                    account,
                    environment,
                    commonTenant,
                    password,
                    promptBehavior,
                    promptAction);

                SubscriptionClient subscriptionClient = null;
                try
                {
                    subscriptionClient = AzureSession.Instance.ClientFactory.CreateCustomArmClient <SubscriptionClient>(
                        environment.GetEndpointAsUri(AzureEnvironment.Endpoint.ResourceManager),
                        new TokenCredentials(commonTenantToken.AccessToken) as ServiceClientCredentials,
                        AzureSession.Instance.ClientFactory.GetCustomHandlers());
                    //TODO: Fix subscription client to not require subscriptionId
                    result = MergeTenants(account, subscriptionClient.Tenants.List(), commonTenantToken);
                }
                finally
                {
                    // In test mode, we are reusing the client since disposing of it will
                    // fail some tests (due to HttpClient being null)
                    if (subscriptionClient != null && !TestMockSupport.RunningMocked)
                    {
                        subscriptionClient.Dispose();
                    }
                }
            }
            catch
            {
                // Unable to acquire token for tenant
                if (account.IsPropertySet(AzureAccount.Property.Tenants))
                {
                    result =
                        account.GetPropertyAsArray(AzureAccount.Property.Tenants)
                        .Select(ti =>
                    {
                        var tenant = new AzureTenant();

                        Guid guid;
                        if (Guid.TryParse(ti, out guid))
                        {
                            tenant.Id        = ti;
                            tenant.Directory = AccessTokenExtensions.GetDomain(account.Id);
                        }
                        else
                        {
                            tenant.Directory = ti;
                        }

                        return(tenant);
                    }).ToList();
                }
                if (!result.Any())
                {
                    throw;
                }
            }

            return(result);
        }
Example #49
0
        static void ListenOnServiceBusTopic()
        {
            /* Create Topic Subscription Client, and bind with Message Property on companyid = xx */
            var namespaceManager = NamespaceManager.CreateFromConnectionString(_sbConnectionString);
            //string subscriptionName = "iothubeventprocessor_iothubalias_" + _IoTHubAlias;
            string    subscriptionName = GetEventProcessorHostName(_IoTHubAlias);
            SqlFilter messageFilter    = new SqlFilter("process = 'iothubeventprocessor' AND iothubalias = '" + _IoTHubAlias + "'");

            /* If the subscription not exist, create it. */
            if (!namespaceManager.SubscriptionExists(_sbProcessCommandTopic, subscriptionName))
            {
                namespaceManager.CreateSubscription(_sbProcessCommandTopic, subscriptionName, messageFilter);
            }

            /* Create subscription client and listen on message  */
            _sbSubscriptionClient = SubscriptionClient.CreateFromConnectionString(_sbConnectionString, _sbProcessCommandTopic, subscriptionName);
            OnMessageOptions options = new OnMessageOptions();

            options.AutoComplete = true;

            _sbSubscriptionClient.OnMessage(async(message) =>
            {
                string command = "", messageBody = "";
                int taskId     = 0;

                try
                {
                    // Process message from subscription.
                    messageBody = message.GetBody <string>();
                    ConsoleLog.WriteBlobLogDebug("onMessage: {0}", messageBody);

                    JObject jsonMessage = JObject.Parse(messageBody);

                    if (jsonMessage["command"] != null)
                    {
                        command = jsonMessage["command"].ToString();
                    }
                    if (jsonMessage["taskId"] != null)
                    {
                        taskId = int.Parse(jsonMessage["taskId"].ToString());
                    }

                    ConsoleLog.WriteToConsole("Command:" + command);
                    ConsoleLog.WriteBlobLogInfo("Received Command:" + command);

                    switch (command.ToLower())
                    {
                    case "start":
                        await _IoTHubMessageReceiver.Start();
                        break;

                    case "stop":
                        await _IoTHubMessageReceiver.Stop();
                        break;

                    case "restart":
                        await _IoTHubMessageReceiver.Stop();
                        await _IoTHubMessageReceiver.Start();
                        break;

                    case "shutdown":
                        message.Complete();
                        await _IoTHubMessageReceiver.Stop();
                        UpdateTaskBySuccess(taskId);
                        Environment.Exit(0);
                        break;
                    }

                    UpdateTaskBySuccess(taskId);
                }
                catch (Exception ex)
                {
                    // Indicates a problem, unlock message in subscription.
                    ConsoleLog.WriteToConsole(ex.Message);
                    ConsoleLog.WriteBlobLogError("Exception: {0}", ex.Message);

                    message.Complete();

                    UpdateTaskByFail(taskId, ex.Message);
                }
            }, options);
        }
        private async void btnStart_Click(object sender, EventArgs e)
        {
            try
            {
                if (btnStart.Text == StopCaption)
                {
                    await CancelActions();

                    btnStart.Text = StartCaption;
                    return;
                }

                if (serviceBusHelper != null &&
                    ValidateParameters())
                {
                    startLog?.Invoke();
                    btnStart.Enabled = false;
                    Cursor.Current   = Cursors.WaitCursor;
                    //*****************************************************************************************************
                    //                                   Retrieve Messaging Factory
                    //*****************************************************************************************************
                    var messagingFactory = serviceBusHelper.MessagingFactory;

                    //*****************************************************************************************************
                    //                                   Initialize Statistics and Manager Action
                    //*****************************************************************************************************
                    actionCount               = 0;
                    receiverMessageNumber     = 0;
                    receiverMessagesPerSecond = 0;
                    receiverMinimumTime       = long.MaxValue;
                    receiverMaximumTime       = 0;
                    receiverAverageTime       = 0;
                    receiverTotalTime         = 0;
                    if (checkBoxReceiverEnableGraph.Checked)
                    {
                        chart.Series.ToList().ForEach(s => s.Points.Clear());
                    }
                    managerResetEvent = new ManualResetEventSlim(false);
                    Action <CancellationTokenSource> managerAction = cts =>
                    {
                        if (cts == null)
                        {
                            return;
                        }
                        try
                        {
                            managerResetEvent.Wait(cts.Token);
                        }
                        catch (OperationCanceledException)
                        {
                        }
                        if (!cts.IsCancellationRequested)
                        {
                            Invoke((MethodInvoker)async delegate
                            {
                                btnStart.Text = StartCaption;
                                await MainForm.SingletonMainForm.RefreshSelectedEntity();
                            });
                        }
                    };

                    Action updateGraphAction = () =>
                    {
                        var  ok  = true;
                        long max = 10;
                        while (!graphCancellationTokenSource.IsCancellationRequested && (actionCount > 1 || ok))
                        {
                            ok = true;
                            long receiveMessageNumber = 0;
                            long receiveTotalTime     = 0;
                            while (ok && receiveMessageNumber < max)
                            {
                                ok = blockingCollection.TryTake(out var tuple, 10);
                                if (ok)
                                {
                                    receiveMessageNumber += tuple.Item1;
                                    receiveTotalTime     += tuple.Item2;
                                    if (receiveMessageNumber > max)
                                    {
                                        max = receiveMessageNumber;
                                    }
                                }
                            }
                            if (receiveMessageNumber > 0)
                            {
                                var receiveTuple = new Tuple <long, long, DirectionType>(receiveMessageNumber, receiveTotalTime, DirectionType.Receive);
                                if (InvokeRequired)
                                {
                                    Invoke(new UpdateStatisticsDelegate(InternalUpdateStatistics), receiveTuple.Item1, receiveTuple.Item2, receiveTuple.Item3);
                                }
                                else
                                {
                                    InternalUpdateStatistics(receiveTuple.Item1,
                                                             receiveTuple.Item2,
                                                             receiveTuple.Item3);
                                }
                            }
                        }
                        if (Interlocked.Decrement(ref actionCount) == 0)
                        {
                            managerResetEvent.Set();
                        }
                    };

                    AsyncCallback updateGraphCallback = a =>
                    {
                        var action = a.AsyncState as Action;
                        if (action != null)
                        {
                            action.EndInvoke(a);
                            if (Interlocked.Decrement(ref actionCount) == 0)
                            {
                                managerResetEvent.Set();
                            }
                        }
                    };

                    blockingCollection = new BlockingCollection <Tuple <long, long, DirectionType> >();
                    //*****************************************************************************************************
                    //                                   Receiving messages from a Subscription
                    //*****************************************************************************************************
                    var currentSubscription = subscriptionWrapper.SubscriptionDescription;
                    if (currentSubscription == null)
                    {
                        throw new ArgumentException(NoSubscriptionSelected);
                    }
                    var currentReceiveMode = cboReceivedMode.Text == PeekLock ?
                                             ReceiveMode.PeekLock :
                                             ReceiveMode.ReceiveAndDelete;
                    var currentMoveToDeadLetterQueue   = checkBoxMoveToDeadLetter.Checked;
                    var currentReadFromDeadLetterQueue = checkBoxReadFromDeadLetter.Checked;

                    try
                    {
                        receiverCancellationTokenSource  = new CancellationTokenSource();
                        receiverCancellationTokenSource  = new CancellationTokenSource();
                        receiverBrokeredMessageInspector = cboReceiverInspector.SelectedIndex > 0
                                                      ? Activator.CreateInstance(serviceBusHelper.BrokeredMessageInspectors[cboReceiverInspector.Text]) as IBrokeredMessageInspector
                                                      : null;

                        Action <int> receiverAction = taskId =>
                        {
                            var allSessionsAccepted = false;

                            while (!allSessionsAccepted)
                            {
                                try
                                {
                                    MessageReceiver messageReceiver;
                                    if (currentReadFromDeadLetterQueue)
                                    {
                                        messageReceiver =
                                            messagingFactory.CreateMessageReceiver(SubscriptionClient.FormatDeadLetterPath(currentSubscription.TopicPath,
                                                                                                                           currentSubscription.Name),
                                                                                   currentReceiveMode);
                                    }
                                    else
                                    {
                                        if (currentSubscription.RequiresSession)
                                        {
                                            var subscriptionClient = messagingFactory.CreateSubscriptionClient(currentSubscription.TopicPath,
                                                                                                               currentSubscription.Name,
                                                                                                               currentReceiveMode);
                                            messageReceiver = subscriptionClient.AcceptMessageSession(TimeSpan.FromSeconds(sessionTimeout));
                                        }
                                        else
                                        {
                                            messageReceiver = messagingFactory.CreateMessageReceiver(SubscriptionClient.FormatSubscriptionPath(currentSubscription.TopicPath,
                                                                                                                                               currentSubscription.Name),
                                                                                                     currentReceiveMode);
                                        }
                                    }
                                    messageReceiver.PrefetchCount = prefetchCount;
                                    string traceMessage;
                                    if (checkBoxReceiverUseTransaction.Checked)
                                    {
                                        using (var scope = new TransactionScope())
                                        {
                                            serviceBusHelper.ReceiveMessages(messageReceiver,
                                                                             taskId,
                                                                             receiveTimeout,
                                                                             filter,
                                                                             currentMoveToDeadLetterQueue,
                                                                             checkBoxCompleteReceive.Checked,
                                                                             checkBoxDeferMessage.Checked,
                                                                             checkBoxEnableReceiverLogging.Checked,
                                                                             checkBoxReceiverVerboseLogging.Checked,
                                                                             checkBoxReceiverEnableStatistics.Checked,
                                                                             checkBoxReceiveBatch.Checked,
                                                                             receiverBatchSize,
                                                                             checkBoxReceiverThinkTime.Checked,
                                                                             receiverThinkTime,
                                                                             receiverBrokeredMessageInspector,
                                                                             UpdateStatistics,
                                                                             receiverCancellationTokenSource,
                                                                             out traceMessage);
                                            var builder = new StringBuilder(traceMessage);
                                            if (checkBoxReceiverCommitTransaction.Checked)
                                            {
                                                scope.Complete();
                                                builder.AppendLine(TransactionCommitted);
                                            }
                                            else
                                            {
                                                builder.AppendLine(TransactionAborted);
                                            }
                                            traceMessage = builder.ToString();
                                        }
                                    }
                                    else
                                    {
                                        serviceBusHelper.ReceiveMessages(messageReceiver,
                                                                         taskId,
                                                                         receiveTimeout,
                                                                         filter,
                                                                         currentMoveToDeadLetterQueue,
                                                                         checkBoxCompleteReceive.Checked,
                                                                         checkBoxDeferMessage.Checked,
                                                                         checkBoxEnableReceiverLogging.Checked,
                                                                         checkBoxReceiverVerboseLogging.Checked,
                                                                         checkBoxReceiverEnableStatistics.Checked,
                                                                         checkBoxReceiveBatch.Checked,
                                                                         receiverBatchSize,
                                                                         checkBoxReceiverThinkTime.Checked,
                                                                         receiverThinkTime,
                                                                         receiverBrokeredMessageInspector,
                                                                         UpdateStatistics,
                                                                         receiverCancellationTokenSource,
                                                                         out traceMessage);
                                    }
                                    if (!string.IsNullOrWhiteSpace(traceMessage))
                                    {
                                        writeToLog(traceMessage.Substring(0, traceMessage.Length - 1));
                                    }
                                    allSessionsAccepted = !currentSubscription.RequiresSession;
                                }
                                catch (TimeoutException ex)
                                {
                                    if (currentSubscription.RequiresSession)
                                    {
                                        writeToLog(string.Format(NoMoreSessionsToAccept, taskId));
                                        allSessionsAccepted = true;
                                    }
                                    else
                                    {
                                        HandleException(ex);
                                    }
                                }
                                catch (Exception ex)
                                {
                                    HandleException(ex);
                                }
                            }
                        };

                        // Define Receiver AsyncCallback
                        AsyncCallback receiverCallback = a =>
                        {
                            var action = a.AsyncState as Action <int>;
                            if (action != null)
                            {
                                action.EndInvoke(a);
                                if (Interlocked.Decrement(ref actionCount) == 0)
                                {
                                    managerResetEvent.Set();
                                }
                            }
                        };

                        // Start Receiver Actions
                        for (var i = 0; i < receiverTaskCount; i++)
                        {
                            receiverAction.BeginInvoke(i, receiverCallback, receiverAction);
                            Interlocked.Increment(ref actionCount);
                        }
                    }
                    catch (Exception ex)
                    {
                        HandleException(ex);
                    }
                    if (actionCount > 0)
                    {
                        managerCancellationTokenSource = new CancellationTokenSource();
                        managerAction.BeginInvoke(managerCancellationTokenSource, null, null);
                        graphCancellationTokenSource = new CancellationTokenSource();
                        updateGraphAction.BeginInvoke(updateGraphCallback, updateGraphAction);
                        Interlocked.Increment(ref actionCount);
                        btnStart.Text = StopCaption;
                    }
                }
            }
            catch (Exception ex)
            {
                HandleException(ex);
            }
            finally
            {
                btnStart.Enabled = true;
                Cursor.Current   = Cursors.Default;
            }
        }
Example #51
0
 public SubscriptionClientWrapper(string connectionString, string topicPath, string name)
 {
     _client = SubscriptionClient.CreateFromConnectionString(connectionString, topicPath, name);
 }
Example #52
0
        public async Task GetRulesTestCase()
        {
            var subscriptionClient = new SubscriptionClient(
                TestUtility.NamespaceConnectionString,
                TestConstants.NonPartitionedTopicName,
                TestConstants.SubscriptionName,
                ReceiveMode.ReceiveAndDelete);
            var sqlRuleName         = "sqlRule";
            var correlationRuleName = "correlationRule";

            try
            {
                var rules = (await subscriptionClient.GetRulesAsync()).ToList();
                Assert.Single(rules);
                var firstRule = rules[0];
                Assert.Equal(RuleDescription.DefaultRuleName, firstRule.Name);
                Assert.IsType <SqlFilter>(firstRule.Filter);
                Assert.Null(firstRule.Action);

                await subscriptionClient.AddRuleAsync(sqlRuleName, new SqlFilter("price > 10"));

                var ruleDescription = new RuleDescription(correlationRuleName)
                {
                    Filter = new CorrelationFilter
                    {
                        CorrelationId = "correlationId",
                        Label         = "label",
                        MessageId     = "messageId",
                        Properties    =
                        {
                            { "key1", "value1" }
                        },
                        ReplyTo          = "replyTo",
                        ReplyToSessionId = "replyToSessionId",
                        SessionId        = "sessionId",
                        To = "to"
                    },
                    Action = new SqlRuleAction("Set CorrelationId = 'newValue'")
                };
                await subscriptionClient.AddRuleAsync(ruleDescription);

                rules = (await subscriptionClient.GetRulesAsync()).ToList();
                Assert.Equal(3, rules.Count);

                var sqlRule = rules.FirstOrDefault(rule => rule.Name.Equals(sqlRuleName));
                Assert.NotNull(sqlRule);
                Assert.Null(sqlRule.Action);
                Assert.IsType <SqlFilter>(sqlRule.Filter);
                Assert.Equal("price > 10", ((SqlFilter)sqlRule.Filter).SqlExpression);

                var correlationRule = rules.FirstOrDefault(rule => rule.Name.Equals(correlationRuleName));
                Assert.NotNull(correlationRule);
                Assert.IsType <SqlRuleAction>(correlationRule.Action);
                var sqlRuleAction = correlationRule.Action as SqlRuleAction;
                Assert.NotNull(sqlRuleAction);
                Assert.Equal("Set CorrelationId = 'newValue'", sqlRuleAction.SqlExpression);
                Assert.IsType <CorrelationFilter>(correlationRule.Filter);
                var correlationFilter = correlationRule.Filter as CorrelationFilter;
                Assert.NotNull(correlationFilter);
                Assert.Equal("correlationId", correlationFilter.CorrelationId);
                Assert.Equal("label", correlationFilter.Label);
                Assert.Equal("messageId", correlationFilter.MessageId);
                Assert.Equal("replyTo", correlationFilter.ReplyTo);
                Assert.Equal("replyToSessionId", correlationFilter.ReplyToSessionId);
                Assert.Equal("sessionId", correlationFilter.SessionId);
                Assert.Equal("to", correlationFilter.To);
                Assert.NotNull(correlationFilter.Properties);
                Assert.Equal("value1", correlationFilter.Properties["key1"]);
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                throw;
            }
            finally
            {
                try
                {
                    await subscriptionClient.RemoveRuleAsync(sqlRuleName);

                    await subscriptionClient.RemoveRuleAsync(correlationRuleName);
                }
                catch (Exception)
                {
                    // Ignore the exception as we are just trying to clean up the rules that we MIGHT have added.
                }
                await subscriptionClient.CloseAsync();
            }
        }
Example #53
0
 public SubscriptionBatchMessageReceiver(SubscriptionClient client)
 {
     Guard.IsNotNull(client, nameof(client));
     this.client = client;
 }
Example #54
0
        public async Task SqlFilterTestCase(string topicName)
        {
            var topicClient        = new TopicClient(TestUtility.NamespaceConnectionString, topicName);
            var subscriptionClient = new SubscriptionClient(
                TestUtility.NamespaceConnectionString,
                topicName,
                this.SubscriptionName,
                ReceiveMode.ReceiveAndDelete);

            try
            {
                try
                {
                    await subscriptionClient.RemoveRuleAsync(RuleDescription.DefaultRuleName);
                }
                catch (Exception e)
                {
                    TestUtility.Log($"Remove Default Rule failed with: {e.Message}");
                }

                await subscriptionClient.AddRuleAsync(new RuleDescription
                {
                    Filter = new SqlFilter("Color = 'RedSql'"),
                    Name   = "RedSql"
                });

                var messageId1 = Guid.NewGuid().ToString();
                await topicClient.SendAsync(new Message
                {
                    MessageId      = messageId1,
                    Label          = "BlueSql",
                    UserProperties = { { "color", "BlueSql" } }
                });

                TestUtility.Log($"Sent Message: {messageId1}");

                var messageId2 = Guid.NewGuid().ToString();
                await topicClient.SendAsync(new Message
                {
                    MessageId      = messageId2,
                    Label          = "RedSql",
                    UserProperties = { { "color", "RedSql" } }
                });

                TestUtility.Log($"Sent Message: {messageId2}");

                var messages = await subscriptionClient.InnerSubscriptionClient.InnerReceiver.ReceiveAsync(maxMessageCount : 2);

                Assert.NotNull(messages);
                Assert.True(messages.Count == 1);
                Assert.Equal(messageId2, messages.First().MessageId);
            }
            finally
            {
                try
                {
                    await subscriptionClient.RemoveRuleAsync("RedSql");

                    await subscriptionClient.AddRuleAsync(RuleDescription.DefaultRuleName, new TrueFilter());
                }
                catch (Exception e)
                {
                    TestUtility.Log($" Cleanup failed with Exception: {e.Message}");
                }

                await subscriptionClient.CloseAsync();

                await topicClient.CloseAsync();
            }
        }
        protected override async Task ExecuteAsync(CancellationToken token)
        {
            while (true)
            {
                SubscriptionClient serviceBusClient = null;

                try
                {
                    var subscription = await InitializeAzureServiceBus(token).ConfigureAwait(false);

                    serviceBusClient = new SubscriptionClient(
                        connectionString: _options.ServiceBusConnectionString,
                        topicPath: _options.Topic,
                        subscriptionName: subscription.Name,
                        receiveMode: ReceiveMode.PeekLock);

                    var opts = new MessageHandlerOptions(ex =>
                    {
                        _logger.LogError(ex.Exception, "unhandled exception in message handler");
                        return(Task.FromResult(0));
                    })
                    {
                        MaxConcurrentCalls = 1,
                    };

                    serviceBusClient.RegisterMessageHandler((msg, token) =>
                    {
                        this.HandleBackplaneMessage(serviceBusClient, msg, token);
                        return(Task.FromResult(0));
                    }, opts);

                    Task newSubscriptionSignal     = _subscribeSignal.WaitAsync();
                    Task subscriptionDisposeSignal = _unsubscribeSignal.WaitAsync();

                    while (true)
                    {
                        try
                        {
                            var result = await Task.WhenAny(newSubscriptionSignal, subscriptionDisposeSignal);

                            if (result == newSubscriptionSignal)
                            {
                                newSubscriptionSignal = _subscribeSignal.WaitAsync();
                            }
                            else if (result == subscriptionDisposeSignal)
                            {
                                subscriptionDisposeSignal = _unsubscribeSignal.WaitAsync();
                            }

                            await UpdateRules(subscription, token);
                        }
                        catch (TaskCanceledException)
                        {
                            _logger.LogDebug("inner loop cancelled");
                            throw;
                        }
                        catch (Exception ex)
                        {
                            _logger.LogError(ex, "unhandled exeption in inner control loop");
                            await Task.Delay(100);
                        }
                    }
                }
                catch (TaskCanceledException)
                {
                    _logger.LogDebug("outer loop cancelled");
                }
                catch (Exception ex)
                {
                    _logger.LogError(ex, "unhandled exception in outer loop control loop");
                }

                _logger.LogDebug("closing backplane connection");
                Task close = serviceBusClient?.CloseAsync();
                if (close != null)
                {
                    await close;
                }

                _logger.LogDebug("something happend in the loop, retrying after a short delay");
                await Task.Delay(5000);
            }
        }
        private Task MessageHandler(Message message, CancellationToken cancellationToken1, SubscriptionClient receiver)
        {
            _messenger.Publish(new NotificationMessage(this, message));

            CompleteMessage();

            return(Task.CompletedTask);

            void CompleteMessage()
            {
                try
                {
                    receiver.CompleteAsync(message.SystemProperties.LockToken);
                }
                catch (Exception ex)
                {
                    Debug.WriteLine(ex);
                }
            }
        }
 /// <summary>
 /// Creates an instance of Azure Service Bus receiver with connection
 /// </summary>
 public AzureServiceBusTopicReceiver(string connectionString, string topicName, string subscriptionName, bool peekLock = true)
 {
     _client   = new SubscriptionClient(connectionString, topicName, subscriptionName, peekLock ? ReceiveMode.PeekLock : ReceiveMode.ReceiveAndDelete);
     _peekLock = peekLock;
 }
Example #58
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),
                                    ExtractSignature(message),
                                    SigningTokenConfigurationKey,
                                    () =>
                                {
                                    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),
                                    ExtractSignature(message),
                                    SigningTokenConfigurationKey,
                                    () =>
                                {
                                    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);
        }
        public async Task <SubscriptionDescription> Create(string topicPath, string subscriptionName, SubscriptionMetadataInternal metadata, string sqlFilter, INamespaceManagerInternal namespaceManager, string forwardTo)
        {
            var meta = metadata as ForwardingTopologySubscriptionMetadata;

            if (meta == null)
            {
                throw new InvalidOperationException($"Cannot create subscription `{subscriptionName}` for topic `{topicPath}` without namespace information required.");
            }

            var subscriptionDescription = new SubscriptionDescription(topicPath, subscriptionName)
            {
                EnableBatchedOperations  = subscriptionSettings.EnableBatchedOperations,
                AutoDeleteOnIdle         = subscriptionSettings.AutoDeleteOnIdle,
                DefaultMessageTimeToLive = subscriptionSettings.DefaultMessageTimeToLive,
                EnableDeadLetteringOnFilterEvaluationExceptions = subscriptionSettings.EnableDeadLetteringOnFilterEvaluationExceptions,
                EnableDeadLetteringOnMessageExpiration          = subscriptionSettings.EnableDeadLetteringOnMessageExpiration,
                ForwardDeadLetteredMessagesTo = subscriptionSettings.ForwardDeadLetteredMessagesTo,
                LockDuration     = subscriptionSettings.LockDuration,
                MaxDeliveryCount = DefaultMaxDeliveryCountForNoImmediateRetries
            };

            subscriptionSettings.DescriptionCustomizer(subscriptionDescription);

            subscriptionDescription.ForwardTo    = forwardTo;
            subscriptionDescription.UserMetadata = metadata.Description;

            try
            {
                var exists = await ExistsAsync(topicPath, subscriptionName, metadata.Description, namespaceManager).ConfigureAwait(false);

                if (!exists)
                {
                    var ruleDescription = new RuleDescription
                    {
                        Filter = new SqlFilter(sqlFilter),
                        Name   = metadata.SubscriptionNameBasedOnEventWithNamespace
                    };

                    try
                    {
                        await namespaceManager.CreateSubscription(subscriptionDescription, ruleDescription).ConfigureAwait(false);

                        logger.Info($"Subscription '{subscriptionDescription.UserMetadata}' created as '{subscriptionDescription.Name}' with rule '{ruleDescription.Name}' for event '{meta.SubscribedEventFullName}' in namespace '{namespaceManager.Address.Host}'.");

                        var key = GenerateSubscriptionKey(namespaceManager.Address, subscriptionDescription.TopicPath, subscriptionDescription.Name);
                        await rememberExistence.AddOrUpdate(key, keyNotFound => TaskEx.CompletedTrue, (updateTopicPath, previousValue) => TaskEx.CompletedTrue).ConfigureAwait(false);
                    }
                    catch (MessagingEntityAlreadyExistsException)
                    {
                        // the subscription already exists or another node beat us to it, which is ok
                        logger.Info($"Subscription '{subscriptionDescription.Name}' in namespace '{namespaceManager.Address.Host}' already exists, another node probably beat us to it.");
                        exists = true;
                    }
                }

                if (exists)
                {
                    logger.Info($"Subscription '{subscriptionDescription.Name}' aka '{subscriptionDescription.UserMetadata}' already exists, skipping creation.");
                    logger.InfoFormat("Checking if subscription '{0}' in namespace '{1}' needs to be updated.", subscriptionDescription.Name, namespaceManager.Address.Host);

                    var existingSubscriptionDescription = await namespaceManager.GetSubscription(subscriptionDescription.TopicPath, subscriptionDescription.Name).ConfigureAwait(false);

                    if (MembersAreNotEqual(existingSubscriptionDescription, subscriptionDescription))
                    {
                        logger.Info($"Updating subscription '{subscriptionDescription.Name}' in namespace '{namespaceManager.Address.Host}' with new description.");
                        await namespaceManager.UpdateSubscription(subscriptionDescription).ConfigureAwait(false);
                    }

                    // Rules can't be queried, so try to add
                    var ruleDescription = new RuleDescription
                    {
                        Filter = new SqlFilter(sqlFilter),
                        Name   = metadata.SubscriptionNameBasedOnEventWithNamespace
                    };
                    logger.Info($"Adding subscription rule '{ruleDescription.Name}' for event '{meta.SubscribedEventFullName}' in namespace '{namespaceManager.Address.Host}'.");
                    try
                    {
                        var subscriptionClient = SubscriptionClient.CreateFromConnectionString(meta.NamespaceInfo.ConnectionString, topicPath, subscriptionName);
                        await subscriptionClient.AddRuleAsync(ruleDescription).ConfigureAwait(false);
                    }
                    catch (MessagingEntityAlreadyExistsException exception)
                    {
                        logger.Debug($"Rule '{ruleDescription.Name}' already exists. Response from the server: '{exception.Message}'.");
                    }
                }
            }
            catch (TimeoutException)
            {
                logger.Info($"Timeout occurred on subscription creation for topic '{subscriptionDescription.TopicPath}' subscription name '{subscriptionDescription.Name}' in namespace '{namespaceManager.Address.Host}' going to validate if it doesn't exist.");

                // there is a chance that the timeout occurred, but the topic was still created, check again
                if (!await ExistsAsync(subscriptionDescription.TopicPath, subscriptionDescription.Name, metadata.Description, namespaceManager, removeCacheEntry: true).ConfigureAwait(false))
                {
                    throw;
                }

                logger.Info($"Looks like subscription '{subscriptionDescription.Name}' in namespace '{namespaceManager.Address.Host}' exists anyway.");
            }
            catch (MessagingException ex)
            {
                var loggedMessage = $"{(ex.IsTransient ? "Transient" : "Non transient")} {ex.GetType().Name} occurred on subscription '{subscriptionDescription.Name}' creation for topic '{subscriptionDescription.TopicPath}' in namespace '{namespaceManager.Address.Host}'.";

                if (!ex.IsTransient)
                {
                    logger.Fatal(loggedMessage, ex);
                    throw;
                }

                logger.Info(loggedMessage, ex);
            }

            return(subscriptionDescription);
        }
Example #60
0
        private void RunAsync(CancellationToken cancellationToken)
        {
            try
            {
                l("Setting up AdminTopic subscription");

                TopicDescription td = new TopicDescription("AdminTopic");
                td.MaxSizeInMegabytes       = 5120;
                td.DefaultMessageTimeToLive = new TimeSpan(0, 1, 0);

                var namespaceManager = NamespaceManager.CreateFromConnectionString(ConfigurationManager.AppSettings["Microsoft.ServiceBus.ConnectionString"]);
                if (!namespaceManager.TopicExists("AdminTopic"))
                {
                    namespaceManager.CreateTopic(td);
                }

                // change the subscription to instanceID!
                if (!namespaceManager.SubscriptionExists("AdminTopic", gvars.iid))
                {
                    namespaceManager.CreateSubscription("AdminTopic", gvars.iid);
                }

                // change the subscription to instanceID!
                SubscriptionClient Client  = SubscriptionClient.CreateFromConnectionString(ConfigurationManager.AppSettings["Microsoft.ServiceBus.ConnectionString"], "AdminTopic", gvars.iid);
                OnMessageOptions   options = new OnMessageOptions();
                options.AutoComplete     = false;
                options.AutoRenewTimeout = TimeSpan.FromMinutes(1);

                Client.OnMessage((message) =>
                {
                    try
                    {
                        // Process message from subscription.
                        string body = message.GetBody <string>();
                        if (body == "suspendBridge")
                        {
                            l("received: suspendBridge = true");
                        }
                        else if (body == "resumeBridge")
                        {
                            l("received: suspendBridge = false");
                        }
                        else
                        {
                            el("AdminTopic message unhandled: " + body);
                        }

                        // Remove message from subscription.
                        message.Complete();
                    }
                    catch (Exception)
                    {
                        // Indicates a problem, unlock message in subscription.
                        message.Abandon();
                    }
                }, options);

                g("Subscripted to AdminTopic");

                while (!cancellationToken.IsCancellationRequested)
                {
                    l("Setting up service host");

                    ServiceBusEnvironment.SystemConnectivity.Mode = ConnectivityMode.AutoDetect;

                    TransportClientEndpointBehavior sasCredential = new TransportClientEndpointBehavior();
                    sasCredential.TokenProvider = TokenProvider.CreateSharedAccessSignatureTokenProvider("RootManageSharedAccessKey", ConfigurationManager.AppSettings["sasKey"]);

                    Uri address = ServiceBusEnvironment.CreateServiceUri("sb", ConfigurationManager.AppSettings["serviceNamespace"], "BridgeService");

                    ServiceHost host = new ServiceHost(typeof(BridgeService), address);

                    IEndpointBehavior serviceRegistrySettings = new ServiceRegistrySettings(DiscoveryType.Private);

                    foreach (ServiceEndpoint endpoint in host.Description.Endpoints)
                    {
                        endpoint.Behaviors.Add(serviceRegistrySettings);
                        endpoint.Behaviors.Add(sasCredential);
                    }

                    host.Open();

                    g("Running service host");
                    while (!cancellationToken.IsCancellationRequested && !suspendBridge)
                    {
                        Thread.Sleep(1000);
                    }

                    host.Close();

                    if (suspendBridge)
                    {
                        el("BridgeService suspended");

                        while (suspendBridge && !cancellationToken.IsCancellationRequested)
                        {
                            Thread.Sleep(1000);
                        }

                        if (!suspendBridge)
                        {
                            l("BridgeService resuming");
                        }
                    }
                }

                el("BusRole stopped - cancellation requested");
            }
            catch (Exception ex1)
            {
                ex(System.Reflection.MethodBase.GetCurrentMethod().Name + " " + ex1.ToString());
            }
        }