Search code examples
c#nservicebus

Define NServiceBus instance-mapping in code


I'm writing an asp.net core application and wanna send a message to an NServiceBus endpoint.

As we have different environments I need to configure an endpoint address for each environment. I do that in the app settings.Env.json.

I like to do the same with the instance mappings. The only ways I know is to have a different instance-mapping.xml file for every environment or add it to the app.config that I don't have. Is there a way to set the instance machine in code? I don't wanna have different XML files.

I use NServiceBus 6.3.4


Solution

  • I added a feature to the endpoint configuration:

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

    Check docs here and here