Protobuf for ASP.NET Core | Web APIs
A lighter way to transfer data through http.
Supported Runtimes
- Csp.AspNetCore.Mvc.Protobuf
- .Net Core 3.1+
- Csp.Net.Http.Protobuf.Extensions
- .Net Standard 2.1+
Media Type
- application/x-protobuf
Runtime Installation
You can use the following command in the Package Manager Console:
For Server Side
Install-Package Csp.AspNetCore.Mvc.Protobuf
For Client Side
Install-Package Csp.Net.Http.Protobuf.Extensions
Package | Version | Downloads |
---|---|---|
Csp.AspNetCore.Mvc.Protobuf | ||
Csp.Net.Http.Protobuf.Extensions |
Sample Usage
Server Side
using Csp.AspNetCore.Mvc.Protobuf;
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers()
.AddProtobufSerializer(true); //true if use as default
}
Client Side
using Csp.Net.Http.Protobuf.Extensions;
...
var client = new HttpClient() { ... };
Get
var forecasts = await client.GetFromProtoAsync<WeatherForecast[]>("/weatherforecast");
Post
using var response = await client.PostAsProtoAsync("/weatherforecast", new WeatherForecast { ... });
using var stream = await response.Content.ReadAsStreamAsync();
var forecast = Serializer.Deserialize<WeatherForecast>(stream);