消息契约的命名规范

命令模式:动词+对象

事件模式:对象+动词

不同模式下要求不同的主键

Creating a message contract

In MassTransit, a message contract is defined using the .NET type system. Messages can be defined using both classes and interfaces, however, it is suggested that types use read-only properties and no behavior.

Note

It is strongly suggested to use interfaces for message contracts, based on experience over several years with varying levels of developer experience. MassTransit will create dynamic interface implementations for the messages, ensuring a clean separation of the message contract from the consumer.

An example message to update a customer address is shown below.

namespace Company.Application.Contracts
{
using System; public interface UpdateCustomerAddress
{
Guid CommandId { get; }
DateTime Timestamp { get; }
string CustomerId { get; }
string HouseNumber { get; }
string Street { get; }
string City { get; }
string State { get; }
string PostalCode { get; }
}
}

A common mistake when engineers are new to messaging is to create a base class for messages, and try to dispatch that base class in the consumer – including the behavior of the subclass. Ouch. This always leads to pain and suffering, so just say no to base classes.

Specifying message names

There are two main message types, events and commands. When choosing a name for a message, the type of message should dictate the tense of the message.

Commands

A command tells a service to do something. Commands are sent (using Send) to an endpoint, as it is expected that a single service instance performs the command action. A command should never be published.

Commands should be expressed in a verb-noun sequence, following the tell style.

Example Commands:

  • UpdateCustomerAddress
  • UpgradeCustomerAccount
  • SubmitOrder

Events

An event signifies that something has happened. Events are published (using Publish) using eitherIBus or the ConsumeContext within a message consumer. An event should never be sent directly to an endpoint.

Events should be expressed in a noun-verb (past tense) sequence, indicating that something happened.

Example Events:

  • CustomerAddressUpdated
  • CustomerAccountUpgraded
  • OrderSubmitted, OrderAccepted, OrderRejected, OrderShipped

Correlating messages

There are several built-in message headers that can be used to correlate messages. However, it is also completely acceptable to add properties to the message contract for correlation. The default headers available include:

CorrelationId
An explicit correlation identifier for the message. If the message contract has a property namedCorrelationIdCommandId, or EventId this header is automatically populated on Send or Publish. Otherwise, it can be manually specified using the SendContext.
RequestId
When using the RequestClient, or the request/response message handling of MassTransit, each request is assigned a unique RequestId. When the message is received by a consumer, the response message sent by the Respond method (on the ConsumeContext) is assigned the sameRequestId so that it can be correlated by the request client. This header should not typically be set by the consumer, as it is handled automatically.
ConversationId
The conversation is created by the first message that is sent or published, in which no existing context is available (such as when a message is sent or published from a message consumer). If an existing context is used to send or publish a message, the ConversationId is copied to the new message, ensuring that a set of messages within the same conversation have the same identifier.
InitiatorId
When a message is created within the context of an existing message, such as in a consumer, a saga, etc., the CorrelationId of the message (if available, otherwise the MessageId may be used) is copied to the InitiatorId header. This makes it possible to combine a chain of messages into a graph of producers and consumers.
MessageId
When a message is sent or published, this header is automatically generated for the message.

MassTransit_契约的创建的更多相关文章

  1. MassTransit_消费者的创建

    Creating a message consumer A message consumer is a class that consumes one or more message types, s ...

  2. [转载]我的WCF之旅(1):创建一个简单的WCF程序

    为了使读者对基于WCF的编程模型有一个直观的映像,我将带领读者一步一步地创建一个完整的WCF应用.本应用功能虽然简单,但它涵盖了一个完整WCF应用的基本结构.对那些对WCF不是很了解的读者来说,这个例 ...

  3. WCF服务二:创建一个简单的WCF服务程序

    在本例中,我们将实现一个简单的计算服务,提供基本的加.减.乘.除运算,通过客户端和服务端运行在同一台机器上的不同进程实现. 一.新建WCF服务 1.新建一个空白解决方案,解决方案名称为"WC ...

  4. 我的WCF之旅(1):创建一个简单的WCF程序

    为了使读者对基于WCF的编程模型有一个直观的映像,我将带领读者一步一步地创建一个完整的WCF应用.本应用功能虽然简单,但它涵盖了一个完整WCF应用的基本结构.对那些对WCF不是很了解的读者来说,这个例 ...

  5. WCF技术剖析之十八:消息契约(Message Contract)和基于消息契约的序列化

    原文:WCF技术剖析之十八:消息契约(Message Contract)和基于消息契约的序列化 [爱心链接:拯救一个25岁身患急性白血病的女孩[内有苏州电视台经济频道<天天山海经>为此录制 ...

  6. WCF 之 数据契约

    前面几篇讲的都只能传递string类型的简单参数,数据契约就是用来解决如传递一个带有多个属性的Class类型的对象的. WCF推荐使用数据契约的方式实现数据的序列化.这部分的内容很好理解但是很重要,先 ...

  7. WCF学习笔记一

    Windows Communication Foundation(WCF)是由微软开发的一系列支持数据通信的应用程序框架,可以翻译为Windows 通讯开发平台. 整合了原有的windows通讯的 . ...

  8. WCF会话(Session)与实例(Instance)管理

    一.理解Session 1.Session的作用:保留Client和Service之间交互的状态,确保Client与Service之间交互唯一性(SessionId),即:多个Client同时访问Se ...

  9. 第一讲:WCF介绍

    代码 https://yunpan.cn/cPns5DkGnRGNs   密码:3913                                                         ...

随机推荐

  1. 体验CoreCLR的stack unwinding特性在Linux/Mac上的初步实现

    有了stack unwinding特性,才能在.NET程序中获取调用堆栈(call stack)信息,才能在异常时显示调用堆栈信息.这个特性之前只在Windows上有实现,Linux/Mac上的实现最 ...

  2. CoreCLR中超过3万行代码的gc.cpp文件的来源

    在CoreCLR的开源代码中,GC的主要实现代码gc.cpp文件大小竟然有1.17MB,打开文件一看,竟然有35490行!第一次见到如此多行的单个代码文件. github都不让直接查看:https:/ ...

  3. 修改注册表来修改IE的设置---资料汇总

    原文链接: http://blog.csdn.net/wangqiulin123456/article/details/17068649 附带批处理执行脚本: @echo off &title ...

  4. node-webkit教程(15)当图片加载失败的时候

    在node-webkit教程(14)禁用缓存中,简单讲了当前禁用缓存的几种方法. 在实际开发过程中,我遇到了一个因为缓存引起的诡异的问题.应用场景如下: 在一个编辑器里,不停的向画布上添加svg或者其 ...

  5. 浅谈压缩感知(二十八):压缩感知重构算法之广义正交匹配追踪(gOMP)

    主要内容: gOMP的算法流程 gOMP的MATLAB实现 一维信号的实验与结果 稀疏度K与重构成功概率关系的实验与结果 一.gOMP的算法流程 广义正交匹配追踪(Generalized OMP, g ...

  6. SVG的路径动画效果

    使用SVG animateMotion实现的一个动画路径效果,相关代码如下. 在线调试唯一地址:http://www.gbtags.com/gb/debug/c88f4099-5056-4ad7-af ...

  7. Atitit.java eval功能的实现  Compiler API

    Atitit.java eval功能的实现  Compiler API 输出echo2 输出目录配置2 针对编译器,JDK 设计了两个接口,分别是 JavaCompiler 和JavaCompiler ...

  8. Atitit. 解压缩zip文件 的实现最佳实践 java c# .net php

    Atitit. 解压缩zip文件 的实现最佳实践 java c# .net php 1. Jdk zip 跟apache ant zip 1 2. Apache Ant包进行ZIP文件压缩,upzip ...

  9. Atitit.java c++指针使用总结O7

    Atitit.java c++指针使用总结O7 1. 指针的本质 1 2. 指针的作用 1 1. 提升性能问题这常常用于遍历数组, 1 2. 计算两个指针的的距离 2 3. 避免栈溢出,创建动态数据结 ...

  10. paip.sqlite 管理最好的工具 SQLite Expert 最佳实践总结

    paip.sqlite 管理最好的工具 SQLite Expert 最佳实践总结 一般的管理工具斗可以...就是要是sqlite没正常地关闭哈,有shm跟wal文件..例如ff的place.sqlit ...