![](/img/trans.png)
[英]What do I use in place of “FirstOrDefault()” if I want to find an exact search result in my database?
[英]What to use if i want to return the result of a webclient
采取以下代碼:
public async Task<string> AuthenticatedGetData(string url, string token)
{
WebClient client = new WebClient();
client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(WebClient_DownloadStringCompleted);
client.DownloadStringAsync(new Uri(url + "?oauth_token=" + token));
}
private void WebClient_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
string response = e.Result;
}
WebClient_DownloadStringCompleted被調用...並且response =我想要的響應...很好。 完善...
現在考慮我如何調用此AuthenticatedGetData方法:
它是從一種存儲庫中調用的...存儲庫需要一個字符串,以便它可以序列化並對生成的對象進行處理...
因此,所有內容都從存儲庫開始異步運行...對該身份驗證的getdata進行了調用,然后發出了一個請求... ...但是,因為downloadstringasync沒有.Result()方法,並且因為downloadstringcompleted需要調用void方法。 ..我無法將結果字符串返回到調用存儲庫。
關於必須做什么才能獲得client.DownloadStringAsync以在完成時返回響應字符串的任何想法?
是我只需要將我的數據訪問操作緊密地耦合到該特定應用程序即可。.似乎是如此..可重復使用的:(我真的想使我的整個身份驗證工作與即將發生的事情完全分開。不想為每個存儲庫重復上面的代碼,因為無論如何它都一樣!
編輯:://
我在我的類中創建了一個抽象方法來處理上述請求...,然后用我的存儲庫擴展該類並實現抽象方法。 聽起來不錯?
按要求編輯://呼叫代碼:
public class OrganisationRepository
{
PostRequest postRequest;
public OrganisationRepository()
{
this.postRequest = new PostRequest();
}
public IEnumerable<Organisation> GetAll()
{
string requestUrl = BlaBla.APIURL + "/org/";
string response = postRequest.AuthenticatedGetData(requestUrl, BlaBla.Contract.AccessToken).Result;
}
}
public class PostRequest
{
public Task<string> AuthenticatedGetData(string url, string token)
{
TaskCompletionSource<string> tcs = new TaskCompletionSource<string>();
WebClient client = new WebClient();
client.DownloadStringCompleted += (sender, e) =>
{
if (e.Error != null)
{
tcs.TrySetException(e.Error);
}
else if (e.Cancelled)
{
tcs.TrySetCanceled();
}
else
{
tcs.TrySetResult(e.Result);
}
};
client.DownloadStringAsync(new Uri(url + "?oauth_token=" + token));
return tcs.Task;
}
}
我不確定Windows Phone 8對此有何限制。 但是我認為這應該可行。
public Task<string> AuthenticatedGetData(string url, string token)
{
TaskCompletionSource<string> tcs = new TaskCompletionSource<string>();
WebClient client = new WebClient();
client.DownloadStringCompleted += (sender, e) =>
{
if (e.Error != null)
{
tcs.TrySetException(e.Error);
}
else if (e.Cancelled)
{
tcs.TrySetCanceled();
}
else
{
tcs.TrySetResult(e.Result);
}
};
client.DownloadStringAsync(new Uri(url + "?oauth_token=" + token));
return tcs.Task;
}
您也許也可以解決這個問題(不確定它是否可以在Windows Phone 8上運行)
public Task<string> AuthenticatedGetData(string url, string token)
{
WebClient client = new WebClient();
return client.DownloadStringTaskAsync(new Uri(url + "?oauth_token=" + token));
}
一個解決方案是根本不使用WebClient庫。 問題出在這里:您正在等待異步的AuthenticatedGetData任務,但是它沒有任何作用,因為該任務中沒有等待調用,這意味着它將立即完成。 在任務上調用.result始終為null,因為您永遠無法在該方法中返回值。 觸發DownloadCompleted事件后,WebClient依賴於調用函數調用。 但是,除非有人也訂閱那個DownloadCompleted事件處理程序,這是很愚蠢的,否則任何人都無法確切知道何時發生這種情況。 我建議使用HttpWebRequest http://blogs.msdn.com/b/andy_wigley/archive/2013/02/07/async-and-await-for-http-networking-on-windows-phone制作真正的異步Web請求服務.aspx祝您好運。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.