NserviceBus 属性注入

发布于 2024-11-28 16:52:35 字数 1937 浏览 1 评论 0原文

我正在尝试将一个对象注入到我的传奇中。使用以下端点,当消息到达 saga 的 handler 方法时,属性为 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

<?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 接受如下消息,

    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() 时,将抛出空引用期望。我是否正确配置了依赖注入?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

只有一腔孤勇 2024-12-05 16:52:35

NServiceBus 会自动为您执行属性注入,因此您只需向容器注册存储库:

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

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

< code>IWantToRunAtStartup 不适用于配置任务(请改用 IWantCustomInitialization

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)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文