Пример #1
0
        public Task Open(string json)
        {
            _logger.LogTrace("Open called on TelemetryHub");
            var subscription = JsonConvert.DeserializeObject <SubscriptionInfo>(json);

            if (_users.ContainsKey(Context.ConnectionId))
            {
                var actualUser = _users[Context.ConnectionId];

                int maxConcurrentCalls = 5;

                _logger.LogInformation("Subscription request received: {0}", JsonConvert.SerializeObject(subscription));

                _messageProcessor.CreateServiceBusConnectionString(
                    subscription.CreationResponse.ConnectionUrl,
                    subscription.SubscriptionTopic,
                    subscription.CreationResponse.SasToken,
                    maxConcurrentCalls
                    );

                var subscriptionId = Guid.NewGuid().ToString();

                _messageProcessor.RegisterOnMessageHandlerAndReceiveMessages(_context, subscription.CreationResponse.SubscriptionId, Context.ConnectionId);

                _connectionHandler.RegisterMessageProcessor(subscriptionId, _messageProcessor);

                UserHubSubscription userHubSubscription = new UserHubSubscription
                {
                    SubscriptionId         = subscription.CreationResponse.SubscriptionId,
                    SubscriptionType       = subscription.SubscriptionTopic,
                    MessageProcessorId     = subscriptionId,
                    DataAccessUrl          = subscription.DataAccessApiUrl,
                    OcpApimSubscriptionKey = subscription.OcpApimSubscriptionKey,
                    Token = subscription.Token
                };

                actualUser.UserHubSubscription.Add(userHubSubscription);

                _users.AddOrUpdate(Context.ConnectionId, actualUser, (key, oldValue) =>
                {
                    return(actualUser);
                });
            }
            var args = new object[] { subscription.CreationResponse.SubscriptionId };

            return(Clients.Caller.SendCoreAsync("opened", args));
        }
Пример #2
0
        private async Task RemoveSubscription(UserHubSubscription userHubSubscription)
        {
            _logger.LogInformation("Remove from DA: {0}", userHubSubscription.DataAccessUrl);
            try
            {
                HttpClient httpClient = new HttpClient();

                string path = $"{userHubSubscription.DataAccessUrl}/Subscriptions?subscriptionId={userHubSubscription.SubscriptionId}&messageType={userHubSubscription.SubscriptionType}";

                httpClient.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", userHubSubscription.OcpApimSubscriptionKey);
                httpClient.DefaultRequestHeaders.Add("Authorization", "Bearer " + userHubSubscription.Token);

                HttpResponseMessage result = await httpClient.DeleteAsync(path);

                this._logger.LogDebug("Remove from DA result: {0}", JsonConvert.SerializeObject(result));
            }
            catch (Exception e)
            {
                this._logger.LogError("Cannot remove subscription", e);
            }
        }