Created
August 18, 2021 04:52
-
-
Save lomholdt/b3f3b448225543ddac7826dc1da71f69 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace DaprKafkaDemo.Shared; | |
public class ConfluentKafkaOptions | |
{ | |
public const string ConfluentKafka = "ConfluentKafka"; | |
public SchemaRegistry SchemaRegistry { get; set; } = new SchemaRegistry(); | |
} | |
public class SchemaRegistry | |
{ | |
public string Url { get; set; } = string.Empty; | |
public string Username { get; set; } = string.Empty; | |
public string Password { get; set; } = string.Empty; | |
public string BasicAuthUserInfo => $"{Username}:{Password}"; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Avro.Specific; | |
namespace DaprKafkaDemo.Shared; | |
public record ConfluentAvroEvent<TEvent>(TEvent Value) | |
where TEvent : ISpecificRecord; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Avro.Specific; | |
using Confluent.SchemaRegistry; | |
using Confluent.SchemaRegistry.Serdes; | |
using Microsoft.AspNetCore.Mvc.ModelBinding; | |
using Microsoft.AspNetCore.Mvc.ModelBinding.Binders; | |
namespace DaprKafkaDemo.Shared; | |
public class ConfluentEventBinder<TEvent> : IModelBinder where TEvent : ISpecificRecord | |
{ | |
private readonly ISchemaRegistryClient _registryClient; | |
public ConfluentEventBinder(ISchemaRegistryClient registryClient) | |
{ | |
_registryClient = registryClient ?? throw new ArgumentNullException(nameof(registryClient)); | |
} | |
public async Task BindModelAsync(ModelBindingContext bindingContext) | |
{ | |
if (bindingContext is null) | |
{ | |
throw new ArgumentNullException(nameof(bindingContext)); | |
} | |
var body = bindingContext.ActionContext.HttpContext.Request.Body; | |
var memoryStream = new MemoryStream(); | |
await body.CopyToAsync(memoryStream); | |
var byteArr = memoryStream.ToArray(); | |
var deserializer = new AvroDeserializer<TEvent>(_registryClient); | |
var @event = await deserializer.DeserializeAsync(byteArr, default, new()); | |
var confluentEvent = new ConfluentAvroEvent<TEvent>(@event); | |
bindingContext.Result = ModelBindingResult.Success(confluentEvent); | |
} | |
} | |
public class ConfluentEventBinderProvider : IModelBinderProvider | |
{ | |
public IModelBinder? GetBinder(ModelBinderProviderContext context) | |
{ | |
if (context is null) | |
{ | |
throw new ArgumentNullException(nameof(context)); | |
} | |
if (context.Metadata.ModelType.GetGenericTypeDefinition() == typeof(ConfluentAvroEvent<>)) | |
{ | |
Type[] types = context.Metadata.ModelType.GetGenericArguments(); | |
Type binderType = typeof(ConfluentEventBinder<>).MakeGenericType(types); | |
return new BinderTypeModelBinder(binderType); | |
} | |
return null; | |
} | |
} |
Author
lomholdt
commented
Aug 18, 2021
•
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment