由于异步调用,Azure .NET Core Appservice锁定发送502.3 [英] Azure .NET Core Appservice locks up sending 502.3 because of async calls

查看:100
本文介绍了由于异步调用,Azure .NET Core Appservice锁定发送502.3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我让此方法在Azure应用服务中运行的.NET Core 2.X应用中运行。我有一个远程服务器,我们使用此方法从Angular网站中的按钮按下进行调用。

I have this method running in a .NET Core 2.X app running in Azure app service. I have a remote server that we use this method to call from button presses in our Angular website. that calls a remote device.

角按钮-> Azure中的.NET Core应用程序服务->另一个应用程序服务-> Internet\cell连接的设备。我们等待设备的响应返回状态码。

Angular button --> .NET Core app service in Azure --> another app service --> internet\cell connected device. We wait for the response from the device to return a status code.

如果我快速向该方法发送命令[2或3秒],则会导致应用程序服务在我重新启动之前停止响应。我阅读了此帖子,并添加了 [,HttpCompletionOption.ResponseHeadersRead).ConfigureAwait(false)]

If I quickly send commands [2 or 3 in a second] to this method it causes the app service to stop responding until I restart it. I read this post and added the [, HttpCompletionOption.ResponseHeadersRead).ConfigureAwait(false)].

但是我仍然可以冻结整个应用程序,并需要从快速发送命令到此方法重新启动。

However I can still freeze the entire app and require a restart from quickly sending commands to this method.

private async void SetEndPointValueAsync(string stunnelUrl, string username, string password)
{
            try
            {
                //set the user name and password
                var httpClientHandler = new HttpClientHandler()
                {
                    Credentials = new NetworkCredential(username, password)
                };
                using (var client = new HttpClient(httpClientHandler))
                {
                    using (var response = await client.GetAsync(stunnelUrl**, HttpCompletionOption.ResponseHeadersRead).ConfigureAwait(false)**)
                    {
                        if (response.IsSuccessStatusCode)
                        {
                            LogInfo(typeof(IntegrationService), stunnelUrl, LogAction.EndpointUpdate);
                        }
                        else
                        {
                            //request failed.
                            LogWarning(typeof(IntegrationService), stunnelUrl, LogAction.DeviceRequest);
                        }
                        //using (var content = response.Content)
                        //{
                        //    //do here your changes when required
                        //}
                    }
                }
            }
            catch (Exception e)
            {
                LogErrorDetailed(e, typeof(IntegrationService), stunnelUrl, LogAction.DeviceRequest);
            }
}


推荐答案

通常,您不想创建这么多 HttpClient 实例,因为您失去了它提供的管理的很多好处。

Generally, you don't want to create so many instances of HttpClient as you lose a lot of the benefits of the management it provides.

您可以通过仅拥有一个实例来减少一些开销,例如...

You could reduce some overhead by only having a single instance of it, like so...

private readonly HttpClient _client;

public ClassConstructor(HttpClient client)
{
    _client = client ?? new HttpClient();       
}

然后您可以更改方法,使其看起来像这样……

Then you could change your method to look something like this...

private async Task SetEndPointValueAsync(Uri stunnelUri, string username, string password)
{
    if (stunnelUri == null) throw new ArgumentNullException(nameof(stunnelUri));
    if (String.IsNullOrEmpty(username)) throw new ArgumentNullException(nameof(username));
    if (String.IsNullOrEmpty(password)) throw new ArgumentNullException(nameof(password));

    byte[] byteArray = Encoding.ASCII.GetBytes($"{username}:{password}");
    string scheme = "Basic";
    string parameter = Convert.ToBase64String(byteArray);

    HttpRequestMessage request = new HttpRequestMessage();
    request.Method = HttpMethod.Post;
    request.RequestUri = stunnelUri;
    request.Headers.Authorization = new AuthenticationHeaderValue(scheme, parameter);
    try
    {
        HttpResponseMessage response = await _client.SendAsync(request);

        // This will throw an HttpRequestException if there is a failure above
        response.EnsureSuccessStatusCode();

        // do your stuff here...
        // { ... }

    }
    catch (HttpRequestException)
    {
        // { log your connection / bad request issue. }
    }
    catch (Exception) 
    {
        // I don't recommend this, but since you had it...
        // { handle all other exceptions }
    }
}

这篇关于由于异步调用,Azure .NET Core Appservice锁定发送502.3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆