我有基于 .NET Core 2.2 的微服务。我使用 RawRabbit(版本 2.0.0-beta9)作为服务总线。与它一起安装了以下软件包:
<PackageReference Include="RawRabbit" Version="2.0.0-beta9" />
<PackageReference Include="RawRabbit.DependencyInjection.ServiceCollection" Version="2.0.0-beta9" />
<PackageReference Include="RawRabbit.Operations.Publish" Version="2.0.0-beta9" />
<PackageReference Include="RawRabbit.Operations.Subscribe" Version="2.0.0-beta9" />
这是我的控制器的样子:
private readonly IBusClient _busClient;
//...constructor that inits the _busClient
[HttpPost("")]
public async Task<IActionResult> Post([FromBody] CreateActivity model)
{
model.Id = Guid.NewGuid();
await _busClient.PublishAsync(model); //Exception thrown here
return Accepted($"Activities/{model.Name}");
}
当代码尝试执行以下操作时会出现问题:
await _busClient.PublishAsync(model);
我得到的例外是:
MissingMethodException:找不到方法:'无效 Newtonsoft.Json.JsonSerializer.set_TypeNameAssemblyFormat(System.Runtime.Serialization.Formatters.FormatterAssemblyStyle)'。RawRabbit.DependencyInjection.RawRabbitDependencyRegisterExtension+<>c.b__0_1(IDependencyResolver 解析器)
....... 更多文字.......
RawRabbit.BusClient.InvokeAsync(Action pipeCfg, Action contextCfg, CancellationToken token) Actio.Api.Controllers.ActivitiesController.Post(CreateActivity model) in ActivitiesController.cs
然后是我的 Post 操作的代码,如上所示。
以下操作按预期工作:
[HttpGet]
public IActionResult Get()
{
return Content("Hello from Actio API!");
}
我认为这是因为此操作不使用IBusClient. 所以,问题必须出在 RawRabbit 上。我用谷歌搜索了这个问题,并在 RawRabbit GitHub repo 上发现了一个问题。解决方案是在 RawRabbit 上升级到更新版本。所以,我尝试升级到2.0.0-rc1但我遇到了一些语法错误。我定义了一个Extensions定义以下方法的类:
public static Task WithCommandHandlerAsync<TCommand>(this IBusClient bus,
ICommandHandler<TCommand> handler) where TCommand: ICommand
=> bus.SubscribeAsync<TCommand>(msg => handler.HandleAsync(msg),
ctx => ctx.UseConsumerConfiguration(cfg =>
cfg.FromDeclaredQueue(q => q.WithName(GetQueueName<TCommand>()))));
问题似乎与UseConsumerConfiguration. 错误说:
ISubscribe Context 不包含 UseConsumerConfiguration 的定义
附加信息:我正在关注 Packt Publishing 的 .NET 微服务课程。使用完全相同的包,这段代码似乎对他们来说工作得很好。