I Have two files:
我有两个文件:
Program.cs:
程序.cs:
serviceSettings = configuration.GetSection(nameof(ServiceSettings)).Get<ServiceSettings>();
IServiceCollection services = new ServiceCollection();
services.AddMassTransit(x =>
{
x.UsingRabbitMq((context, configurator) =>
{
var rabbitMQSettings = builder.Configuration.GetSection(nameof(RabbitMQSettings)).Get<RabbitMQSettings>();
configurator.Host(rabbitMQSettings.Host);
configurator.ConfigureEndpoints(context, new KebabCaseEndpointNameFormatter(serviceSettings.ServiceName, false));
});
});
services.AddSingleton<IPublishEndpoint>(provider => provider.GetRequiredService<IBusControl>());
services.AddMassTransitHostedService();
and ItemsController.cs which is my controller-
和ItemsController.cs,这是我的控制器-
[ApiController]
[Route("items")]
public class ItemsController : ControllerBase
{
private readonly IRepository<Item> itemRepository;
private readonly IPublishEndpoint publishEndpoint;
private static int requestCounter = 0;
public ItemsController(IRepository<Item> itemRepository, IPublishEndpoint publishEndpoint)
{
this.publishEndpoint = publishEndpoint;
this.itemRepository = itemRepository;
}
[HttpPost]
public async Task<ActionResult<ItemDto>> CreateItemAsync(CreateItemDto createItemDto)
{
var item = new Item
{
Name = createItemDto.Name,
Description = createItemDto.Description,
Price = createItemDto.Price,
CreatedDate = DateTimeOffset.UtcNow
};
await itemRepository.CreateItemAsync(item);
await publishEndpoint.Publish(new CatalogItemCreated(item.Id, item.Name, item.Description));
return CreatedAtAction(nameof(CreateItemAsync), new { id = item.Id }, item);
}
But everytime I hit the route- https://localhost:7234/items which used to previously work(before setup of rabbitmq) is giving me this error-
但每次我上路https://localhost:7234/items以前(在设置rabbitmq之前)工作的方法给了我这个错误-
fail: Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware[1]
An unhandled exception has occurred while executing the request.
System.InvalidOperationException: Unable to resolve service for type 'MassTransit.IPublishEndpoint' while attempting to activate 'Play.Catalog.Service.Controllers.ItemsController'.
The App builds and run just fine but gives this error whenever I try any endpoints.
I also tried to change versions to 7.1.3 for MassTransit.RabbitMQ but to no avail.
该应用程序构建和运行都很好,但每当我尝试任何端点时都会出现此错误。我还尝试将MassTransit.RabbitMQ的版本更改为7.1.3,但没有成功。
更多回答
MassTransit is currently on v8.1, I'd suggest if you're starting out, you start there.
MassTransit目前在v8.1上,我建议如果你刚开始,就从那里开始。
我是一名优秀的程序员,十分优秀!