SchemaRegistryAvroObjectSerializer Fails To Serialize

LarryF 16 Reputation points
2020-11-10T14:54:23.577+00:00

I have attempted to recreate a simple Schema Registry sample based on the Microsoft.Azure.Data.SchemaRegistry.ApacheAvro sample. Unfortunately the serializer fails to serialize. I receive the following error: System.ArgumentException: 'Type Person is not supported for serialization operations.'

Any ideas?

Here is my schema from Azure Portal

{
    "type": "record",
    "name": "Person",
    "namespace": "PersonSchema",
    "fields": [
        {
            "name": "PersonId",
            "type": "int"
        },
        {
            "name": "PersonName",
            "type": "string"
        }
    ]
}

Here is my POCO class. I added the [Serializable] attribute but that did not help.

    [Serializable]
    public class Person
    {
        public int PersonId { get; set; }

        public string PersonName { get; set; }

        public Person()
        {

        }
    }

Here is the code that creates SchemaRegistryClient and serializes my Person class instance using SchemaRegistryAvroObjectSerializer

        internal void TestSchemaMethod()
        {
            var cred = new ClientSecretCredential("MY_TENANT_ID", "MY_CLIENT_ID", "MY_SECRET");
            var schemaRegistryClient = new SchemaRegistryClient("MYNAMESPACE.servicebus.windows.net", cred);
            using (var memoryStream = new MemoryStream())
            {
                var serializer = new SchemaRegistryAvroObjectSerializer(schemaRegistryClient, "TestDataSchema", new SchemaRegistryAvroObjectSerializerOptions { AutoRegisterSchemas = true });
                var id = 1;
                var person = new Person() { PersonId = id, PersonName = $"SomeName{id}" };
                serializer.Serialize(memoryStream, person, typeof(Person), CancellationToken.None);
            }
        }
Azure Event Hubs
Azure Event Hubs
An Azure real-time data ingestion service.
591 questions
{count} votes

1 answer

Sort by: Most helpful
  1. MartinJaffer-MSFT 26,051 Reputation points
    2020-11-12T23:05:09.73+00:00

    I think I am very close. I believe the issue is, the serializer only supports classes derived from GenericRecord or ISpecificRecord.
    Once I changed public class Person to public class Person : Avro.Specific.ISpecificRecord or some variant thereof, the error messages changed. Chasing down those I had to implement the other methods found in the example class.

    Right now I am getting a schema validation failed, but this does get past the "not supported" error. My code attatched.
    39407-program.txt

    Thank you for your patience. This is my first time working with the schema registry.