在代码中定义 NServiceBus 实例映射

Define NServiceBus instance-mapping in code

我正在编写 asp.net 核心应用程序并想向 NServiceBus 端点发送消息。

由于我们有不同的环境,我需要为每个环境配置一个端点地址。我在应用程序 settings.Env.json 中这样做。

我喜欢对实例映射做同样的事情。我知道的唯一方法是为每个环境使用不同的 instance-mapping.xml 文件,或者将其添加到我没有的 app.config 中。有没有办法在代码中设置实例机器?我不想有不同的 XML 个文件。

我使用 NServiceBus 6.3.4

我在端点配置中添加了一项功能:

endpointConfiguration.EnableFeature<MyFeature>();

public class MyFeature : Feature
{
    protected override void Setup(FeatureConfigurationContext context)
    {
        var endpointInstances = context.Settings.Get<EndpointInstances>();
        endpointInstances.AddOrReplaceInstances("InstanceMapping",
            new List<EndpointInstance>
            {
                new EndpointInstance("MyEndpoint").AtMachine("VM-1")
            });
    }
}

查看文档here and here