gpt4 book ai didi

c# - HttpResponseMessage 没有响应

转载 作者:行者123 更新时间:2023-11-30 20:27:15 29 4
gpt4 key购买 nike

我有如下服务的 MVC 项目:

namespace comp.Services
{
public class CompService
{
public HttpClient client = new HttpClient();

public CompService()
{
client.BaseAddress = new Uri("someapiurl");
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(
new MediaTypeWithQualityHeaderValue("application/json"));
}

protected async Task<string> GetProductAsync(string path)
{
var resp = "nothing here";
HttpResponseMessage response = await client.GetAsync(path);
if (response.IsSuccessStatusCode)
{
resp = await response.Content.ReadAsStringAsync();
}
return resp;
}

public string GetProduct(string path)
{
return GetProductAsync(path).GetAwaiter().GetResult();
}
}
}

和要查看的actionResult:

namespace comp.Controllers
{
public class HomeController : Controller
{
public CompService compService;
public HomeController()
{
compService = new CompService();
}

public ActionResult About()
{
var timeServer = compService.GetProduct("/api/time");
ViewBag.timeServer = timeServer;
return View();
}
}
}

在调试器中我遇到这一行:

HttpResponseMessage response = await client.GetAsync(path);

程序退出调试器,浏览器无响应。

在控制台应用程序中编写的相同代码可以工作。

在 VS 输出是响应成功的消息:

Application Insights Telemetry (unconfigured): {"name":"Microsoft.ApplicationInsights.Dev.RemoteDependency","time":"2018-02-18T13:48:31","tags":{"ai.internal.sdkVersion":"rddf:2.2.0-738","ai.internal.nodeName":"DESKTOP-xxxxx","ai.cloud.roleInstance":"DESKTOP-xxxxx"},"data":{"baseType":"RemoteDependencyData","baseData":{"ver":2,"name":"/api/v1/time","id":"xxxxx=","data":"https://api.xxx.com/api/v1/time","duration":"00:00:01.3150000","resultCode":"200","success":true,"type":"Http","target":"xxx","properties":{"DeveloperMode":"true"}}}}

在浏览器控制台输出:

[14:51:33 GMT+0100 (Central European Standard Time)] Browser Link: Failed to send message to browser link server:
Error: SignalR: Connection must be started before data can be sent. Call .start() before .send()

感谢您的帮助。

最佳答案

你真的应该一直保持代码异步,而不是试图混契约(Contract)步和异步代码

namespace comp.Services {
public class CompService {
static HttpClient client = new HttpClient();

static CompService() {
client.BaseAddress = new Uri("someapiurl");
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(
new MediaTypeWithQualityHeaderValue("application/json"));
}

public async Task<string> GetProductAsync(string path) {
var resp = string.Empty;
using(var response = await client.GetAsync(path)) {
if (response.IsSuccessStatusCode) {
resp = await response.Content.ReadAsStringAsync();
}
}
return resp;
}
}
}

Controller Action 也应该是异步的

namespace comp.Controllers {
public class HomeController : Controller {
private CompService compService;

public HomeController() {
compService = new CompService();
}

public async Task<ActionResult> About() {
var timeServer = await compService.GetProductAsync("api/time");
ViewBag.timeServer = timeServer;
return View();
}
}
}

也就是说,服务也应该被抽象

public interface ICompService {
Task<string> GetProductAsync(string path)
}

public class CompService : ICompService {
//...code removed for brevity
}

并注入(inject) Controller 而不是手动创建它。

public class HomeController : Controller {
private ICompService compService;

public HomeController(ICompService compService) {
this.compService = compService;
}

public async Task<ActionResult> About() {
var timeServer = await compService.GetProductAsync("api/time");
ViewBag.timeServer = timeServer;
return View();
}
}

引用 Async/Await - Best Practices in Asynchronous Programming

关于c# - HttpResponseMessage 没有响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48852453/

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