NserviceBus property injection

末鹿安然 提交于 2020-01-04 02:14:06

问题


I am attempting to inject an object into my saga. Using the following endpoint, when the message arrives at the handle method of the saga the property is null.

The endpoint:

 public class EndpointConfig : IConfigureThisEndpoint, AsA_Server,  IWantToRunAtStartup
        {
            public void Run()
            {
                IOrderRepository orderRepository = new OrderRepository();
                Configure.Instance.Configurer.ConfigureProperty<CreateOrderSaga>(x => x.OrderRepository, orderRepository);
            }

// stop method removed
    }

The app.config

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="MsmqTransportConfig" type="NServiceBus.Config.MsmqTransportConfig, NServiceBus.Core" />
    <section name="UnicastBusConfig" type="NServiceBus.Config.UnicastBusConfig, NServiceBus.Core" />
  </configSections>

  <MsmqTransportConfig
    InputQueue="Fulfilment.CreateOrder.OrderRecievedMessage"
    ErrorQueue="error"
    NumberOfWorkerThreads="1"
    MaxRetries="3"
  />

  <UnicastBusConfig
   DistributorControlAddress=""
   DistributorDataAddress="">
    <MessageEndpointMappings>
      <add Messages="NServiceBus.Saga.TimeoutMessage, NServiceBus" Endpoint="timeoutmanager" />
    </MessageEndpointMappings>
  </UnicastBusConfig>
</configuration>

and my Saga accepting messages as follows

    public class CreateOrderSaga : Saga<CreateOrderSagaData>,
            IAmStartedByMessages<OrderRecievedMessage>,
            IHandleMessages<OrderCompletedMessage>,
            IHandleMessages<OrderCancelledMessage>
        {
            public IOrderRepository OrderRepository { get; set; }
            public void Handle(OrderRecievedMessage message)
            {
var order = new Order();
 OrderRepository.SaveOrder(order);
            }

a null reference expection will be thrown when attempting to call SaveOrder(). Have i configured the dependency injection correctly?


回答1:


NServiceBus will automatically do property injection for you so you only need to register your repository with the container:

In your Init() method: (Implement IWantCustomInitialization on a separate class)

Configure.Instance.ConfigureComponent< OrderRepository >([The lifecycle you want]);

IWantToRunAtStartup is not meant for configuration tasks (use IWantCustomInitialization instead)



来源:https://stackoverflow.com/questions/7007562/nservicebus-property-injection

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!