Avro SerializationException: Type is not supported by the resolver
Today in this article, we will cover below aspects,
Issue Description
Avro schema serialization or conversion produces the below error,
System.Runtime.Serialization.SerializationException: 'Type 'AvrosampleNetCore.AccountDetails' is not supported by the resolver.'
I was using AvroSerializer using Microsoft.Hadoop.Avro2 .
Resolution
I came across this issue while converting a type to create Avro schema details.
Below is the type used for the conversion,
class AccountDetails
{
public int AccountId { get; set; }
public string AccountName { get; set; }
[NullableSchema]
public SubAccounts[] Accounts { get; set; }
}
public class SubAccounts
{
public int AccountId { get; set; }
[NullableSchema]
public string AccountType { get; set; }
}
I was using AvroSerializer using Microsoft.Hadoop.Avro2
var result = AvroSerializer.Create<AccountDetails>(settings).WriterSchema.ToString();
I was able to fix the issue using any of the below approaches.
Approach I – Specify schema resolver
I was able to resolve the issue by specifying AvroSerializerSettings using AvroPublicMemberContractResolver while creating the schema.
AvroSerializerSettings settings = new AvroSerializerSettings();
settings.Resolver = new AvroPublicMemberContractResolver();
var result = AvroSerializer.Create<AccountDetails>
(settings).WriterSchema.ToString();
This option does not require you to specify the [DataContract] or [DataMember] attribute to your class definition.
After executing the above I was able to resolve the error successfully.
Approach II – Specify attributes [DataContract] and [DataMember]
I was also able to resolve the issue by specifying the [DataContract] or [DataMember] attribute to the class definition.
[DataContract]
class AccountDetails
{
[DataMember]
public int AccountId { get; set; }
[DataMember]
public string AccountName { get; set; }
[DataMember]
[NullableSchema]
public SubAccounts[] Accounts { get; set; }
}
[DataContract]
public class SubAccounts
{
[DataMember]
public int AccountId { get; set; }
[NullableSchema]
[DataMember]
public string AccountType { get; set; }
}
The below code generates the schema successfully,
var result = AvroSerializer.Create<AccountDetails>().WriterSchema.ToString();
Did I miss anything else in these resolution steps?
Did the above steps resolve your issue? Please sound off your comments below!
Happy Coding !!
References:
Please bookmark this page and share it with your friends. Please Subscribe to the blog to receive notifications on freshly published(2024) best practices and guidelines for software design and development.