c# - 使用 HttpClient 时收到无效参数
问题描述
我正在尝试在控制台应用程序中获取 web api 数据。接收数据正常工作,直到我尝试将其他名为“Nsi”的库中的其他类连接到应用程序。因此,接收数据失败并出现 Invalid argument received 错误。更改库名称可解决该错误。所有的库都建立在 Net5 平台上
链接到示例项目 https://drive.google.com/drive/folders/1wftAzUWqtEefdv2vWiOwzuNGHBvmehG9?usp=sharing
控制台应用程序:
static void Main(string[] args)
{
var httpServices = new ServiceCollection();
httpServices.AddHttpClient<IHttpService, HttpService>();
httpServices.AddTransient<ITest, Test>();
var httpServiceProvider = httpServices.BuildServiceProvider();
var client = httpServiceProvider.GetRequiredService<IHttpService>();
var ss = client.Get("https://www.google.com/");
Console.Write(ss);
}
插件类:
public class HttpService : IHttpService
{
private readonly HttpClient Client;
public HttpService(HttpClient httpClient)
{
Client = httpClient;
}
public string Get(string url)
{
return Client.GetStringAsync(url).Result;
}
}
public interface IHttpService
{
public string Get(string url);
}
此类已放置在单独的库“Nsi”中:
public interface ITest
{
void TestMethod();
}
public class Test : ITest
{
public void TestMethod()
{
}
}
解决方案
推荐阅读
- java - java.lang.ClassNotFoundException:无法加载请求的类:org.hibernate.dialect.Oracle12cDialect
- oracle - 在 PL/SQL 中,您创建了一个具有以下标题和声明部分的新函数
- python - 水平滚动条未放置在我想要的位置
- reactjs - 使用带有材料 UI 的反应钩子的表中列的平均总值
- python - 在熊猫数据框中将日期时间格式 2020-09-16 00:00:00 转换为 20200916
- c# - 左右移位的目的是什么
- git - 单个 Repo 下的 Azure DevOps 多个构建管道 使用 Git 时耗尽构建服务器内存
- javascript - 在 JavaScript 不工作的情况下获取 HTML 中范围滑块的值
- php - 用PHP登录和注册,登录说密码无效
- java - 多对多 ormlite 问题