コード例 #1
0
        private async Task SendPostAsync(string formatedUrl, WebHookMessage message)
        {
            // TODO: Use HttpClientFactory
#warning use HTTP client factory
            using (HttpClient client = new HttpClient())
            {
                string convertedjson = System.Text.Json.JsonSerializer.Serialize(message);
                using (StringContent content = new StringContent(convertedjson, Encoding.UTF8, "application/json"))
                {
                    using (HttpResponseMessage response = await client.PostAsync(formatedUrl, content).ConfigureAwait(false))
                    {
                        string dataString = await response.Content.ReadAsStringAsync().ConfigureAwait(false);

                        if (response.IsSuccessStatusCode)
                        {
                            this.logger.LogTrace("Succes web hook on {0} for queuId={1} and messageId={2} with content: {3}", formatedUrl, message.QueuId, message.MessageId, dataString);
                            this.logger.LogInformation("Succes web hook on {0} for queuId={1} and messageId={2}", formatedUrl, message.QueuId, message.MessageId);
                        }
                        else
                        {
                            this.logger.LogWarning("Failed web hook on {0} with status {1} for queuId={2} and messageId={3} with content: {4}",
                                                   formatedUrl,
                                                   message.QueuId,
                                                   message.MessageId,
                                                   response.StatusCode,
                                                   dataString);
                        }
                    }
                }
            }
        }
コード例 #2
0
        public async Task SendNotificationAsync(Guid queuId, Guid messageId)
        {
            if (queuId == Guid.Empty)
            {
                throw new ArgumentException("Parameter queuId is empty.");
            }
            if (messageId == Guid.Empty)
            {
                throw new ArgumentException("Parameter messageId is empty.");
            }

            if (!this.memoryCache.TryGetValue($"WebHookNotification-{queuId}", out string url))
            {
                url = await this.ReadAdress(queuId).ConfigureAwait(false);

                this.memoryCache.Set($"WebHookNotification-{queuId}", url);
            }

            if (url != null)
            {
                this.logger.LogTrace("Prepare webhook on {0} for queuId={1} and messageId={2}.", url, queuId, messageId);

                string         formatedUrl = url.Replace("$(queuId)", queuId.ToString(), StringComparison.Ordinal).Replace("${messageId}", messageId.ToString(), StringComparison.Ordinal);
                WebHookMessage message     = new WebHookMessage()
                {
                    MessageId = messageId,
                    QueuId    = queuId,
                    Time      = this.timeAccessor.UtcNow
                };

                this.SuperssAwait(this.SendPostAsync(formatedUrl, message).ContinueWith(this.ContinueWebHook, formatedUrl));
            }
        }