MassTransit_契约的创建
消息契约的命名规范
命令模式:动词+对象
事件模式:对象+动词
不同模式下要求不同的主键
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 named
CorrelationId,CommandId, orEventIdthis header is automatically populated on Send or Publish. Otherwise, it can be manually specified using theSendContext. - RequestId
- When using the
RequestClient, or the request/response message handling of MassTransit, each request is assigned a uniqueRequestId. When the message is received by a consumer, the response message sent by theRespondmethod (on theConsumeContext) is assigned the sameRequestIdso 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
ConversationIdis 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
CorrelationIdof the message (if available, otherwise theMessageIdmay be used) is copied to theInitiatorIdheader. 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_契约的创建的更多相关文章
- MassTransit_消费者的创建
Creating a message consumer A message consumer is a class that consumes one or more message types, s ...
- [转载]我的WCF之旅(1):创建一个简单的WCF程序
为了使读者对基于WCF的编程模型有一个直观的映像,我将带领读者一步一步地创建一个完整的WCF应用.本应用功能虽然简单,但它涵盖了一个完整WCF应用的基本结构.对那些对WCF不是很了解的读者来说,这个例 ...
- WCF服务二:创建一个简单的WCF服务程序
在本例中,我们将实现一个简单的计算服务,提供基本的加.减.乘.除运算,通过客户端和服务端运行在同一台机器上的不同进程实现. 一.新建WCF服务 1.新建一个空白解决方案,解决方案名称为"WC ...
- 我的WCF之旅(1):创建一个简单的WCF程序
为了使读者对基于WCF的编程模型有一个直观的映像,我将带领读者一步一步地创建一个完整的WCF应用.本应用功能虽然简单,但它涵盖了一个完整WCF应用的基本结构.对那些对WCF不是很了解的读者来说,这个例 ...
- WCF技术剖析之十八:消息契约(Message Contract)和基于消息契约的序列化
原文:WCF技术剖析之十八:消息契约(Message Contract)和基于消息契约的序列化 [爱心链接:拯救一个25岁身患急性白血病的女孩[内有苏州电视台经济频道<天天山海经>为此录制 ...
- WCF 之 数据契约
前面几篇讲的都只能传递string类型的简单参数,数据契约就是用来解决如传递一个带有多个属性的Class类型的对象的. WCF推荐使用数据契约的方式实现数据的序列化.这部分的内容很好理解但是很重要,先 ...
- WCF学习笔记一
Windows Communication Foundation(WCF)是由微软开发的一系列支持数据通信的应用程序框架,可以翻译为Windows 通讯开发平台. 整合了原有的windows通讯的 . ...
- WCF会话(Session)与实例(Instance)管理
一.理解Session 1.Session的作用:保留Client和Service之间交互的状态,确保Client与Service之间交互唯一性(SessionId),即:多个Client同时访问Se ...
- 第一讲:WCF介绍
代码 https://yunpan.cn/cPns5DkGnRGNs 密码:3913 ...
随机推荐
- 转:DataTable的Compute方法的应用
转自:http://www.cnblogs.com/hfliyi/archive/2013/01/08/2851944.html 项目中遇到计算平均值.标准偏差.平均值+标准偏差.平均值+2倍标准偏差 ...
- GUI 快捷键的实现思路
思路: 前提快捷键操作不可重复,即一个快捷键对应一个控件的动作 一个窗体保持一份快捷键的map映射 在相应的消息中获取快捷键列表如键盘消息 在控件类对象中定义一个默认的响应行为,比如Button按 ...
- ASP.NET中GridView控件删除数据的两种方法
今天在用GridView控件时,发现了一个问题,就是使用GridView控件在删除数据时的问题.接下来我们通过模板列方式和CommandField方式删除某条数据讲解下两者之间的区别. 方式一:通 ...
- jdbc实现简单的增删改查
先是Book类. 略 然后一个主页,写一个表单,提交Book的信息到AddBook. 略 AddBook.jsp连接jdbc,并向Book表插入. <%@ page language=" ...
- atitit.eclipse 新特性总结3.1--4.3
atitit.eclipse 新特性总结3.1--4.3 Eclipse 3.1 1 Eclipse 3.2 Java开发工具的新特性 2 1. 内容辅助(Ctrl+Space)模板 2 2. 动态地 ...
- paip.提升效率--僵尸代码的迷思
paip.提升效率--僵尸代码的迷思 僵尸代码是指你的代码库里被注释掉的那部分代码, 很少去使用它,就像僵尸一样, 看雷kill-the-zombies-in-your-code ========== ...
- EntityFramework IEnumerable,IQueryable ,Include
使用IQueryable using (var db = new CentaStaffEntities()) { IQueryable<Staff> queryablestaffs = d ...
- iOS-OC内存管理
目标 1.[理解]内存管理 2.[掌握]第一个MRC程序 3.[掌握]内存管理的原则 4.[理解]野指针与僵尸对象 5.[理解]单个对象的内存管理 6.[理解]多个对象的内存管理 7.[掌握]set方 ...
- ES6转码器babel的使用
1. 进入ES6的项目,执行 npm init // 初始化package.json 2. 在与package.json同一目录下编写配置文件 .babelrc { "presets&quo ...
- 日常开发中常见的HTTP协议的状态码
301Moved Permanently请求的网页已永久移动到新位置.服务器返回此响应(对 GET 或 HEAD 请求的响应)时,会自动将申请人转到新位置.您应使用此代码告诉 Googlebot 某个 ...