У нас теперь есть что -то вроде ниже, которое использует отражение, но хотела бы улучшить и упростить этот код. < /p>
Код: Выделить всё
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($"EndpointSettings:{consumerName}:PrefetchCount", null);
if (prefetchCount.HasValue)
{
setting.GetType().GetProperty(nameof(EndpointSettings.PrefetchCount)).SetValue(setting, prefetchCount.Value);
}
}
});
Подробнее здесь: https://stackoverflow.com/questions/794 ... fetchcount
Мобильная версия