I have an API that fetches data from other service. The following code works great on local 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;
}
But after i have deployed this code to Azure the session breaks on this line of code:
WebRequestHandler handler = new WebRequestHandler();
The client application gets the 502 HTTP error. I have tried to increase the the RAM of webserver. The catch block not even run. What can cause this problem?
Looking at the documentation for WebRequestHandler https://msdn.microsoft.com/en-us/library/system.net.http.webrequesthandler(v=vs.110).aspx , it may not be the best component to use here.
Perhaps, try the same with HttpClient. https://msdn.microsoft.com/en-us/library/system.net.http.httpclient(v=vs.110).aspx because, WebRequestHandler is more for desktop applications.
Since you mentioned IIS, I am guessing you are not building a desktop app.