Getting Started

This guide will help you start using Solace Schema Registry Avro SERDES for .NET in your applications.

Installation

Install the NuGet package:

dotnet add package Solace.SchemaRegistry.Serdes.Avro

This package includes the Core package as a dependency.

Basic Usage

Creating an AvroSerializer

using Avro.Generic;
using Solace.SchemaRegistry.Serdes.Avro;
using Solace.SchemaRegistry.Serdes.Core.Resolver;

// Create the serializer
using var serializer = new AvroSerializer<GenericRecord>();

// Configure with Solace Schema Registry credentials
var config = new Dictionary<string, object>
{
    { SchemaResolverPropertyKeys.RegistryUrl, "https://your-registry.solace.cloud/apis/registry/v3" },
    { SchemaResolverPropertyKeys.AuthUsername, "your-username" },
    { SchemaResolverPropertyKeys.AuthPassword, "your-password" }
};

serializer.Configure(config);

Creating an AvroDeserializer

using Avro.Generic;
using Solace.SchemaRegistry.Serdes.Avro;
using Solace.SchemaRegistry.Serdes.Core.Resolver;

// Create the deserializer
using var deserializer = new AvroDeserializer<GenericRecord>();

// Configure with Solace Schema Registry credentials
var config = new Dictionary<string, object>
{
    { SchemaResolverPropertyKeys.RegistryUrl, "https://your-registry.solace.cloud/apis/registry/v3" },
    { SchemaResolverPropertyKeys.AuthUsername, "your-username" },
    { SchemaResolverPropertyKeys.AuthPassword, "your-password" }
};

deserializer.Configure(config);

Integration with Solace Messaging API for .NET

When using with the Solace Messaging API for .NET:

using System.IO;
using Avro;
using Avro.Generic;
using Solace.SchemaRegistry.Serdes.Avro;
using Solace.SchemaRegistry.Serdes.Core.Resolver;
using SolaceSystems.Solclient.Messaging;
using SolaceSystems.Solclient.Messaging.Serialization;
using Solace.Serdes;

// Create and configure Avro serializer and deserializer
using var deserializer = new AvroDeserializer<GenericRecord>();
using var serializer = new AvroSerializer<GenericRecord>();

var config = new Dictionary<string, object>
{
    { SchemaResolverPropertyKeys.RegistryUrl, "https://your-registry.solace.cloud/apis/registry/v3" },
    { SchemaResolverPropertyKeys.AuthUsername, "your-username" },
    { SchemaResolverPropertyKeys.AuthPassword, "your-password" }
};

deserializer.Configure(config);
serializer.Configure(config);

// Wrap async serializer/deserializer with synchronous adapters for use with the API's synchronous callbacks
var syncDeserializer = deserializer.AsSyncOverAsync();
var syncSerializer = serializer.AsSyncOverAsync();

// Create a session with the message handler
using ISession session = context.CreateSession(
    sessionProps,
    (source, msgArgs) => HandleMessage(source, msgArgs, syncDeserializer),
    null);

// Publishing a message
using (var message = ContextFactory.Instance.CreateMessage())
{
    message.Destination = topic;

    // Build a GenericRecord from the Avro schema
    var schema = (RecordSchema)Schema.Parse(File.ReadAllText("user.avsc"));
    var user = new GenericRecord(schema);
    user.Add("name", "Test");
    user.Add("id", 42);

    // Serialize the record to the message using Avro serialization
    message.Serialize(syncSerializer, user);
    session.Send(message);
}

// Receiving a message
void HandleMessage(object source, MessageEventArgs args, IDeserializer<GenericRecord> deserializer)
{
    // Deserialize the message payload using Avro schema validation
    GenericRecord data = args.Message.Deserialize(deserializer);
    Console.WriteLine($"Received: name={data["name"]}, id={data["id"]}");
}

Additional Examples

For complete working examples, including integration with Solace messaging, visit the Solace Samples for .NET repository.