We are looking for a way in MassTransit to configure the prefetch count for all endpoints with a few lines of code while still being able to configure a different setting per endpoint using .NET configuration and the name of the consumer for the configuration key.
We now have something like below, which uses reflection, but would like to improve and simplify this code.
services.AddMassTransit(x =>
{
var endpointSettingServices = x.Where(x =>
x.Lifetime == ServiceLifetime.Singleton
&& x.ImplementationInstance is not null
&& x.ServiceType.IsGenericType
&& x.ServiceType.GetGenericTypeDefinition() == typeof(IEndpointSettings<>)).ToList();
foreach (var service in endpointSettingServices)
{
var setting = service.ImplementationInstance;
var consumerType = service.ServiceType.GetGenericArguments()[0].GetGenericArguments()[0];
var consumerName = consumerType.Name;
var prefetchCount = configuration.GetValue<int?>($"EndpointSettings:{consumerName}:PrefetchCount", null);
if (prefetchCount.HasValue)
{
setting.GetType().GetProperty(nameof(EndpointSettings<object>.PrefetchCount)).SetValue(setting, prefetchCount.Value);
}
}
});
So we are wondering how we could do this in a more clean and simple way.
We are looking for a way in MassTransit to configure the prefetch count for all endpoints with a few lines of code while still being able to configure a different setting per endpoint using .NET configuration and the name of the consumer for the configuration key.
We now have something like below, which uses reflection, but would like to improve and simplify this code.
services.AddMassTransit(x =>
{
var endpointSettingServices = x.Where(x =>
x.Lifetime == ServiceLifetime.Singleton
&& x.ImplementationInstance is not null
&& x.ServiceType.IsGenericType
&& x.ServiceType.GetGenericTypeDefinition() == typeof(IEndpointSettings<>)).ToList();
foreach (var service in endpointSettingServices)
{
var setting = service.ImplementationInstance;
var consumerType = service.ServiceType.GetGenericArguments()[0].GetGenericArguments()[0];
var consumerName = consumerType.Name;
var prefetchCount = configuration.GetValue<int?>($"EndpointSettings:{consumerName}:PrefetchCount", null);
if (prefetchCount.HasValue)
{
setting.GetType().GetProperty(nameof(EndpointSettings<object>.PrefetchCount)).SetValue(setting, prefetchCount.Value);
}
}
});
So we are wondering how we could do this in a more clean and simple way.
Share Improve this question asked Feb 7 at 9:39 sdecsdec 1832 silver badges6 bronze badges1 Answer
Reset to default 1You can set the PrefetchCount
on the bus and that setting will be used as the default for all receive endpoints. Then you may override that setting on individual receive endpoints.