6.19Adding IHttpRemoteService Extensions
Created on Aug 17, 2026~1 min read
In addition to the IHttpRemoteService methods provided by the system, you can also add custom extension methods to it to simplify code and reduce duplication. For example, you can add a SendAsSpan method for sending an HTTP remote request that returns Span<char>. The specific implementation is as follows:
public static class HttpRemoteServiceExtensions{ public static Span<char> SendAsSpan(this IHttpRemoteService httpRemoteService, HttpRequestBuilder httpRequestBuilder, CancellationToken cancellationToken = default) { // Null check ArgumentNullException.ThrowIfNull(httpRequestBuilder); var str = httpRemoteService.SendAsString(httpRequestBuilder, cancellationToken); return str.AsSpan(); } public static async Task<Span<char>> SendAsSpanAsync(this IHttpRemoteService httpRemoteService, HttpRequestBuilder httpRequestBuilder, CancellationToken cancellationToken = default) { // Null check ArgumentNullException.ThrowIfNull(httpRequestBuilder); var str = await httpRemoteService.SendAsStringAsync(httpRequestBuilder, cancellationToken); return str.AsSpan(); }}Afterwards, you can easily use this method in an IHttpRemoteService instance:
httpRemoteService.SendAsSpan(HttpRequestBuilder.Get("https://furion.net"));await httpRemoteService.SendAsSpanAsync(HttpRequestBuilder.Get("https://furion.net"));By leveraging the features of C# extension methods, you can greatly enrich the functionality of IHttpRemoteService, reduce duplicated code, and improve code readability and maintainability.