Show / Hide Table of Contents

Method GetDeferral

GetDeferral()

Returns a deferral which can be completed when the event is ready to continue.

Declaration
public IDisposable GetDeferral()
Returns
Type Description
System.IDisposable

A deferral object that implements System.IDisposable.

Remarks

Sometimes, you may need to use async code when working with the HttpResponseEnd event. Normally, you might be able to use an async void event handler without issue.

Consider a scenario where you want to change the response received based on the contents of the current response. In order to do this, you need to asynchronously read the contents of the response before you can inspect it. In this scenario, an async event handler needs a way to delay the HTTP handler until it has completed processing the response.

The approach that WinRT has taken for this scenario is through "deferral" objects. An asynchronous event handler acquires a deferral before the first await and notifies that deferral when the handler is complete.

Examples

The deferral should be combined with using for convenient writing of async code.

ArcGISHttpClientHandler.HttpResponseEnd += async (sender, e) =>
{
    using (e.GetDeferral())
    {
        // Your async code goes here...
        var content = await e.Response.Content.ReadAsStringAsync().ConfigureAwait(false);
    }
}

Applies to

Platforms and versions
TargetVersions
.NET Standard 2.0100.10 - 200.8
.NET100.13 - 200.8
.NET Windows100.13 - 200.8
.NET Android200.0 - 200.8
.NET iOS200.0 - 200.8
.NET Framework100.10 - 200.8
Xamarin.Android100.10 - 100.15
Xamarin.iOS100.10 - 100.15
UWP100.10 - 200.8
In This Article
Back to top Copyright © 2022 Esri.