c# - 在 ASMX WebService 中使用 HttpClient
问题描述
在我的 asmx Web 服务中,我必须从另一个 Web 服务调用方法。因为 asmx webService 不处理 async/await,所以我的代码如下所示:
public string switchRelay(int TermId)
{
string result = "-1";
string request = "...";
client.DefaultRequestHeaders.Add("SOAPAction", "...");
var content = new StringContent(request, Encoding.UTF8, "text/xml");
using (var response = client.PostAsync(ConfigurationManager.AppSettings["Url"], content))
{
response.Wait();
var soapResponse = response.Result.Content.ReadAsStringAsync();
soapResponse.Wait();
var soap = XDocument.Parse(soapResponse.Result, LoadOptions.PreserveWhitespace);
soapResponse.Dispose();
result = soap.Descendants().ToList()[3].Value;
}
return result;
}
我的问题是,我应该在哪里创建 HttpClient 的实例。现在我将它创建为全局服务变量,但这会随着每个请求运行,所以它每次都会创建新的 httpClient(我试图将它用作单例,但它看起来在 asmx WebService 中是不可能的)。可以在 WebMethod 中创建新的 HttpClient 实例吗?
谢谢任何建议。
解决方案
推荐阅读
- java - 从 Listener 修改 UI
- autohotkey - 我的 AutoHotKey 脚本运行时无法键入空格?
- python - ValueError:尝试在具有祖父目录的顶级包之外进行相对导入(再次出现相对导入问题)
- android - Kotlin:由具有具体类型参数的函数覆盖
- docker - 如何简单地扩展 docker-compose 服务并将索引和计数传递给每个服务?
- python - 为什么 %pylab 没有启动 pylab 模式?
- here-api - 我不断收到“未找到请求的资源”
- r - ggplot随机截距垂直而不是水平
- python - self.variable.function() 在python中的含义或通过类self变量访问方法
- java - HashMap 中的 Java 存储特定类类型