Skip to content
This repository was archived by the owner on Apr 10, 2025. It is now read-only.

Latest commit

 

History

History
86 lines (72 loc) · 2.26 KB

README.md

File metadata and controls

86 lines (72 loc) · 2.26 KB

Solid.Http XML extensions

There is currently one XML extension for Solid.Http.Core. The extension uses the DataContractSerializer for serialization/deserialization.

Solid.Http.Xml

Solid.Http.Xml uses DataContractSerializer internally.

> dotnet add package Solid.Http.Xml

Initialization with Solid.Http.Core

You can add Solid.Http.Xml using the builder.

public void ConfigureServices(IServiceCollection services)
{
    // This call to AddSolidHttpCore could be replaced with AddSolidHttp
    services.AddSolidHttpCore(builder => 
    {
        builder.AddXml(options => 
        {
            options.SerializerSettings = new DataContractSerializerSettings
            {
                // configure your DataContractSerializerSettings here
            };
        });
    });
}

Http request body serialization

Included in the package is an extension method that makes it easier to add XML content to an HTTP request.

public async Task<Post> CreateAsync(Post post)
{
    var settings = new DataContractSerializerSettings
    {
        // configure your settings here.
    };
    await _client
        .PostAsync("posts")
        // contentType and settings are optional
        // If settings are omitted, the settings that are specified in the SolidHttpXmlOptions are used.
        .WithXmlContent(post, contentType: "application/xml", settings: settings)
    ;
    return post;
}

Http response body deserialization

The As methods that are included with Solid.Http.Core will use the configured DataContractSerializerSettings to deserialize the received response body.

public async Task<Post> GetAsync(int id)
{
    return await _client
        .GetAsync("posts/{id}")
        .WithNamedParameter("id", id)
        .ExpectSuccess()
        .As<Post>()
    ;
}

You can also specify DataContractSerializerSettings if a specific API endpoint differs from the default in how it serializes it's entities.

public async Task<Post> GetAsync(int id)
{
    var settings = new DataContractSerializerSettings
    {
        // configure your settings here.
    };
    return await _client
        .GetAsync("posts/{id}")
        .WithNamedParameter("id", id)
        .ExpectSuccess()
        .As<Post>(settings)
    ;
}