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?

