2012-05-18 25 views
2

這是一個類庫,它獲取調用形式的網頁Web客戶端 - 異步操作是不允許在此背景下

try 
    { 
    System.Net.WebClient wc = WebAccess.GetWebClient(); 
    wc.UploadStringCompleted += new System.Net.UploadStringCompletedEventHandler(wc_UploadStringCompleted); 
    wc.UploadStringAsync(new Uri("http://" + MobileWLCUrl + urlCreateCacheAPI), GetJSONforGetMenuDetails(Id, MenuIds));      
    } 
catch (Exception ex) { EngineException.HandleException(ex); } 

void wc_UploadStringCompleted(object sender, System.Net.UploadStringCompletedEventArgs e) 
    { 
     string result = e.Result; 
     EngineException.CreateLog("Cache Created (for Menus: " + MenuIds + ") in API for LocationId: " + LocId); 
    } 

HIE傢伙,米試圖異步打這個URL,它是給我這個錯誤,任何幫助將appriciated。

Asynchronous operations are not allowed in this context. 
Page starting an asynchronous operation has to have the Async attribute set to true 
and an asynchronous operation can only be started on a page prior to PreRenderComplete event. 

at System.Web.AspNetSynchronizationContext.OperationStarted() 
at System.Net.WebClient.UploadStringAsync(Uri address, String method, String data, Object userToken) 
at System.Net.WebClient.UploadStringAsync(Uri address, String data) 

日Thnx ...

+0

沒有回答這個?我認爲它是一個常見的錯誤米麪臨,真的很失望 – 1Mayur

回答

9

Async="true"參數添加到@Page指令。

<%@ Page Async="true" ... %> 

西瓦尼,

您需要申請頁面(存儲在"http://" + MobileWLCUrl + urlCreateCacheAPI)本次變動叫下面的代碼:

wc.UploadStringAsync(new Uri("http://" + MobileWLCUrl + urlCreateCacheAPI), GetJSONforGetMenuDetails(Id, MenuIds)); 
+0

你的意思是說,頁面調用? 它是一個.cs文件 我從我的頁面後面調用這個類後面的代碼,我添加了這個但仍然是結果,它不斷給我這個異常 – 1Mayur

+0

頁面指令在aspx頁面上找到。 –

+0

我在頁面中添加了這個內容,但仍然沒有運氣 – 1Mayur