Solid.Http.Core is where most of the included extension methods are located. It can be used on it's own if you don't need to serialize/deserialize content. The Http request/response is done using HttpClient which is provided by the IHttpClientFactory in Microsoft.Extensions.Http.
> dotnet add package Solid.Http.Core
You can do basic intialization, and Solid.Http.Core will work just fine.
public void ConfigureServices(IServiceCollection services)
{
services.AddSolidHttpCore();
}
You can also register event handlers during initialization.
public void ConfigureServices(IServiceCollection services)
{
services
.AddSolidHttpCore(builder =>
{
builder
.Configure(options =>
{
options.OnClientCreated((services, client) =>
{
// your client created handler here.
});
options.OnRequestCreated((services, request) =>
{
// your request created handler here.
});
options.OnHttpRequest(async (services, httpRequest) =>
{
// your http request handler here.
});
options.OnHttpResponse(async (services, httpResponse) =>
{
// your http response handler here.
});
})
;
})
;
}
Solid.Http.Core includes some interfaces that you will, well, interface with.
ISolidHttpClientFactory is where you request an ISolidHttpClient so that you can perform http request fluently and asynchronously. The interface has two methods.
Create() does what it says. It creates an ISolidHttpClient and provides it to you. This client expects a full and absolute url when it is performing an HTTP request.
CreateWithBaseAddress(Uri baseAddress) creates an ISolidHttpClient that has a base address. Requests made using this client should be relative.
Some people don't realize the difference between /posts and posts as relative addresses.
/posts will go to the root of the baseAddress that it's used on. So if you have a base address of https://mysite.com/api/, a relative address of /posts would point to https://mysite.com/posts, while posts would point to https://mysite.com/api/posts.
ISolidHttpClient is what you use to create an ISolidHttpRequest, which is the interface that has the most extension methods for the fluent syntax.
BaseAddress is a readonly property that is populated when you create the client using ISolidHttpClientFactory.CreateWithBaseAddress.
OnRequestCreated is where the event handlers are registered for when an ISolidHttpRequest is created. They will be run along with any event handlers that were registered when Solid.Http.Core was added to the IServiceCollection.
PerformRequestAsync is what creates an ISolidHttpRequest and starts the fluent syntax of the http request. This method is what the request method extension methods call internally, i.e. GetAsync, PostAsync etc.
ISolidHttpRequest is an awaitable interface which performs an http request and returns an HttpResponseMessage.
This is the root IServiceProvider and is passed to the event handlers that are run on the ISolidHttpRequest. It can also be used in extension methods on ISolidHttpRequest.
The is a simple hashtable of items that can be used in extension methods if you need context between multiple methods.
The cancellation token to be used by HttpClient.
This is the HttpRequestMessage that will be passed to HttpClient and sent over the wire.
This will be populated once the ISolidHttpRequest has been awaited.
OnHttpRequest is where the event handlers are registered that run just before the http request is sent. They will be run along with any event handlers that were registered when Solid.Http.Core was added to the IServiceCollection.
OnHttpResponse is where the event handlers are registered that are run directly after a response has be received. They will be run along with any event handlers that were registered when Solid.Http.Core was added to the IServiceCollection.
As is a method for deserialization of the response content body.
GetAwaiter is what enables the ISolidHttpRequest to be awaited. Once the ISolidHttpRequest is awaited, it will return a HttpResponseMessage.