Class MessageNameAttribute
Declares the stable name a message is known by, independent of the type's namespace, assembly and version.
[AttributeUsage(AttributeTargets.Class|AttributeTargets.Struct, AllowMultiple = false, Inherited = false)]
public sealed class MessageNameAttribute : Attribute
- Inheritance
-
MessageNameAttribute
- Inherited Members
- Extension Methods
Examples
[MessageName("customer-created")]
public sealed record CustomerCreated(Guid CustomerId, string Name) : IDomainEvent;
Remarks
A message's stored identity is declared here and nowhere else. It is required: a name derived from the type would embed its namespace, assembly and assembly version, so moving the type or shipping a new version would change the identity of data already written and make it unreadable.
The name is chosen once and is permanent. Give it a publisher prefix and a bounded context --
"Contoso.Sales.CustomerCreated" -- so it is unique across every package a consumer might
combine, and says who defines the event's meaning. It owes nothing to the code layout, so the type
stays free to move. This is the shape Azure Event Grid uses for its own event types, and it
satisfies the CloudEvents guidance that type identify the organization defining the event. To rename a message later, declare the new name here and keep the old one with
MessageNameAliasAttribute, or data written under it becomes unreadable.
Names share one global namespace: two message types declaring the same name is refused at registration, because stored data records the name and nothing else, so the two could not be told apart when read back.
Constructors
MessageNameAttribute(string)
Initializes a new instance of the MessageNameAttribute class.
public MessageNameAttribute(string name)
Parameters
namestringThe stable name this event is stored under.
Exceptions
- ArgumentException
nameis null or whitespace.
Properties
Name
Gets the stable name this event is stored under.
public string Name { get; }