6.31Long Polling Event Handler
Created on Aug 17, 2026~3 min read
The IHttpLongPollingEventHandler interface allows you to define pre-processing operations for sending long polling requests. By implementing this interface, you can create a custom long polling event handler, such as the CustomLongPollingEventHandler class:
public class CustomLongPollingEventHandler : IHttpLongPollingEventHandler{ // Used to receive data when the server returns a 200~299 status code public Task OnDataReceivedAsync(HttpResponseMessage httpResponseMessage, CancellationToken cancellationToken) {} // Used to receive data when the server returns a status code other than 200~299 public Task OnErrorAsync(HttpResponseMessage httpResponseMessage, CancellationToken cancellationToken) {} // Used for the operation triggered when the response headers contain X-End-Of-Stream public Task OnEndOfStreamAsync(HttpResponseMessage httpResponseMessage, CancellationToken cancellationToken) {}}To enable this handler in your application, register the CustomLongPollingEventHandler service in the Startup.cs or Program.cs file:
services.TryAddSingleton<CustomLongPollingEventHandler>();Next, you can specify this handler when building the HTTP request:
HttpRequestBuilder.LongPolling("https://localhost:7044/HttpRemote/LongPolling" , async (responseMessage, token) => { Console.WriteLine(await responseMessage.Content.ReadAsStringAsync(cancellationToken)); await Task.CompletedTask; }) .SetEventHandler<CustomLongPollingEventHandler>();HttpRequestBuilder.LongPolling("https://localhost:7044/HttpRemote/LongPolling" , async (responseMessage, token) => { Console.WriteLine(await responseMessage.Content.ReadAsStringAsync(cancellationToken)); await Task.CompletedTask; }) .SetEventHandler(typeof(CustomLongPollingEventHandler)); // Using the type-based approach