'RestClient' 是一个命名空间,但它像类型一样使用 - 包问题

'RestClient' is a namespace but its used like a type - package issue

我已经删除了敏感数据,但是在尝试发出 API 请求时遇到了这个问题,尽管我已经安装了相关的软件包。为什么会这样?

using System;
using IronXL; 
using RestClient;
using Rest;
using Newtonsoft;
using RestSharp;
using RestSharp.Authenticators;
using RestSharp.Validation;
    public static void APIRequest()
        
    {
        var client = new RestClient("");
        client.Timeout = -1;
        var request = new RestRequest(Method.POST);
        request.AddHeader("Accept", "application/vnd.evolutionx.v1+json");
        request.AddHeader("Authorization", "Bearer  ");
        request.AddHeader("Content-Type", "application/json");
        IRestResponse response = client.Execute(request);
        Console.WriteLine(response.Content);

    }

这一行:

using RestClient;

使 RestClient 成为您代码中的已知命名空间。然后,C# 将假定对 RestClient 的任何引用都引用该命名空间,除非您特别说明。

如果RestClient确实是一个class,那么你必须在它前面指定它的命名空间

例如

new RelevantNamespace.RestClient("")

以区别于命名空间。我猜这是来自 RestSharpRestClient class,所以可能新的 RestSharp.RestClient("") 是有道理的。


另一方面,您在评论中说 RestClient 命名空间在 IDE 中是灰色的。这意味着你没有使用它的任何东西。因此,要解决您的问题,您可以简单地从文件中删除 using RestClient; 语句。

一般情况下,您可能也不需要 RestClient.net 包,因为 RestSharp 已经完成了非常相似的工作。

当您使用 RestClient.net 时,您似乎需要使用

进行实例化

var client = new RestClient.Net.Client();

所以 class 是 Client

如何设置 class 的详细信息在这里: https://github.com/MelbourneDeveloper/RestClient.Net

注意如何开始

var client = new Client(new NewtonsoftSerializationAdapter(), new Uri("https://restcountries.eu/rest/v2/"));
var response = await client.GetAsync<List<RestCountry>>();

或使用 .NET Core 序列化

var client = new Client(new Uri("https://restcountries.eu/rest/v2/"));
var response = await client.GetAsync<List<RestCountry>>();