Once you have installed and configured the host your now ready to add some message handlers.

To do this you will need to create a .net project in Visual Studio and add references to the following assemblies:
  • AppFx.ServiceBus
  • AppFx.ServiceBus.Receiver
  • System.ComponentModel.Composition

The first two assemblies are part of the AppFx.ServiceBus framework and the third assembly is because we use the Managed Extensibility Framework (MEF) to load message handlers.

You will need to create a class which will handle the message. It will also need to be decorated with the MEF attributes to inform the framework what kind of message the class handles.

The following code snippet shows the decleration of this class.

    [Export(typeof(IHandleMessage))]
    [ExportMetadata("MessageType", XsdMessageType)]
    public class MyMessageHandler : IHandleMessage
    {
        public const string XsdMessageType = "http://messaging/contracts/sample/v1.0#AdjudicateClaimCommandResponse";

The class must also implement the IHandleMessage interface from the AppFx.ServiceBus framework.

The MEF attributes will use the message type which has been declared as a constant and indicate that the class is a message handler and that it also handles a specific type of message.

IHandleMessage Interface

The methods implemented by the interface are as follows.

object HandleRequestResponse(object message);

This method is used when you want to implement a handler which will handle a message which will return a response.

Note:
The client will indicate if its expecting a response by sending details of a reply to address. If the framework finds that a response is expected (ie: there is a reply to address) but the handler only supports a one way message then an error will be thrown to the client.

void Handle(object message);

This method is used when you want to implement a handler which will handle a message which will not return a response.

Note:
If the client sends a one way message (ie: no reply to address) but the handler only supports messages with a response then an error will be thrown. A handler can support both one and two way messages at the same time.

string InputMessageType { get; }

This is a string which will return the message type which should be in the namespace#root element format.

Type InputClrMessageType { get; }

This property allows you to tell the framework the CLR type that the message relates to. This is used when serializing and deserializing messages.

bool HandlesRequestResponseMessages { get; }

This tells the framework if the handler supports two way messaging.

bool HandlesOneWayMessages { get; }

This tells the framework if the handler supports one way messages.

int Priority { get; }

This property is currently not used and is there for a future release where we will include a prioritisation feature.

bool DeserializeRequestMessage { get; }

This property tells the framework if the handler wants the message to be deserialized. If it is then the framework will use the CLR Message Type provided by the handler to deserialize the message and pass that into the handle method. If this property is false then a stream containing the message will be passed in.


Example

The below code snippet is an example handler from one of the samples.

namespace Fabrikam.MessageHandlers
{
    /// <summary>
    /// </summary>
    [Export(typeof(IHandleMessage))]
    [ExportMetadata("MessageType", XsdMessageType)]
    public class AdjudicateClaimCommandResponse_v1_0_Handler : IHandleMessage
    {
        public const string XsdMessageType = "http://messaging/contracts/sample/v1.0#AdjudicateClaimCommandResponse";

        public object HandleRequestResponse(object message)
        {
            throw new NotSupportedException("this handler does not support request response");
        }

        public void Handle(object message)
        {
            var request = message as Contracts.Types.AdjudicateClaimCommandResponse;
            if (request == null)
            {
                Logger.Instance.Warn(string.Format("{0} recieved a null message", this.GetType().FullName));
                throw new AppFx.ServiceBus.Exceptions.ApplicationException("Null message recieved")
                {
                    RetrySupported = false
                };
            }

            Logger.Instance.Info("");
            Logger.Instance.Info(string.Format("Recieved an adjudicate claim Response - Claim ID={0}, Response={1}", request.ClaimId, request.AdjudicationResult));
            Logger.Instance.Info("");

            var path = string.Format(@"{0}\AdjudicateClaimCommandResponse.{1}.xml", ConfigSettings.FilePath, request.MessageID);
            using (var fs = new FileStream(path, FileMode.Create, FileAccess.Write))
            {
                Serializer.Serialize(fs, request, SerializationFormat.Json);
                fs.Flush();
            }
        }

        public string InputMessageType
        {
            get { return XsdMessageType; }
        }

        public Type InputClrMessageType
        {
            get { return typeof(Contracts.Types.AdjudicateClaimCommandResponse); }
        }

        public bool HandlesRequestResponseMessages
        {
            get { return false; }
        }

        public bool HandlesOneWayMessages
        {
            get { return true; }
        }

        public int Priority
        {
            get { return 1; }
        }

        public bool DeserializeRequestMessage
        {
            get { return true; }
        }
    }
}

Last edited Feb 1, 2013 at 10:30 PM by michaelstephenson, version 3

Comments

No comments yet.