Alphacloud.MessagePack.HttpFormatter
5.0.0-alpha0020
dotnet add package Alphacloud.MessagePack.HttpFormatter --version 5.0.0-alpha0020
NuGet\Install-Package Alphacloud.MessagePack.HttpFormatter -Version 5.0.0-alpha0020
<PackageReference Include="Alphacloud.MessagePack.HttpFormatter" Version="5.0.0-alpha0020" />
paket add Alphacloud.MessagePack.HttpFormatter --version 5.0.0-alpha0020
#r "nuget: Alphacloud.MessagePack.HttpFormatter, 5.0.0-alpha0020"
// Install Alphacloud.MessagePack.HttpFormatter as a Cake Addin #addin nuget:?package=Alphacloud.MessagePack.HttpFormatter&version=5.0.0-alpha0020&prerelease // Install Alphacloud.MessagePack.HttpFormatter as a Cake Tool #tool nuget:?package=Alphacloud.MessagePack.HttpFormatter&version=5.0.0-alpha0020&prerelease
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 | ||
NuGet ASP.NET Core MVC formatters | ||
NuGet MediaTypeFormatter for HttpClient |
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 isapplication/x-msgpack
.FormatterResolver
- allows to customize serialization, see MsgPack object serialization for details.FileExtensions
- allows to specify reponse format URL mapping. Default ismsgpack
. 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
- MessagePack format https://msgpack.org/index.html
- MessagePack-CSharp https://github.com/neuecc/MessagePack-CSharp
- AddMVC vs AddMvcCore
- Content negotiation in ASP.NET Core MVC https://code-maze.com/content-negotiation-dotnet-core/
- Microsoft.AspNet.WebApi.Client https://www.nuget.org/packages/Microsoft.AspNet.WebApi.Client/
- Source Link - https://github.com/ctaggart/SourceLink
- Postman https://www.getpostman.com/downloads/
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. 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 was computed. 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 was computed. 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. |
.NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
.NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen40 was computed. tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.0
- JetBrains.Annotations (>= 2023.3.0)
- MessagePack (>= 2.5.140 && < 3.0.0)
- Microsoft.AspNet.WebApi.Client (>= 6.0.0 && < 7.0.0)
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 | 97 | 3/14/2024 |
5.0.0-alpha0019 | 100 | 3/8/2024 |
5.0.0-alpha0013 | 206 | 4/1/2023 |
5.0.0-alpha0012 | 134 | 3/31/2023 |
4.1.0 | 29,635 | 3/31/2023 |
4.0.0 | 17,629 | 5/30/2022 |
4.0.0-beta0001 | 164 | 5/27/2022 |
4.0.0-alpha0025 | 173 | 5/27/2022 |
4.0.0-alpha0021 | 175 | 5/26/2022 |
3.2.0 | 5,041 | 8/1/2021 |
3.2.0-alpha0018 | 268 | 8/1/2021 |
3.2.0-alpha0017 | 292 | 8/1/2021 |
3.2.0-alpha0016 | 270 | 8/1/2021 |
3.2.0-alpha0010 | 244 | 6/20/2021 |
3.2.0-alpha0008 | 250 | 4/30/2021 |
3.2.0-alpha0007 | 278 | 4/19/2021 |
3.2.0-alpha0006 | 285 | 4/19/2021 |
3.2.0-alpha0001 | 204 | 1/29/2021 |
3.1.0 | 1,258 | 1/29/2021 |
3.1.0-beta0001 | 201 | 1/29/2021 |
3.1.0-alpha0003 | 213 | 1/28/2021 |
3.1.0-alpha0002 | 271 | 12/27/2020 |
3.1.0-alpha0001 | 281 | 12/27/2020 |
3.0.0 | 534 | 12/6/2020 |
3.0.0-beta0001 | 350 | 12/5/2020 |
2.3.0-alpha0010 | 367 | 12/5/2020 |
2.3.0-alpha0001 | 351 | 10/21/2020 |
2.2.0 | 1,022 | 10/21/2020 |
2.2.0-beta0001 | 329 | 10/21/2020 |
2.2.0-alpha0045 | 358 | 10/21/2020 |
2.2.0-alpha0025 | 382 | 2/26/2020 |
2.2.0-alpha0024 | 462 | 2/8/2020 |
2.2.0-alpha0023 | 455 | 2/8/2020 |
2.2.0-alpha0020 | 388 | 8/19/2020 |
2.2.0-alpha0019 | 397 | 6/22/2020 |
2.2.0-alpha0017 | 372 | 5/20/2020 |
2.2.0-alpha0016 | 396 | 5/20/2020 |
2.2.0-alpha0015 | 352 | 5/19/2020 |
2.2.0-alpha0001 | 379 | 3/1/2020 |
2.1.3 | 1,579 | 8/19/2020 |
2.1.3-beta0001 | 356 | 8/19/2020 |
2.1.2 | 1,531 | 5/29/2020 |
2.1.2-beta0001 | 354 | 5/29/2020 |
2.1.1 | 875 | 3/1/2020 |
2.1.1-beta0001 | 382 | 3/1/2020 |
2.1.0 | 728 | 2/8/2020 |
2.1.0-beta0001 | 458 | 2/8/2020 |
2.1.0-alpha0004 | 460 | 2/8/2020 |
2.1.0-alpha0002 | 469 | 2/8/2020 |
2.0.0 | 772 | 1/14/2020 |
2.0.0-beta0001 | 381 | 1/14/2020 |
2.0.0-alpha0003 | 492 | 1/12/2020 |
1.1.0-alpha0002 | 395 | 11/18/2019 |
1.0.0 | 613 | 11/18/2019 |
1.0.0-beta0001 | 377 | 11/18/2019 |
0.3.0-alpha0011 | 384 | 11/9/2019 |
0.3.0-alpha0005 | 426 | 7/10/2019 |
0.3.0-alpha0002 | 412 | 7/6/2019 |
0.3.0-alpha0001 | 442 | 5/8/2019 |
0.2.0 | 2,131 | 2/5/2019 |
0.2.0-alpha0019 | 523 | 2/4/2019 |
0.2.0-alpha0018 | 530 | 2/4/2019 |