Alphacloud.MessagePack.AspNetCore.Formatters 5.0.0-alpha0020

This is a prerelease version of Alphacloud.MessagePack.AspNetCore.Formatters.
dotnet add package Alphacloud.MessagePack.AspNetCore.Formatters --version 5.0.0-alpha0020
                    
NuGet\Install-Package Alphacloud.MessagePack.AspNetCore.Formatters -Version 5.0.0-alpha0020
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Alphacloud.MessagePack.AspNetCore.Formatters" Version="5.0.0-alpha0020" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Alphacloud.MessagePack.AspNetCore.Formatters" Version="5.0.0-alpha0020" />
                    
Directory.Packages.props
<PackageReference Include="Alphacloud.MessagePack.AspNetCore.Formatters" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Alphacloud.MessagePack.AspNetCore.Formatters --version 5.0.0-alpha0020
                    
#r "nuget: Alphacloud.MessagePack.AspNetCore.Formatters, 5.0.0-alpha0020"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#addin nuget:?package=Alphacloud.MessagePack.AspNetCore.Formatters&version=5.0.0-alpha0020&prerelease
                    
Install as a Cake Addin
#tool nuget:?package=Alphacloud.MessagePack.AspNetCore.Formatters&version=5.0.0-alpha0020&prerelease
                    
Install as a Cake Tool

MessagePack Formatters for ASP.NET Core MVC and HttpClient

Allows 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).

Build status

Stable Pre-release
Build Master PreRelease
NuGet ASP.NET Core MVC formatters NuGet NuGet
NuGet MediaTypeFormatter for HttpClient NuGet NuGet

Packages

ASP.NET Core MVC formatters

Installation

Install-Packagge Alphacloud.MessagePack.AspNetCore.Formatters

Features

  • Input formatter (decode MessagePack payload).
  • Output formatter (encode MessagePack payload).
  • Source link support (source code on demand in debugger).

Usage

Default configuration

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();
    // ...
}
MVC Core (ASP.NET Core 2.x)

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()
    ;
}

Custom configuration

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;
  })

MediaTypeFormatter for HttpClient

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.

Installation

Install-Package Alphacloud.MessagePack.HttpFormatter

Usage

Sending request

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);

Reading response

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.

Samples

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.

References

Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 is compatible.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
5.0.0-alpha0020 158 3/14/2024
5.0.0-alpha0019 121 3/8/2024
5.0.0-alpha0013 261 4/1/2023
5.0.0-alpha0012 147 3/31/2023
4.1.0 150,357 3/31/2023
4.0.0 13,484 5/30/2022
4.0.0-beta0001 200 5/27/2022
4.0.0-alpha0025 189 5/27/2022
4.0.0-alpha0021 193 5/26/2022
3.2.0 12,156 8/1/2021
3.2.0-alpha0018 312 8/1/2021
3.2.0-alpha0017 321 8/1/2021
3.2.0-alpha0016 318 8/1/2021
3.2.0-alpha0010 257 6/20/2021
3.2.0-alpha0008 286 4/30/2021
3.2.0-alpha0007 317 4/19/2021
3.2.0-alpha0006 317 4/19/2021
3.2.0-alpha0001 277 1/29/2021
3.1.0 5,388 1/29/2021
3.1.0-beta0001 258 1/29/2021
3.1.0-alpha0003 289 1/28/2021
3.1.0-alpha0002 349 12/27/2020
3.1.0-alpha0001 328 12/27/2020
3.0.0 652 12/6/2020
3.0.0-beta0001 393 12/5/2020
2.3.0-alpha0010 423 12/5/2020
2.3.0-alpha0001 387 10/21/2020
2.2.0 2,215 10/21/2020
2.2.0-beta0001 412 10/21/2020
2.2.0-alpha0045 389 10/21/2020
2.2.0-alpha0025 468 2/26/2020
2.2.0-alpha0024 541 2/8/2020
2.2.0-alpha0023 514 2/8/2020
2.2.0-alpha0020 413 8/19/2020
2.2.0-alpha0019 454 6/22/2020
2.2.0-alpha0017 460 5/20/2020
2.2.0-alpha0016 458 5/20/2020
2.2.0-alpha0015 415 5/19/2020
2.2.0-alpha0001 425 3/1/2020
2.1.3 2,458 8/19/2020
2.1.3-beta0001 427 8/19/2020
2.1.2 1,224 5/29/2020
2.1.2-beta0001 430 5/29/2020
2.1.1 1,333 3/1/2020
2.1.1-beta0001 471 3/1/2020
2.1.0 828 2/8/2020
2.1.0-beta0001 505 2/8/2020
2.1.0-alpha0004 519 2/8/2020
2.1.0-alpha0002 542 2/8/2020
2.0.0 1,282 1/14/2020
2.0.0-beta0001 443 1/14/2020
2.0.0-alpha0003 554 1/12/2020
1.1.0-alpha0002 457 11/18/2019
1.0.0 791 11/18/2019
1.0.0-beta0001 465 11/18/2019
0.3.0-alpha0011 454 11/9/2019
0.3.0-alpha0005 511 7/10/2019
0.3.0-alpha0002 492 7/6/2019
0.3.0-alpha0001 511 5/8/2019
0.2.0 1,822 2/5/2019
0.2.0-alpha0019 598 2/4/2019
0.2.0-alpha0018 592 2/4/2019
0.2.0-alpha0016 830 1/28/2019
0.2.0-alpha0013 837 1/27/2019
0.2.0-alpha0001 787 8/19/2018
0.1.0 1,123 8/19/2018
0.1.0-rc0024 785 8/19/2018
0.1.0-beta0001 813 8/19/2018