MessagePack formatters for ASP.NET Core MVC
$ dotnet add package Alphacloud.MessagePack.AspNetCore.FormattersAllows to use MessagePack format with ASP.NET Core MVC. If multiple formatters are configured, content negotiation is used to determine which formatter to use.
This library leverages MessagePack-CSharp library by Yoshifumi Kawai (a.k.a. neuecc).
| Stable | Pre-release | |
|---|---|---|
| Build | ||
| NuGet ASP.NET Core MVC formatters | ||
| NuGet MediaTypeFormatter for HttpClient |
Install-Packagge Alphacloud.MessagePack.AspNetCore.Formatters
Default configuration uses application/x-msgpack as media typeidentificator, msgpack as file name extension and ContractlessStandardResolver.
Add services.AddMessagePack(); call to your Startup.cs / ConfigureServices
public void ConfigureServices(IServiceCollection services)
{
services.AddMessagePack();
// ...
}
When using minimal MVC configuration (e.g. in WebAPI service) only base services are added by default. You are responsible for configuring each of the service you are going to use.
Note: order of formatters is important - in the example below Json will be default serializer, unless MVC is configured to reject requests with unsupported media type.
public void ConfigureServices(IServiceCollection services)
{
services.AddMvcCore(options =>
{
// ...
})
.AddDataAnnotations()
.AddJsonOptions(options =>
{
options.SerializerSettings.Culture = CultureInfo.InvariantCulture;
options.SerializerSettings.Formatting = Formatting.None;
})
.AddFormatterMappings()
.AddJsonFormatters()
.AddXmlSerializerFormatters()
.AddMsgPackFormatters()
;
}
Default configuration can be changed by providing callback to AddMessagePack method.
Available settings:
MediaTypes - allows to specify additional media handled by MessagePack formatters. Default is application/x-msgpack.FormatterResolver - allows to customize serialization, see MsgPack object serialization for details.FileExtensions - allows to specify reponse format URL mapping. Default is msgpack. See sample project or
Microsoft Docs for more information.Compression - allows to specify LZ4 compression for messages.UseOldSpecification - write message using old specification compatibility mode. Reading will support both old and new specifications. See Message Pack format specification.OmitAssemblyVersion - don't write assembly version and public key token when using typeless formatter.AllowAssemblyVersionMismatch - allows assembly version mistmatch when loading types during deserialization.services.AddMessagePack(opt =>
{
opt.MediaTypes.Clear();
opt.MediaTypes.Add("application/x-messagepack");
opt.FileExtensions.Add("messagepack");
opt.FormatterResolver = MessagePack.Resolvers.ContractlessStandardResolverAllowPrivate.Instance;
})
Microsoft.AspNet.WebApi.Client package contains adds support for formatting and content negotiation to System.Net.Http.
It allows to add custom content serializers by extending MediaTypeFormatter class.
Install-Package Alphacloud.MessagePack.HttpFormatter
Library contains two extension methods PostAsMsgPackAsync and PutAsMsgPackAsync. To serialize payload ContractlessStandardResolver is used, which gives a JSON-like experience.
var response = await httpClient.PostAsMsgPackAsync(uri, payload, cancellationToken).ConfigureAwait(false);
To deserialize Msgpack response, formatter must be added to formatters collection passed to ReadAsAsync extension method.
This is recommeded method as it enables content negotiation.
var res = await response.Content.ReadAsAsync<TestModel>(_formatters, CancellationToken.None);
if you are expecting only MsgPack content, use MsgPackHttpContentExtensions.ReadMsgPackAsAsync() for HttpContent.
Sample application is located at src/samples/NetCoreWebApi.
Sample Postman requests can be found at src/samples/MessagePack.postman_collection.json. To post MessagePack content, use SingleModel.msgpack located under src/samples.