here it is how I am publishing to an object to event grid. I want to be able to use the azure service bus to listen to it.
public void Publicar<T>(T model, string operation, string entity)
{
_nomeEvento = entity + operation;
Boolean.TryParse(Configuration["EventGridConfig:Enabled"], out var eventGridIsActive);
if (!eventGridIsActive)
return;
var primaryTopicKey = Configuration["EventGridConfig:AcessKey"];
var primaryTopic = Configuration["EventGridConfig:Endpoint"];
var primaryTopicHostname = new Uri(primaryTopic).Host;
var topicCredentials = new TopicCredentials(primaryTopicKey);
var client = new EventGridClient(topicCredentials);
client.PublishEventsAsync(primaryTopicHostname, GetEventsList(model)).GetAwaiter().GetResult();
}
private List<EventGridEvent> GetEventsList<T>(T model)
{
return new List<EventGridEvent>
{
new EventGridEvent()
{
Id = Guid.NewGuid().ToString(),
EventType = _nomeEvento,
Data = model,
EventTime = DateTime.Now,
Subject = "MS_Clientes",
DataVersion = "1.0",
}
};
}
here it is how i am connection to the service bus
static class CustomExtensionsMethods
{
public static IServiceCollection AddBus(this IServiceCollection services, IConfiguration configuration,
IHostingEnvironment env)
{
services.AddMassTransit(x => { x.AddConsumer<NomeEmailChangeConsumer>(); });
services.AddSingleton(provider => Bus.Factory.CreateUsingAzureServiceBus(cfg =>
{
var keyName = "RootManageSharedAccessKey";
var busName = configuration["ServiceBus:Name"];
var secret = configuration["ServiceBus:Secret"];
var host = cfg.Host(
"Endpoint=sb://" + busName + ".servicebus.windows.net/;" +
"SharedAccessKeyName=" + keyName + ";" +
"SharedAccessKey=" + secret,
z =>
{
TokenProvider
.CreateSharedAccessSignatureTokenProvider(keyName, secret);
});
cfg.ConfigureJsonSerializer(settings =>
{
settings.Converters.Add(new InterfaceConverter());
return settings;
});
cfg.UseExtensionsLogging(provider.GetService<ILoggerFactory>());
cfg.ReceiveEndpoint(host, configuration["ServiceBus:Topic"],
e => { e.Consumer<NomeEmailChangeConsumer>(provider); });
}));
services.AddSingleton<IPublishEndpoint>(provider => provider.GetRequiredService<IBusControl>());
services.AddSingleton<ISendEndpointProvider>(provider => provider.GetRequiredService<IBusControl>());
services.AddSingleton<IBus>(provider => provider.GetRequiredService<IBusControl>());
services.AddScoped(provider => provider.GetRequiredService<IBus>().CreateRequestClient<NomeEmailChange>());
services.AddSingleton<IHostedService, BusService>();
return services;
}
}
but then I get the same error
fail: MassTransit.Messages[0]
R-FAULT sb://dev.servicebus.windows.net/bff-queue 9ade19ec-238c-4c08-8e03-28bac695ea7b No deserializer was registered for the message content type: application/json; charset=utf-8. Supported content types include application/vnd.masstransit+json, application/vnd.masstransit+bson, application/vnd.masstransit+xml
System.Runtime.Serialization.SerializationException: No deserializer was registered for the message content type: application/json; charset=utf-8. Supported content types include application/vnd.masstransit+json, application/vnd.masstransit+bson, application/vnd.masstransit+xml
at MassTransit.Serialization.SupportedMessageDeserializers.Deserialize(ReceiveContext receiveContext)
at MassTransit.Pipeline.Filters.DeserializeFilter.Send(ReceiveContext context, IPipe`1 next)
at GreenPipes.Filters.RescueFilter`2.GreenPipes.IFilter<TContext>.Send(TContext context, IPipe`1 next)
I have tried to add a JsonConverter I found online, but no luck
public class InterfaceConverter : JsonConverter
{
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
serializer.Serialize(writer, value);
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue,
JsonSerializer serializer)
{
// Set TypeNameHandling to Auto for deserializing objects with $type
// Should be set directly in ConfigureJsonDeserializer when setting up MT Service bus
serializer.TypeNameHandling = TypeNameHandling.Auto;
return serializer.Deserialize(reader);
}
public override bool CanConvert(Type objectType)
{
return objectType.IsInterface;
}
}