NserviceBus 属性注入
我正在尝试将一个对象注入到我的传奇中。使用以下端点,当消息到达 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
NServiceBus 会自动为您执行属性注入,因此您只需向容器注册存储库:
在您的 Init() 方法中:(在单独的类上实现 IWantCustomInitialization)
< 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: (ImplementIWantCustomInitialization
on a separate class)IWantToRunAtStartup
is not meant for configuration tasks (useIWantCustomInitialization
instead)