There is currently one XML extension for Solid.Http.Core. The extension uses the DataContractSerializer for serialization/deserialization.
Solid.Http.Xml uses DataContractSerializer internally.
> dotnet add package Solid.Http.Xml
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
};
});
});
}
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;
}
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)
;
}