Contents
|
Unobtrusive SampleIn order to see messaging in unobtrusive mode open the unobtrusive sample. First of all run the solution - you should see two console applications start up. Find the client application by looking for the one with "Client" in its path and press 'C' to send a Command and 'R' to send a Request. Press 'E' at the server application to publish an event.
Configuring Unobtrusive messageBelow is a snippet that shows how to pass in your own conventions to determine which types are message definitions instead of using the IMessage interface: Configure.With() .DefiningCommandsAs(t => t.Namespace != null && t.Namespace.EndsWith("Commands")) .DefiningEventsAs(t => t.Namespace != null && t.Namespace.EndsWith("Events")) .DefiningMessagesAs(t => t.Namespace != null && t.Namespace.EndsWith("Messages")) .DefiningEncryptedPropertiesAs(p => p.Name.StartsWith("Encrypted")); The code above tells NServiceBus to treat all interfaces/classes with a namespace
that ends with “Messages” the same as to messages that explicitly implements
IMessage.
NServiceBus supports property level encryption by using a special WireEncryptedString property and the last line in the snippet is the unobtrusive way to accomplish that encryption. Now let's go look at the code:
There are 5 projects in the solution:
Sample messaging patternsThis sample contains three messaging patterns:
Send/Reply messaging pattern codeFor full sample please visit the Full Duplex sample. Client sideMessage declarationThe following code, at the client project endpointConfig does the messages configuration: .DefiningMessagesAs(t => t.Namespace != null && t.Namespace =="Messages")The above line of code tells NServiceBus that it should treat classes that are declared in the Messages namespace as they were explicitly implementing the IMessage interface. Let's open the client application configuration file (app.config) to see its configuration: <add Messages="Messages" Endpoint="server"/>The above declaration instruct NServiceBus that the target of IMessage type messages is the Server endpoint. The following is the client code to send the Request:
Bus.Send<Request>(m =>
{
m.RequestId = requestId;
});
Server sideAt the server side, it is handling the Request in the RequestMessageHandler class and only Replying back to the client as follows:
bus.Reply(new Response
{
ResponseId = message.RequestId
});
Back to the client code we will find a Response handler code: public class ResponseHandler : IHandleMessages<Response> Command/Status messaging pattern codeClient sideMessage declarationThe following declaration instructs NServiceBus to use those classes with namespace that ends with Commands the same as to messages that are explicitly implements the ICommand interface .DefiningCommandsAs(t => t.Namespace != null && t.Namespace.EndsWith("Commands")) Let's open the client application configuration file (app.config) to see its configuration: <add Messages="Commands" Endpoint="server"/> The above declaration instruct NServiceBus that the target of ICommand type messages is the Server endpoint. The following is the client code to send the Command:
Bus.Send<MyCommand>(m =>
{
m.CommandId = commandId;
m.EncryptedString = "Some sensitive information";
}).Register<CommandStatus>(outcome=> Console.WriteLine("Server returned status: " + outcome));
The client is sending a message and registering a method to handle returned status. Server sideAt the server side, it is handling in the Handle method, and all it is doing is sending back an OK status: public class MyCommandHandler : IHandleMessages<MyCommand> { readonly IBus bus; public MyCommandHandler(IBus bus) { this.bus = bus; } public void Handle(MyCommand message) { Console.WriteLine("Command received, id:" + message.CommandId); Console.WriteLine("EncryptedString:" + message.EncryptedString); bus.Return(CommandStatus.Ok); } } The message.EncryptedString is encrypted by the NServiceBus framework since it was declared as follows, in the endpointConfig class (of both client and server): public void Init() { Configure.With() .DefiningCommandsAs(t => t.Namespace != null && t.Namespace.EndsWith("Commands")) .DefiningEventsAs(t => t.Namespace != null && t.Namespace.EndsWith("Events")) .DefiningMessagesAs(t => t.Namespace != null && t.Namespace == "Messages") .DefiningEncryptedPropertiesAs(p => p.Name.StartsWith("Encrypted")); } The above code instructs NServiceBus to encrypt any property that starts with the string Encrypted and resides in any class in the namespaces that ends with
Command or Events, or at namespaces that equal to Messages. Publish/Subscribe messaging pattern codeFor full sample please visit the PubSub documentation Client sideMessage declarationThe following declaration instructs NServiceBus to use those classes with namespace that ends with Events the same as to messages that are explicitly implements the IEvent interface .DefiningEventsAs(t => t.Namespace != null && t.Namespace.EndsWith("Events")) Let's open the client application configuration file (app.config) to see its configuration: <add Messages="Events" Endpoint="server"/> The above declaration instruct NServiceBus that the target of IEvent type messages is the Server endpoint. In the case of events, it means that the client subscribe to the publishing Server endpoint. The following is the client code to handle published events and just print a message to the console:public class MyEventHandler : IHandleMessages<IMyEvent> { public void Handle(IMyEvent message) { Console.WriteLine("IMyEvent received from server with id:" + message.EventId); } } Server sideAt the server side, it is publishing an event as follows:
Bus.Publish<IMyEvent>(m =>
{
m.EventId = eventId;
});
When using naming convention to mark your commands events and messages, you can achieve freedom from dependency on NServiceBus message versioning.
Where to go next?It might be a good idea now to cover the Message Mutators subject, here. |