如何從線程向web服務發出異步請求?異步請求到Web服務
0
A
回答
0
這是一個使用WCF的解決方案。
服務代碼FileService.svc
public class FileService
{
[OperationContract]
public byte[] GetFile(string filename)
{
byte[] File;
//do logic
return File;
}
}
客戶端代碼
public int requested_file_count = 5;
public list<string> filenames;
public FileServiceClient svc
//Constructor
public Example()
{
svc = new FileServiceClient();
}
Public void GetFiles()
{
//Initialise the list of names and set the count of files received
filenames = new list<string>(5);
requested_file_count = filenames.Count;
svc.GetFileCompleted += new EventHandler<GetFileCompletedEventArgs>(GetFile_Completed);
//Call the Async Method passing it the file name and setting the userstate to 1;
svc.GetFileAsync(filenames[0],1);
}
void GetFile_Completed(object Sender, GetFileCompletedEventArgs e)
{
if (e.UserState == requested_file_count)
{
//All files have been downloaded
}
else
{
svc.GetFileAsync(filenames[e.UserState],++e.UserState);
}
//Do Something with the downloaded file
byte[] filedata = e.result;
}
1
這裏是沒有解釋的負載簡短的回答。
之前調用Async
方法您的客戶對象上,確保您不會在UI線程上運行: -
System.Threading.ThreadPool.QueueUserWorkItem(o =>
{
try
{
svc.SomeMethodAsync();
}
catch (err)
{
// do something sensible with err
}
});
現在相應的完成事件發生在ThreadPool
線程沒有UI線程。
相關問題
- 1. 異步http請求服務
- 2. MonoDroid異步服務請求
- 3. Python的Web服務器:如何以服務請求異步
- 4. 異步Web服務請求的處理異常
- 5. 異步任務塊的Web請求
- 6. Web請求異步完成
- 7. 請求到Web服務(POST)
- 8. 異步Web服務
- 9. 向服務器重複異步請求
- 10. IOS POST請求到Rails Web服務:一步一步
- 11. 異步請求多次相同的Web服務
- 12. 多個異步web服務請求NSURLConnection iOS
- 13. Web服務器 - 如何解析請求?異步流標記器?
- 14. 使用Rx簡化異步Silverlight Web服務請求
- 15. Restfull Web服務中的異步HTTP請求
- 16. F#異步Web請求,處理異常
- 17. 用於流式傳輸到異步web請求的服務器端技術
- 18. C#異步Web服務
- 19. 異步REST web服務
- 20. 異步web服務流
- 21. 異步Web服務SOAP
- 22. 創建異步Web服務
- 23. 異步web服務調用
- 24. 異步Web服務調用
- 25. 調用Web服務異步
- 26. 異步調用Web服務
- 27. 異步的Web服務JPA
- 28. 如何構建Tornado異步Web服務以便一次處理單個請求(Singleton請求服務器)
- 29. IIS同步服務請求?
- 30. 請求1.0.4異步請求
爲了迴應您的第一點,如果您使用啓用Silverlight的WCF服務,silverlight會自動爲您生成異步代碼,例如,服務上的GetData()方法將在客戶端生成GetDataCompleted和GetDataAsync方法供您使用。 – Midimatt
是的,但我需要在單獨的線程上執行此操作,以防止用戶嘗試在UI中執行其他操作時掛起UI。 – Joan
所有的代碼都運行在__a__線程中,但我懷疑你的意思是你不想在UI線程不正確的線程中發出異步請求? – AnthonyWJones