1

我有基于 .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 微服务课程。使用完全相同的包,这段代码似乎对他们来说工作得很好。

4

2 回答 2

4

对于未来的任何人,您必须执行以下操作:

  1. RawRabbit 2.0.0-rc5(撰写本文时最新)。包括预发布版本。
  2. 更改 UseConsumerConfiguration -> UseSubscribeConfiguration
  3. 安装 RawRabbit.Operations.Subscribe 因为 SubscribeAsync 将不再被识别

最终输出应如下所示:

public static Task WithCommandHandlerAsync<TCommand>(this IBusClient bus,
            ICommandHandler<TCommand> handler) where TCommand : ICommand
            => bus.SubscribeAsync<TCommand>(msg => handler.HandleAsync(msg),
                ctx => ctx.UseSubscribeConfiguration(cfg => 
                    cfg.FromDeclaredQueue(q => q.WithName(GetQueueName<TCommand>()))));
于 2020-03-15T23:37:40.377 回答
0

将 RawRabbit 版本更新为 2.0.0-rc5

之后使用 UseSubscribeConfiguration 而不是 UseConsumerConfiguration

于 2019-06-14T21:05:31.947 回答