.NET Core 2.2 RawRabbit序列化程序/依赖项注入问题

时间:2019-04-24 19:07:41

标签: asp.net-core-2.2 rawrabbit

我有基于.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(动作pipeCfg,动作contextCfg,CancellationToken令牌)   ActivitiesController.cs中的Actio.Api.Controllers.ActivitiesController.Post(CreateActivity模型)

     

后跟上面显示的我的Post操作代码。

以下操作将按预期进行:

    [HttpGet]
    public IActionResult Get()
    {
        return Content("Hello from Actio API!");
    }

我认为是因为此操作未使用IBusClient。因此,问题必须出在RawRabbit上。我在问题上进行了搜索,并在RawRabbit GitHub存储库上发现了一个问题。解决方案是在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上下文不包含UseConsumerConfiguration的定义

其他信息:我正在关注Packt Publishing的.NET微服务课程。完全相同的程序包对于他们来说,这段代码似乎很好用。

2 个答案:

答案 0 :(得分:3)

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

  1. RawRabbit 2.0.0-rc5(撰写本文时为最新)。包括Prelease版本。
  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>()))));

答案 1 :(得分:-1)

将RawRabbit版本更新为2.0.0-rc5

之后,使用UseSubscribeConfiguration代替UseConsumerConfiguration