如何以编程方式将映射添加到 UnicastBusConfig?

发布于 2024-10-27 14:06:11 字数 594 浏览 1 评论 0 原文

我希望能够在运行时向我的应用程序添加对可能位于不同服务器上的其他消息类型的订阅。我想要实现的是一个监控应用程序,我可以在运行时添加/删除订阅。这可能吗?如何获取当前 UnicastBus 映射的引用?

这是我到目前为止正在做的事情,但我相信这将覆盖当前存在的任何映射。

MessageEndpointMappingCollection mappings = new MessageEndpointMappingCollection();
mappings.Add(new MessageEndpointMapping()
{
    Messages = m.MessageType.ToString(),
    Endpoint = m.QueueName
});

IComponentConfig<UnicastBusConfig> busConfig = config.Configurer.ConfigureComponent<UnicastBusConfig>(ComponentCallModelEnum.None);
busConfig.ConfigureProperty(u => u.MessageEndpointMappings, mappings);

I would like to be able to add subscriptions to additional message types living on potentially different servers to my application at runtime. What I'm trying to achieve is a Monitoring application where I can add/remove subscriptions at runtime. Is this possible? How do I get a reference to the current UnicastBus mappings?

Here is what I'm doing so far, but I believe this will overwrite any mappings currently in existence.

MessageEndpointMappingCollection mappings = new MessageEndpointMappingCollection();
mappings.Add(new MessageEndpointMapping()
{
    Messages = m.MessageType.ToString(),
    Endpoint = m.QueueName
});

IComponentConfig<UnicastBusConfig> busConfig = config.Configurer.ConfigureComponent<UnicastBusConfig>(ComponentCallModelEnum.None);
busConfig.ConfigureProperty(u => u.MessageEndpointMappings, mappings);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

°如果伤别离去 2024-11-03 14:06:11

Yves 在他的 Azure 示例中使用了此代码(可在 NSB 示例集合中找到)

using NServiceBus.Config;
using NServiceBus.Config.ConfigurationSource;

namespace OrderService
{
    class ConfigOverride : IProvideConfiguration<UnicastBusConfig>
    {
        public UnicastBusConfig GetConfiguration()
        {
            return new UnicastBusConfig
            {
                MessageEndpointMappings = new MessageEndpointMappingCollection
                {
                    new MessageEndpointMapping { Messages="MyMessages", Endpoint="orderserviceinputqueue" }
                }
            };
        }
    }
}

Yves used this code in his Azure samples (to be found in NSB samples collection)

using NServiceBus.Config;
using NServiceBus.Config.ConfigurationSource;

namespace OrderService
{
    class ConfigOverride : IProvideConfiguration<UnicastBusConfig>
    {
        public UnicastBusConfig GetConfiguration()
        {
            return new UnicastBusConfig
            {
                MessageEndpointMappings = new MessageEndpointMappingCollection
                {
                    new MessageEndpointMapping { Messages="MyMessages", Endpoint="orderserviceinputqueue" }
                }
            };
        }
    }
}
凯凯我们等你回来 2024-11-03 14:06:11

解决此问题的最佳方法是实现 IConfigurationSource 并提供您自己的配置。然后,您可以挑选您想要从配置文件中加载的内容(如果有的话)以及您想要在运行时自己指定的内容。

我会反映 DefaultConfigurationSource 类或参考此要点获取指导。

The best way to approach this would be to implement IConfigurationSource and provide your own configuration. Then you could cherry pick what you would like to load from the config file (if anything) and what you would like to specify yourself at runtime.

I would reflect the DefaultConfigurationSource class or refer to this gist for guidance.

苍暮颜 2024-11-03 14:06:11

在我目前参与的一个项目中,我们正在通过在我们自己的路由表中跟踪动态订阅/取消订阅代理来对它们进行一些基于内容的路由。

我们将 IBus 包装在装饰器中(通过使用 Windsor 对装饰器的支持,如 此处),查看消息是否实现了特殊的IRoutableMessage接口,该接口允许装饰器通过<代码>bus.Send(destinationEndpoint)。

要做到这一点有点复杂,我建议尽可能使用 NServiceBus 的内置路由。但是是否可以将消息显式路由到任何端点。

In a project, I am currently involved with, we are doing some content-based routing to dynamically subscribed/unsubscribed agents by keeping track of them in our own routing table.

We have wrapped the IBus in a decorator (by using Windsor's support of decorators, as described <plug>here</plug>), that sees if the message implements a special IRoutableMessage interface, that allows the decorator to route the message by explicitly specifying the destination endpoint via bus.Send(destinationEndpoint).

This was a little bit complex to get right, and I would recommend going with NServiceBus' built-in routing as far as possible. But is is possible to explicitly route messages to any endpoint.

待天淡蓝洁白时 2024-11-03 14:06:11

如果您正在研究监控,请查看 NSBManager 存储库。这采用相反的方法,让端点向管理器注册。

If you are looking into monitoring, check out the NSBManager repository. This takes the opposite approach and lets the endpoints register with the manager.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文