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
, orEventId
this 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 theRespond
method (on theConsumeContext
) 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 theMessageId
may be used) is copied to theInitiatorId
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_契约的创建的更多相关文章
- 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 ...
随机推荐
- SQLite主键自增需要设置为integer PRIMARY KEY
按照正常的SQL语句,创建一个数据表,并设置主键是这样的语句: ), EventType )) 但使用这种办法,在SQLite中创建的的数据表,如果使用Insert语句插入记录,如下语句: INSER ...
- C#设计模式(19)——状态者模式(State Pattern)
一.引言 在上一篇文章介绍到可以使用状态者模式和观察者模式来解决中介者模式存在的问题,在本文中将首先通过一个银行账户的例子来解释状态者模式,通过这个例子使大家可以对状态者模式有一个清楚的认识,接着,再 ...
- MVC4+WebApi+Redis Session共享练习(下)
上一篇文章我们主要讲解了一些webApi和redis缓存操作,这篇文章我们主要说一些MVC相关的知识(过滤器和错误处理),及采用ajax调用webApi服务. 本篇例子采用的开发环境为:VS2010( ...
- solr与.net系列课程(一)solr的安装与配置
不久之前开发了一个项目,需要用到solr,因为所以在开始再网上查找资料,但是发现大部分的资料都是很片面的,要么就是只讲解solr如何安装的,要么就是只讲解solr的某一个部分的,而且很多都是资料都是一 ...
- Url转Link的C#正则表达式
网上关于Url转链接(href)的正则表达式一搜一大堆,但真正好用的没几个. 后来在Matthew O'Riordan的Blog上发现一个很好用的正则表达式,是用Javascript写的,代码如下: ...
- C# 通过SerialPort简单调用串口
问题 最近比较经常使用串口进行发送以及传输数据,但是笔者在刚开始接触SerialPort类时,对于Write之后去Read数据的时候,由于设备上面还没有返回数据,读取到的只能是空值.然而,再进行下一次 ...
- centos6.4 64位下安装nfs文件共享系统
不知道谁装的服务器,默认自带,以下内容摘自互联网,配置部分按教程执行成功 一.环境介绍: 服务器:centos 192.168.1.225 客户端:centos 192.168.1.226 二.安装: ...
- Atitit..组件化事件化的编程模型--(2)---------Web datagridview 服务器端控件的实现原理and总结
Atitit..组件化事件化的编程模型--(2)---------Web datagridview 服务器端控件的实现原理and总结 1. 服务端table控件的几个流程周期 1 1.1. 确认要显示 ...
- Activemq 平台搭建与C#示列
ActiveMQ ActiveMQ 是Apache出品,最流行的,能力强劲的开源消息总线.ActiveMQ 是一个完全支持JMS1.1和J2EE 1.4规范的 JMS Provider实现,尽管JMS ...
- HDU 1711 Number Sequence (KMP)
白书说这个是MP,没有对f 数组优化过,所以说KMP有点不准确 #include <stdio.h> int a,b; int T[1000010],P[10010];//从0开始存 in ...