【发布时间】:2020-11-11 13:51:36
【问题描述】:
我是 MassTransit 和 Mediator 的新手,我有一系列事件要按顺序执行,我正在使用 MassTransit进程内和内存中,对于我的用例,不需要传输。
我想通过Mediator向消费者、sagas、activity发送和发布消息,我有下面的代码,但我想通过在startup.cs注册MassTransit来改进它:
//asp net core 3.1 Controller
[ApiController]
public class MyController : ControllerBase
{
private readonly IProductService _productService ;
private readonly IMediator mediator;
public MyController(IProductService productService)
{
_productService = productService;
var repository = new InMemorySagaRepository<ApiSaga>();
mediator = Bus.Factory.CreateMediator(cfg =>
{
cfg.Saga<ProductSaga>(repository);
});
}
[HttpPost]
public async Task<IActionResult> Post([FromBody] ProductContract productContract)
{
try
{
var result = await _productService.DoSomeThingAsync(productContract);
await mediator.Publish<ProductSubmittedEvent>(new { CorrelationId = Guid.NewGuid(), result.Label });
return Ok();
}
catch (Exception ex)
{
return BadRequest(ex.Message);
}
}
}
//My saga
public class ProductSaga :
ISaga,
InitiatedBy<ProductSubmittedEvent>
{
public Guid CorrelationId { get; set; }
public string State { get; private set; } = "Not Started";
public Task Consume(ConsumeContext<ProductSubmittedEvent> context)
{
var label= context.Message.Label;
State = "AwaitingForNextStep";
//...
//send next command
}
}
像这样它可以工作,但它不正确,我想在我的startup.cs 中使用 Mediator 配置 masstransit 以获得一个正确的实例,为此我首先删除了 IMediator,使用 IPublishEndpoint 发布消息到Saga 并配置我的startup.cs,但它没有按预期工作:
//startup.cs
public void ConfigureServices(IServiceCollection services)
{
services.AddMediator(cfg =>
{
cfg.AddSaga<ProductSaga>().InMemoryRepository();
});
}
//in controller using:
private readonly IPublishEndpoint _publishEndpoint;
//then
await _publishEndpoint.Publish<ProductSubmittedEvent>(
new { CorrelationId = Guid.NewGuid(), result.Label });
我收到了System.InvalidOperationException:
在尝试激活“GaaS.API.Controllers.ManageApiController”时无法解析“MassTransit.IPublishEndpoint”类型的服务。
我尝试更新我的startup.cs:
var repository = new InMemorySagaRepository<ApiSaga>();
services.AddMassTransit(cfg =>
{
cfg.AddBus(provider =>
{
return Bus.Factory.CreateMediator(x =>
{
x.Saga<ProductSaga>(repository);
});
});
});
我明白了:
无法将类型“MassTransit.Mediator.IMediator”隐式转换为“MassTransit.IBusControl”。
如果你有任何推荐想法感谢分享和挑战我????
【问题讨论】:
标签: c# dependency-injection masstransit