【发布时间】:2014-06-26 11:06:03
【问题描述】:
对于我的一个项目,我想开发一个可用于不同平台(桌面、移动、Surface 等)的库。因此选择了可移植类库。
我正在开发一个使用 HttpClient 调用不同 API 调用的类。我对如何调用方法、响应和解决方法感到困惑。这是我的代码:-
public static async Task<JObject> ExecuteGet(string uri)
{
using (HttpClient client = new HttpClient())
{
// TODO - Send HTTP requests
HttpRequestMessage reqMsg = new HttpRequestMessage(HttpMethod.Get, uri);
reqMsg.Headers.Add(apiIdTag, apiIdKey);
reqMsg.Headers.Add(apiSecretTag, ApiSecret);
reqMsg.Headers.Add("Content-Type", "text/json");
reqMsg.Headers.Add("Accept", "application/json");
//response = await client.SendAsync(reqMsg);
//return response;
//if (response.IsSuccessStatusCode)
//{
string content = await response.Content.ReadAsStringAsync();
return (JObject.Parse(content));
//}
}
}
// Perform AGENT LOGIN Process
public static bool agentStatus() {
bool loginSuccess = false;
try
{
API_Utility.ExecuteGet("http://api.mintchat.com/agent/autoonline").Wait();
// ACCESS Response, JObject ???
}
catch
{
}
finally
{
}
和 ExecuteGet 一样,我也会为 ExecutePost 创建。我的查询来自 ExecuteGet,如果 (1) 我仅在 IsSuccessStatusCode 时通过 JObject 解析,那么我如何知道任何其他错误或消息以通知用户。 (2)如果我通过响应,那么我如何在这里分配它
response = API_Utility.ExecuteGet("http://api.mintchat.com/agent/autoonline").Wait();
这是错误的。
处理这种情况的最佳方法是什么?而且我必须调用多个 API,所以不同的 API 会有不同的结果集。
另外,您能否确认以这种方式设计并添加 PCL 参考,我将能够在多个项目中访问。
更新:- 如以下 2 个答案所述,我已经更新了我的代码。如提供的链接中所述,我从另一个项目中调用。这是我的代码:-
可移植类库:-
private static HttpRequestMessage getGetRequest(string url)
{
HttpRequestMessage reqMsg = new HttpRequestMessage(HttpMethod.Get, url);
reqMsg.Headers.Add(apiIdTag, apiIdKey);
reqMsg.Headers.Add(apiSecretTag, ApiSecret);
reqMsg.Headers.Add("Content-Type", "text/json");
reqMsg.Headers.Add("Accept", "application/json");
return reqMsg;
}
// Perform AGENT LOGIN Process
public static async Task<bool> agentStatus() {
bool loginSuccess = false;
HttpClient client = null;
HttpRequestMessage request = null;
try
{
client = new HttpClient();
request = getGetRequest("http://api.mintchat.com/agent/autoonline");
response = await client.SendAsync(request).ConfigureAwait(false);
if (response.IsSuccessStatusCode)
{
string content = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
JObject o = JObject.Parse(content);
bool stat = bool.Parse(o["status"].ToString());
///[MainAppDataObject sharedAppDataObject].authLogin.chatStatus = str;
o = null;
}
loginSuccess = true;
}
catch
{
}
finally
{
request = null;
client = null;
response = null;
}
return loginSuccess;
}
在另一个 WPF 项目中,在 btn 点击事件中,我将其称为如下:-
private async void btnSignin_Click(object sender, RoutedEventArgs e)
{
/// Other code goes here
// ..........
agent = doLogin(emailid, encPswd);
if (agent != null)
{
//agent.OnlineStatus = getAgentStatus();
// Compile Error at this line
bool stat = await MintWinLib.Helpers.API_Utility.agentStatus();
...
我收到这 4 个错误:-
Error 1 Predefined type 'System.Runtime.CompilerServices.IAsyncStateMachine' is not defined or imported D:\...\MiveChat\CSC
Error 2 The type 'System.Threading.Tasks.Task`1<T0>' is defined in an assembly that is not referenced. You must add a reference to assembly 'System.Threading.Tasks, Version=1.5.11.0, Culture=neutral, PublicKeyToken=b03f5f7f89d50a3a'. D:\...\Login Form.xaml.cs 97 21
Error 3 Cannot find all types required by the 'async' modifier. Are you targeting the wrong framework version, or missing a reference to an assembly? D:\...\Login Form.xaml.cs 97 33
Error 4 Cannot find all types required by the 'async' modifier. Are you targeting the wrong framework version, or missing a reference to an assembly? D:\...\Login Form.xaml.cs 47 28
我尝试仅从 PCL 库中添加 System.Threading.Tasks,这给出了 7 个不同的错误。我哪里错了?怎样做才能使它工作?
请指导我。花费大量时间寻找最佳方式来开发可访问桌面应用程序和 Win Phone 应用程序的库。 任何帮助都非常感谢。谢谢。
【问题讨论】:
标签: c# .net async-await dotnet-httpclient