我有一个桌面应用程序,该应用程序调用asp.net mcv控制器的操作并获取响应。在大多数情况下,它都能正常工作,但有时桌面应用程序会收到异常“无法解析远程名称:URL”
这是桌面应用程序中发出http发布请求的功能:
> private bool postRequest(string uri, string json, out string jsonResult)
> {
> try
> {
> if (CheckForInternetConnection())
> {
> var httpWebRequest = (HttpWebRequest)WebRequest.Create(uri);
> httpWebRequest.ContentType = "application/json";
> httpWebRequest.Method = "POST";
> httpWebRequest.Timeout = 300000;
> //ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 |
> SecurityProtocolType.Tls;
> using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
> {
> //string json = JsonConvert.SerializeObject();
> //string json = new JavaScriptSerializer().Serialize(new
> //{
> // Username = "myusername",
> // Password = "pass"
> //});
> streamWriter.Write(json);
> streamWriter.Flush();
> streamWriter.Close();
> }
>
> var res = httpWebRequest.GetResponse();
> var httpResponse = (HttpWebResponse)res;
> //message = httpResponse.ToString();
> //jsonResult = httpResponse.StatusCode.ToString();
> //message = httpResponse.StatusDescription;
> //return true;
> using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
> {
> var result = streamReader.ReadToEnd();
> jsonResult = result.ToString();
> return true;
> }
> }
> else
> {
> throw new Exception("Problem in internet connection.");
> }
> }
> catch (Exception ex)
> {
> //using (StreamWriter w = File.AppendText(@"C:\Users\Shubham\Desktop\log.txt"))
> //{
> // Log(ex.Message, w);
> //}
> throw ex;
> }
>
> }
以下是asp.net mcv中的动作之一,该动作由桌面应用程序调用:
> [HttpPost]
> public ActionResult getSku(EmployeeDetails emp)
> {
> try
> {
> if (helper.getSku(emp, out SkuDetails skuDt))
> {
> JsonCreateResult json = new JsonCreateResult()
> {
> ex = null,
> id = skuDt.SemId,
> Message = JsonConvert.SerializeObject(skuDt),
> statusCode = HttpStatusCode.Found
> };
> return Json(json);
> }
> else
> {
> var key = dictionary.GetOrAdd(emp.SubscriberId+emp.EmployeeRole, new object());
> bool flag;
> SkuDetails skuDetails;
> lock (key)
> {
> flag = helper.getSkuFromSm(emp, out skuDetails);
> }
> if (flag)
> {
> JsonCreateResult json = new JsonCreateResult()
> {
> ex = null,
> id = skuDetails.SemId,
> Message = JsonConvert.SerializeObject(skuDetails),
> statusCode = HttpStatusCode.Found
> };
> return Json(json);
> }
> else
> {
> JsonCreateResult json = new JsonCreateResult()
> {
> ex = null,
> id = string.Empty,
> Message = string.Empty,
> statusCode = HttpStatusCode.NotFound
> };
> return Json(json, JsonRequestBehavior.AllowGet);
> }
> }
> }
> catch (Exception ex)
> {
> helper.getErrorInBuild(emp.SubscriberId, "getSku", ex.Message, emp.EmployeeId + emp.EmployeeRole, string.Empty);
> JsonCreateResult json = new JsonCreateResult()
> {
> ex = ex,
> id = string.Empty,
> Message = string.Empty,
> statusCode = HttpStatusCode.InternalServerError
> };
> return Json(json, JsonRequestBehavior.AllowGet);
> }
> }
我在asp.net mvc控制器中使用lock()和static ConcurrentDictionary,如您在上述Action中所见。我不知道这是否是问题的原因,就像静态变量存储在数据上然后是限制一样。 该桌面应用程序在多个桌面上运行,每个桌面并行生成请求。 如果有人知道此异常及其解决方案,请帮助我