在代码中定义NServiceBus实例映射

时间:2017-07-13 12:25:01

标签: c# nservicebus

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

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

我喜欢对实例映射执行相同操作。我知道的唯一方法是为每个环境提供不同的instance-mapping.xml文件,或将其添加到我没有的app.config中。有没有办法在代码中设置实例机器?我不想拥有不同的XML文件。

我使用NServiceBus 6.3.4

1 个答案:

答案 0 :(得分:1)

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

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")
            });
    }
}

检查文档herehere