Web Client - Asynchronous operations are not allowed in this context

11,338

Add the Async="true" parameter to the @Page directive.

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

Siwani,

You need to apply this change in the page (stored in "http://" + MobileWLCUrl + urlCreateCacheAPI) called in following code:

wc.UploadStringAsync(new Uri("http://" + MobileWLCUrl + urlCreateCacheAPI), GetJSONforGetMenuDetails(Id, MenuIds)); 
Share:
11,338
1Mayur
Author by

1Mayur

Senior software engineer working on real-time solutions LinkedIn

Updated on June 04, 2022

Comments

  • 1Mayur
    1Mayur almost 2 years

    It is a Class Library and it is getting called form a web page

    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 guys, m trying to hit this url asynchronously and it is giving me this error, any help will be 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...