由于WebRequestHandler,Web应用程序在部署到azure后崩溃

时间:2017-08-25 19:58:01

标签: c# azure webrequest bad-gateway

我有一个从其他服务获取数据的API。以下代码适用于本地IIS:

if (model.Phone[0] != '+')
        {
            model.Phone = "+" + model.Phone;
        }

        string data = Newtonsoft.Json.JsonConvert.SerializeObject(new { ID = Guid.NewGuid(), Phone = model.Phone});

        if (!string.IsNullOrEmpty(model.Email))
        {
            data = Newtonsoft.Json.JsonConvert.SerializeObject(new { ID = Guid.NewGuid(), Phone = model.Phone, Email = model.Email });
        }

        logger.Debug("GetDataFromSupplier before try");

        try
        {
            WebRequestHandler handler = new WebRequestHandler();
            X509Certificate2 certificate = GetMyX509Certificate();
            ServicePointManager.ServerCertificateValidationCallback += (sender, cert, chain, sslPolicyErrors) => true;
            handler.ClientCertificates.Add(certificate);
            HttpClient client = new HttpClient(handler);
            var content = new StringContent(data, Encoding.UTF8, "application/json");
            var request = client.PostAsync(API_URL, content);
            var result = request.Result;
            return result;
        } catch (Exception ex)
        {
            logger.Debug($"GetDataFromSupplier in catch: {ex.Message}");
            var message = new HttpResponseMessage(HttpStatusCode.BadRequest);
            message.Content = new StringContent(ex.Message);
            return message;
        }

但是在我将此代码部署到Azure之后,会话中断了这行代码:

WebRequestHandler handler = new WebRequestHandler();

客户端应用程序获取502 HTTP错误。我试图增加网络服务器的RAM。捕获块甚至没有运行。什么可能导致这个问题?

1 个答案:

答案 0 :(得分:0)

查看WebRequestHandler https://msdn.microsoft.com/en-us/library/system.net.http.webrequesthandler(v=vs.110).aspx的文档,它可能不是此处使用的最佳组件。

也许,尝试使用HttpClient。 https://msdn.microsoft.com/en-us/library/system.net.http.httpclient(v=vs.110).aspx因为,WebRequestHandler更适用于桌面应用程序。

由于您提到了IIS,我猜您没有构建桌面应用程序。