I want to set a value to a public boolean property in my NServicebus message handler through EndPoint config.

One way which I know is by creating

  1. An interface with that boolean property.
  2. A concrete class inherited from the interface.
  3. Initializing / Setting the value in that concrete class.
  4. Injecting the concrete class through End-point config.

Is there any other way to achieve this.

有帮助吗?

解决方案

Configure.Instance.Configurer
  .ConfigureProperty<YourHandlerType>(h => h.BoolProperty,
      Bool.Parse(ConfigurationManager.AppSettings["YourSetting"]));

In future versions, this will be even easier.

Edit:
You can try registering late with IWantToRunBeforeConfigurationIsFinalized, as suggested by David.

You can also register the component early. Since NServiceBus registers handlers as DependencyLifecycle.InstancePerUnitOfWork, your code should be doing the same

Configure.Component<YourHandlerType>(DependencyLifecycle.InstancePerUnitOfWork)
  .ConfigureProperty(h => h.BoolProperty,
      Bool.Parse(ConfigurationManager.AppSettings["YourSetting"]));
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top