Web 客户端 - 此上下文中不允许异步操作

本文关键字:不允许 异步操作 上下文 客户端 Web | 更新日期: 2023-09-27 18:34:44

它是一个类库,它被称为网页

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);
    }

嗨,伙计们,我试图异步访问此 URL,它给了我此错误,任何帮助都将得到赞赏。

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)

闷......

Web 客户端 - 此上下文中不允许异步操作

Async="true" 参数添加到 @Page 指令中。

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

西瓦尼,

您需要在以下代码中调用的页面(存储在"http://" + MobileWLCUrl + urlCreateCacheAPI中(中应用此更改:

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