NserviceBus属性注入 [英] NserviceBus property injection

查看:67
本文介绍了NserviceBus属性注入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将一个物体注入我的传奇中.使用以下端点,当消息到达saga的handle方法时,该属性为null.

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.

端点:

 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
    }

app.config

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>

和我的Saga接受消息如下

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

尝试调用SaveOrder()时将引发空引用期望.我是否正确配置了依赖项注入?

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

推荐答案

NServiceBus将自动为您进行属性注入,因此您只需要在容器中注册存储库:

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

在您的Init()方法中:(在单独的类上实现IWantCustomInitialization)

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

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

IWantToRunAtStartup不适用于配置任务(请改用IWantCustomInitialization)

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

这篇关于NserviceBus属性注入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆