最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

c# - MassTransit central configuration of endpoint prefetchcount - Stack Overflow

programmeradmin1浏览0评论

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 badges
Add a comment  | 

1 Answer 1

Reset to default 1

You 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.

发布评论

评论列表(0)

  1. 暂无评论