Use message and exception listeners in .NET
A .NET application uses a message listener to receive messages asynchronously, and it uses an exception listener to be notified asynchronously of a problem with a connection.
The functionality of both the message and exception listeners is the same for .NET and for C++. However, there are some small implementation differences.
Procedure
-
To set up a message listener in order to receive messages asynchronously, complete the
following steps:
-
Define a method that matches the signature of the message listener delegate.
The method that you define can be either a static or an instance method and can be defined in
any accessible class. The delegate signature is as follows:
public delegate void MessageListener(IMessage msg);
and so you could define the method as:void SomeMethodName(IMessage msg);
-
Instantiate this method as a delegate using something similar to the following example:
MessageListener OnMsgMethod = new MessageListener(SomeMethodName)
-
Register the delegate with one or more consumers by setting it to the MessageListener property
of the consumer:
consumer.MessageListener = OnMsgMethod;
We can remove the delegate by setting the MessageListener back to null:consumer.MessageListener = null;
-
Define a method that matches the signature of the message listener delegate.
The method that you define can be either a static or an instance method and can be defined in
any accessible class. The delegate signature is as follows:
-
To set up an exception listener, complete the following steps.
The exception listener works in much the same way as the message listener, but has a different
delegate definition and is assigned to the connection rather then the message consumer. This is the
same as for C++.
-
Define the method.
The delegate signature is as follows:
public delegate void ExceptionListener(Exception ex);
and so the method defined could be:void SomeMethodName(Exception ex);
-
Instantiate this method as a delegate using something similar to the following example:
ExceptionListener OnExMethod = new ExceptionListener(SomeMethodName)
-
Register the delegate with the connection by setting its ExceptionListener property:
connection.ExceptionListener = OnExMethod ;
We can remove the delegate by resetting the ExceptionListener to:null: connection.ExceptionListener = null;
-
Define the method.
The delegate signature is as follows:
Parent topic: Writing XMS .NET applications