gpt4 book ai didi

c# - HttpClient.SendAsync 停滞不前,即使发送的数据已处理

转载 作者:太空宇宙 更新时间:2023-11-03 19:54:27 25 4
gpt4 key购买 nike

我必须将应用程序从 ASP.Net Core 重写为 ASP.Net 4.6.1 并且在我发送的应用程序的核心版本中 Json 数据到 BugZilla 进行处理,它工作正常。它发送数据并相当快地得到响应。应用程序 4.6.1 版本中的相同代码位于并卡在发送命令上。我可以进入 BugZilla 并看到它获取了数据并生成了错误,但即使在 15 分钟后应用程序也没有进展。我是 Task 和通过 Json 发送数据的新手,所以我不确定为什么会遇到这个问题。不会返回任何错误,在 Debug模式下,我可以单步执行所有操作,并且知道它正在处理并等待 SendAsync 行。我做错了什么导致 4.6.1 中出现这种情况?

private static async Task<int> LogToBugZilla(string Component, Exception ex)
{
var ErrorSummary = "Auto Error Logging: " + ex.Message + " " + ex.InnerException.Message;

var BugData = new Dictionary<string, string>
{
{ "Bugzilla_api_key", ConfigurationManager.AppSettings["BugZillaAPIKey"] },
{ "product", "Job Site" },
{ "component", Component },
{ "version", ConfigurationManager.AppSettings["AppVersion"] },
{ "summary", ErrorSummary },
{ "op_sys", "All" },
{ "platform", "Other" },
{ "description", ex.StackTrace + ex.InnerException.StackTrace }
};

string Json = JsonConvert.SerializeObject(BugData, Formatting.None);
var Client = new HttpClient();
var Request = new HttpRequestMessage(HttpMethod.Post, "http://bugzilla/rest/bug");
Request.Content = new StringContent(Json, Encoding.UTF8, "application/json");
Request.Headers.Add("Accept", "application/json");
var Response = await Client.SendAsync(Request);
var JsonResults = await Response.Content.ReadAsStringAsync();

if (Response.StatusCode == HttpStatusCode.OK)
{
var Results = JsonConvert.DeserializeObject<Dictionary<string, int>>(JsonResults);
return Results["id"];
}
else
{
return -1;
}
}

编辑:

public static string ProcessError(Exception ex, string OriginatingController)
{
var Component = GetComponent(OriginatingController);
string UserErrorMessage = "";

switch (ex.GetType().ToString())
{
case "System.Data.DataException":
LogToITSupport("Database Connection");
UserErrorMessage = @"An error has accrued connecting to the database. <a href=""mailto:"">IT Support</a> has been notified of this error. Please try your request again later, thank you.";
break;

default:
var BugID = LogToBugZilla(Component, ex).Result;
UserErrorMessage = @"An error has accrued processing your request and a log of this error has been made on the <a href=""http://bugzilla/show_bug.cgi?id=" + BugID + "\">Internal Tools BugZilla system</a> with an ID of " + BugID + ". Please try your request again later, thank you.";
break;
}

return UserErrorMessage;
}

重申一下,应用程序卡在代码 var Response = await Client.SendAsync(Request);

编辑 2:

Controller 代码:

public ActionResult About()
{
try
{
using (var db = new JobSightDbContext())
{
var model = db.ReleaseVersions
.OrderByDescending(rv => rv.DateReleased)
.Select(rv => new ReleaseNotesVM()
{
ID = rv.ID,
CurrentVersion = string.Concat(rv.Major, ".", rv.Minor, ".", rv.Patch),
CurrentVersionReleaseDate = rv.DateReleased,
ReleaseNotes = rv.ReleaseNotes.Select(rn => rn.Note).ToList()
}).First();

//model.VersionList = db.ReleaseVersions
// .OrderByDescending(rv => rv.DateReleased)
// .Select(rv => new SelectListItem()
// {
// Value = rv.ID.ToString(),
// Text = string.Concat(rv.Major, '.', rv.Minor, '.', rv.Patch)
// }).ToList();

return View(model);
}
}
catch (System.Data.DataException ex)
{
ViewBag.ErrorMessage = EHALM.ProcessError(ex, RouteData.Values["controller"].ToString());
return View("Dashboard");
}
}

最佳答案

我要在这里跛行并说你使用 LogToBugZilla在你的调用堆栈中像这样:

public void DoStuff()
{
var result = LogToBugZilla("Component", exception).Result;
}

这实际上会使您的代码陷入僵局,这也是 you shouldn't block on async code 的原因。 .你需要让你的代码“一直异步”,直到你的调用堆栈的顶部。这意味着转 DoStuffDoStuffAsync并这样调用它:

public async Task DoStuffAsync()
{
var result = await LogToBugZillaAsync("Component", exception);
// Do stuff with result
}

请注意,我在 LogToBugZilla 中添加了“async”后缀,按照推荐的编码约定。

编辑:

鉴于您提供了调用方法,它应该如下所示:

public static async Task<string> ProcessErrorAsync(
Exception ex, string OriginatingController)
{
var Component = GetComponent(OriginatingController);
string UserErrorMessage = "";

switch (ex.GetType().ToString())
{
case "System.Data.DataException":
LogToITSupport("Database Connection");
UserErrorMessage = @"An error has accrued connecting to the
database. <a href=""mailto:"">IT Support</a> has been notified
of this error. Please try your request again later, thank you.";
break;

default:
var bugId = await LogToBugZillaAsync(Component, ex);
UserErrorMessage = @"An error has accrued processing your request
and a log of this error has been made
on the <a href=""http://bugzilla/show_bug.cgi?id=" + BugID +
"\">Internal Tools BugZilla system</a>
with an ID of " + bugId + ". Please try your request again later, thank you.";
break;
}
return UserErrorMessage;
}

现在请记住,这需要一直向上。这意味着每个方法调用 ProcessErrorAsync , 本身应该是async并返回 TaskTask<T> .

关于c# - HttpClient.SendAsync 停滞不前,即使发送的数据已处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35364992/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com