轨道交通.使用不同命名空间中定义的相同对象 [英] MassTransit. Consume equal objects defined in different namespaces

查看:37
本文介绍了轨道交通.使用不同命名空间中定义的相同对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,请原谅我的英语很糟糕.我将 MassTransit 与 Azure 服务总线一起用于微服务之间的异步通信.根据它们自己的定义,为了避免它们之间产生依赖关系,不同微服务之间发送的消息被定义在每个微服务中,即它们是不同命名空间的一部分.MassTransit 的自动管理导致队列和主题按对象类型进行管理,从而阻止使用消息的微服务接收微服务发布者发送的消息.同样的事情发生在同一个命名空间中具有相同属性但具有不同类名的两个类.

First of all, excuse my English, it's very bad. I am using MassTransit with Azure Service Bus for asynchronous communication between microservices. By their own definition, and to avoid generating dependencies between them, messages sent between different microservices are defined in each of them, that is, they are part of different namespaces. The automatic management of MassTransit causes queues and topics to be managed by the object type, which prevents the microservices that consume a message from receiving the messages sent by the microservice publisher. The same thing happens with two classes with the same properties in the same namespace but with a different class name.

有什么办法可以解决这个问题吗?我想到的选项是:

Is there any way to solve this? The options that have occurred to me are:

  • 从目标地址的端点移除命名空间,仅使用类名命名.
  • MassTransit 可以根据对象的序列化来管理队列和主题的创建,而不是根据对象类型来管理它(也许通过某种类型的包装对象?)

我留下了一个例子,希望可以帮助你理解问题.

I leave an example that I hope can help you in understanding the problem.

//FIRST PROGRAM - MESSAGE CONSUMER 

namespace Consumer
{
    public class Example
    {
        public string PropOne { get; set; }

        public string PropTwo { get; set; }
    }

    public class ExampleConsumer : 
        IConsumer<Example>
    {
        public List<Example> ConsumedTestObjectList { get; } = new List<Example>();

        //THIS METHOD NEVER CALL !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
        public Task Consume(ConsumeContext<ExampleConsumer> context)
        {
            ConsumedTestObjectList.Add(context.Message);
            return Task.CompletedTask;
        }
    }

    public class ConsumerProgram
    {
        public static void Main()
        {
            var bus = Bus.Factory.CreateUsingAzureServiceBus(sbc =>
            {
                var host = sbc.Host("connectionString", h => {});

            });

            sbc.ReceiveEndpoint(host, e =>
            {
                e.Consumer<ConsumerProgram.Example>(context =>
                {
                    return Console.Out.WriteLineAsync($"Message Received: {JsonConvert.SerializeObject(context.Message)}");
                });
            });

            bus.Start(); // This is important!

            Console.WriteLine("Press any key to exit");
            Console.ReadKey();

            bus.Stop();
        }
    }
}


//SECOND PROGRAM - MESSAGE PUBLISHER 

namespace Publisher
{
    public class Example
    {
        public string PropOne { get; set; }

        public string PropTwo { get; set; }
    }

    public class PublisherProgram
    {
        public static void Main()
        {
            var bus = Bus.Factory.CreateUsingAzureServiceBus(sbc =>
            {
                var host = sbc.Host("connectionString", h => {});

            });

            bus.Start(); // This is important!

            //send new instance of Publisher.Example 
            var example = new Example() { PropOne = "1", PropTwo = "2" };
            bus.Publish(example);

            Console.WriteLine("Press any key to exit");
            Console.ReadKey();

            bus.Stop();
        }
    }
}

非常感谢.

问候

博尔哈

推荐答案

消息类型和结果名称是 MassTransit 中的一个关键概念.如果您想避免在项目之间共享程序集,那很好,但是您需要匹配整个接口(或类,在您的情况下)名称,包括命名空间,否则将无法正确路由.

The message type, and the resulting name, are a key concept within MassTransit. If you want to avoid sharing assemblies between projects, that is fine, but you will need to match the entire interface (or class, in your case) name, including namespace, or it will not route properly.

是的,您可以覆盖实体名称格式化程序以更改主题的命名方式,但它不会更改消息反序列化的消息类型要求(按类型发生).

Yes, you can override the entity name formatter to change how topics are named but it won't change the message type requirement for deserialization of the message (which happens, by type).

所以这里的建议是为合约使用相同的命名空间,即使它们在不同的项目中.

So the recommendation here is to use the same namespace for the contracts, even if they're in separate projects.

这篇关于轨道交通.使用不同命名空间中定义的相同对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆