Create Formatter¶
Before creating publishers or subscribers, create an IDataFormatter instance.
This guide focuses on creating formatter implementations.
For implementation details, architecture and formatter comparisons, see the Formatters section.
Install A Formatter Package¶
Choose one formatter package.
Microsoft¶
dotnet add package ServiceBus.Formatters.Ms
Newtonsoft.Json¶
dotnet add package ServiceBus.Formatters.Newtonsoft
Only one formatter package is required.
Available Formatter Implementations¶
| Package | Formatter |
|---|---|
ServiceBus.Formatters.Ms |
MsJsonDataFormatter |
ServiceBus.Formatters.Newtonsoft |
RwJsonDataFormatter (recommended) |
ServiceBus.Formatters.Newtonsoft |
JsonDataFormatter |
Microsoft Formatter¶
Example:
var options = new JsonSerializerOptions
{
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
PropertyNameCaseInsensitive = true,
DictionaryKeyPolicy = JsonNamingPolicy.CamelCase,
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull
};
options.Converters.Add(
new JsonStringEnumConverter());
IDataFormatter formatter =
new MsJsonDataFormatter(options);
Newtonsoft Formatter (Recommended)¶
The recommended Newtonsoft implementation is:
RwJsonDataFormatter
Example:
var settings = new JsonSerializerSettings();
var serializer =
JsonSerializer.Create(settings);
IDataFormatter formatter =
new RwJsonDataFormatter(
serializer,
Encoding.UTF8);
Alternative Newtonsoft Formatter¶
The package also provides:
JsonDataFormatter
Example:
var settings =
new JsonSerializerSettings();
IDataFormatter formatter =
new JsonDataFormatter(
settings,
Encoding.UTF8);
Next Step¶
Once the formatter has been created, continue with one of the following guides:
- Create Publisher
- Create Subscriber
- Create REST Publisher
- Create REST Subscriber