类型或命名空间名称“IRestResponse”无法找到(您是否缺少 using 指令或程序集引用?)

发布于 2025-01-10 09:11:58 字数 1515 浏览 1 评论 0原文

我在下面的 IRestResponse 中遇到问题:

public async Task<CezanneToken> GetAccessToken()
{
    var client = new RestClient(WebConfigurationManager.AppSettings["TokenUrl"]);
    var request = new RestRequest();
    request.Method = Method.Post;
    request.AddHeader("cache-control", "no-cache");
    request.AddHeader("content-type", "application/x-www-form-urlencoded");
    request.AddParameter("application/x-www-form-urlencoded", "grant_type=client_credentials&client_id=" + WebConfigurationManager.AppSettings["ClientId"] + "&client_secret=" + WebConfigurationManager.AppSettings["ClientSecret"] + "", ParameterType.RequestBody);

    IRestResponse response = await client.ExecuteAsync(request);
    string serStatus = ((RestResponseBase)response).Content;
    CezanneToken details = JsonConvert.DeserializeObject<CezanneToken>(serStatus);
    string Token = details.access_token;
    
    return details;
}

IRestResponse throws

找不到类型或命名空间名称“IRestResponse”(是否缺少 using 指令或程序集引用?) 我无法让它发挥作用。 IntelliSense 建议使用 RestResponse>而不是IRestResponse

但是,当我使用 RestResponse 时,我在响应中收到 Bad Request

上面的代码示例是从 Visual Basic“翻译”而来的,但它在 VB 中运行得很好。我不知道 Bad Request 的问题是否来自使用 RestResponse,但我假设像在 VB 中一样需要 IRestResponse

我也见过有人使用 IRestResponse 但它对我不起作用。我有 using RestSharp; 但我还需要其他东西吗?

I have a problem with IRestResponse in the below:

public async Task<CezanneToken> GetAccessToken()
{
    var client = new RestClient(WebConfigurationManager.AppSettings["TokenUrl"]);
    var request = new RestRequest();
    request.Method = Method.Post;
    request.AddHeader("cache-control", "no-cache");
    request.AddHeader("content-type", "application/x-www-form-urlencoded");
    request.AddParameter("application/x-www-form-urlencoded", "grant_type=client_credentials&client_id=" + WebConfigurationManager.AppSettings["ClientId"] + "&client_secret=" + WebConfigurationManager.AppSettings["ClientSecret"] + "", ParameterType.RequestBody);

    IRestResponse response = await client.ExecuteAsync(request);
    string serStatus = ((RestResponseBase)response).Content;
    CezanneToken details = JsonConvert.DeserializeObject<CezanneToken>(serStatus);
    string Token = details.access_token;
    
    return details;
}

IRestResponse throws

The type or namespace name "IRestResponse" could not be found (are you missing a using directive or an assembly reference?)
I cannot make it work. IntelliSense suggest to go with RestResponse> instead of IRestResponse.

But when I go with RestResponse I get Bad Request on the response.

The code sample above is "translated" from Visual Basic but it works just fine in VB. I don't know if the problem with the Bad Request comes from using RestResponse but I assume IRestResponse is needed just as in VB.

I've also seen people using IRestResponse but it just doesn't work for me. I have the using RestSharp; but do I need something else as well?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

執念 2025-01-17 09:11:58

您需要降级 RestSharp NuGet 软件包 (<=106.15)。

RestSharp 在 v107 中进行了重大升级,迁移指南可用。

v107 中删除了已弃用的接口,包括:

  • IRestRequest
  • IRestResponse
  • IHttp

可以在此处找到类/接口重新定位位置的文档< /a>

You will need to downgrade the RestSharp NuGet package (<=106.15).

RestSharp got a major upgrade in v107, a Migration Guide is available.

Depercated interfaces where removed in v107, including:

  • IRestRequest
  • IRestResponse
  • IHttp

Documentation of where classes/interfaces were relocated can be found here

睫毛上残留的泪 2025-01-17 09:11:58

降速到 106.15 对我有用。

Downrev to 106.15 worked for me.

无所谓啦 2025-01-17 09:11:58

根据 RestSharp 文档 https://restsharp.dev/v107/#recommished-usage

IRestResponse 接口已弃用。你得到一个实例
RestResponse 作为回报。

将 nuget 包更新到最新稳定版本 v.108.03 后,执行请求按预期工作,没有编译器错误。

RestResponse response = _client.Execute(request);  

或者

RestResponse response = await client.ExecuteAsync(request);

According the RestSharp docs https://restsharp.dev/v107/#recommended-usage

The IRestResponse interface is deprecated. You get an instance of
RestResponse in return.

After just updating the nuget package to the latest stable release v.108.03, Execute request works as intended, without the compiler error.

RestResponse response = _client.Execute(request);  

OR

RestResponse response = await client.ExecuteAsync(request);
夕嗳→ 2025-01-17 09:11:58

由于 IRestResponse 已弃用,建议使用 RestResponse。这应该可以修复错误。

RestResponse response = _client.Execute(request);

As IRestResponse is deprecated, using of RestResponse is suggested. This should fix the error.

RestResponse response = _client.Execute(request);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文