2

I am trying to use the WebClient Class inside a Visual Web Part (via a User Control code behind class). When I want to run the web part I always get:

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.

How can I avoid this. I know that I can set the async property to true on a normal ASP.NET page. But how can I achieve this inside SharePoint 2010.

3 Answers 3

1

At what point in the page lifecycle are you using your WebClient object? Not inside the Render method I hope? :)

If you want to set the Async property of the page, have a look at Page.AsyncMode Property

You should be able to set this.Page.AsyncMode on the Load event (in code-behind) of your Visual WebPart.

0

I am developing a sharepoint webpart and trying to make async web service calls, I get the "Asynchronous operations are not allowed in this context. Page starting an asynchronous operation..." message. I was trying to add Page.AsyncMode = true; in the OnLoad of the webPart class however my Page doesnt have the AsyncMode property?

any ideas why I dont have the property ?

0
using (var httpClient = new HttpClient())
        {
            var content = new StringContent(JsonConvert.SerializeObject(req), Encoding.UTF8, "application/json");
            var postResponse = httpClient.PostAsync(endpoint, content).GetAwaiter().GetResult();
            if (postResponse.IsSuccessStatusCode)
            {
                //var responseContent = response.Content;
            //    return responseContent.ReadAsStringAsync().GetAwaiter().GetResult();
            }
        }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.