0

I'm trying to implement an async call in an ASP.NET WebForms page. I can do this by either registering the async method:

RegisterAsyncTask(new PageAsyncTask())

or adding async to page load:

protected async void Page_Load(object sender, EventArgs e)

In either case, when I access the page in a browser I get the following error:

Task-returning Page methods are unsupported in the current application configuration. To work around this, remove the following configuration switch in Web.config: <appSettings> <add key="aspnet:UseTaskFriendlySynchronizationContext" /> </appSettings>

If I remove this from the web.config (or set the value to "true") everything works as expected. However, when I deploy the code to an Azure Web App, I get the error:

System.InvalidOperationException: <%@ Page AspCompat="true" %> and are unsupported in the current application configuration. To work around this, add the following configuration switch in Web.config: <appSettings> <add key="aspnet:UseTaskFriendlySynchronizationContext" value="false" />

So you can see the problem is that the async method needs UseTaskFriendlySynchronizationContext="true", but the Azure Web App environment needs UseTaskFriendlySynchronizationContext="false". How can I resolve this?

9
  • What exactly do you want to execute asynchronously? Usually that’s a red flag IMHO as people tend to do the wrong things there. Commented Oct 24, 2024 at 14:39
  • Are you getting the error locally as well ? Commented Oct 24, 2024 at 16:00
  • Not getting the error locally, only in Azure. Commented Oct 24, 2024 at 16:14
  • Can you please share the full code snippet ? Commented Oct 24, 2024 at 16:23
  • Did you get a chance to check this MSDoc Commented Oct 24, 2024 at 16:25

1 Answer 1

0

Thanks @mason for the comments.

Even I got the same error.

Error with false value,

_Task-returning Page methods are unsupported in the current application configuration. To work around this, remove the following configuration switch in Web.config:  
<appSettings>  
<add key="aspnet:UseTaskFriendlySynchronizationContext" />  
</appSettings>  
For more information, see http://go.microsoft.com/fwlink/?LinkId=252465._

Error with true value

 _This_operation requires the page to be asynchronous (the Async attribute must be set to true)._
  • Error message clearly says to add Async="true" in the .aspx file.

  • As suggested in the error message added Async="true" in the .aspx Page directive.

<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="ASPForms._Default" Async="true" %>
  • Now, I am able to access the app without appsetting UseTaskFriendlySynchronizationContext key as well as with value="true" with Async="true" in .aspx page.

  • Deployed the app without the appsetting and able to access it without any issues.

enter image description here

but the Azure Web App environment needs UseTaskFriendlySynchronizationContext="false"

enter image description here

  • Web.config for Local and Web.Release.config for Azure App service (Production).

Web.Release.config:

  <appSettings>
    <add key="aspnet:UseTaskFriendlySynchronizationContext" value="false" xdt:Transform="SetAttributes" xdt:Locator="Match(key)" />
  </appSettings>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.