类型或命名空间名称“IRestResponse”无法找到(您是否缺少 using 指令或程序集引用?)
我在下面的 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 withRestResponse
> instead ofIRestResponse
.
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您需要降级 RestSharp NuGet 软件包 (<=106.15)。
RestSharp 在 v107 中进行了重大升级,迁移指南可用。
v107 中删除了已弃用的接口,包括:
可以在此处找到类/接口重新定位位置的文档< /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:
Documentation of where classes/interfaces were relocated can be found here
降速到 106.15 对我有用。
Downrev to 106.15 worked for me.
根据 RestSharp 文档 https://restsharp.dev/v107/#recommished-usage
将 nuget 包更新到最新稳定版本 v.108.03 后,执行请求按预期工作,没有编译器错误。
或者
According the RestSharp docs https://restsharp.dev/v107/#recommended-usage
After just updating the nuget package to the latest stable release v.108.03, Execute request works as intended, without the compiler error.
OR
由于
IRestResponse
已弃用,建议使用RestResponse
。这应该可以修复错误。As
IRestResponse
is deprecated, using ofRestResponse
is suggested. This should fix the error.