2.6HTTP Declarative Requests (Proxy Approach)
The HTTP Declarative Requests mechanism dynamically builds implementation classes at runtime by implementing the IHttpDeclarative interface. This mechanism intelligently intercepts method calls that match specific rules and automatically generates the corresponding HTTP remote request code. This approach not only greatly reduces the burden on developers writing HTTP request code, but also makes the code structure clearer and easier to organize, maintain, and reuse.
The following example briefly demonstrates how to define and use HTTP Declarative Requests:
1. Define the IHttpService Interface and Implement IHttpDeclarative
public interface IHttpService : IHttpDeclarative{ // Get the website content [Get("https://furion.net")] Task<string> GetWebSiteContent(); // Carry request data [Post("https://localhost:7044/HttpRemote/AddModel")] [QueryParam("query1", 1)] // Set query parameters Task<YourRemoteModel> PostData([QueryParam(AliasAs = "query2")] string param, [Body(MediaTypeNames.Application.Json)] object data); // Set query parameters and specify an alias and request content // Form form submission [Post("https://localhost:7044/HttpRemote/AddForm?id=1")] Task<YourRemoteFormResult> PostForm(Action<HttpMultipartFormDataBuilder> multipart); // Form form submission [Post("https://localhost:7044/HttpRemote/AddForm?id=1")] Task<YourRemoteFormResult> PostForm2([Multipart(AsFormItem = false)] object obj, [Multipart("file", AsFileFrom = FileSourceType.Path)] string filePath); // URL-encoded form submission [Post("https://localhost:7044/HttpRemote/AddURLForm")] Task<YourRemoteModel> PostURLForm([Body(MediaTypeNames.Application.FormUrlEncoded)] object data);}2. Register the IHttpService Service
In the Startup.cs or Program.cs file, register and configure the HttpRemote service to support HTTP Declarative Requests:
services.AddHttpRemote(builder =>{ // Register a single HTTP declarative request interface builder.AddHttpDeclarative<IHttpService>(); // Scan assemblies to register HTTP declarative request interfaces in bulk (this registration method is recommended) // builder.AddHttpDeclarativesFromAssemblies([Assembly.GetEntryAssembly()]); // If you are using the Furion framework, you can pass App.Assemblies directly});3. Inject the IHttpService Service
In classes that need to use IHttpService, obtain its instance via dependency injection:
public class YourService{ private readonly IHttpService _httpService; public YourService(IHttpService httpService) { _httpService = httpService; }}If you are using .NET 8 or later, you can simplify the code by injecting via primary constructors:
public class YourService(IHttpService httpService){ // Use the httpService variable}Alternatively, you can also inject on demand within a specific method:
public class YourService{ public Task<string> GetResource([FromServices] IHttpService httpService) { // Your code logic }}4. Call the IHttpService Methods
Use the injected IHttpService instance to call its methods to send HTTP requests and obtain responses:
// Get the website contentvar content = await httpService.GetWebSiteContent();// Carry request datavar content = await httpService.PostData("furion", new { id = 1, name = "furion" });// Form form submissionvar content = await httpService.PostForm(multipart => multipart .AddJson(new { id = 1, name = "furion" }) // Set regular fields .AddFileAsStream(@"C:\Workspaces\httptest.jpg", "file"));var content = await httpService.PostForm2(new { id = 1, name = "furion" }, @"C:\Workspaces\httptest.jpg");// URL-encoded form submissionvar content = await httpService.PostURLForm(new { id = 1, name = "furion" });By using HTTP Declarative Requests, you can significantly reduce the effort of writing HTTP request code and make the code more concise and easier to organize and maintain. This approach is especially recommended for large projects or multi-person collaboration projects.