To avoid a reference leak when you pass an XLANG message or an XLANG message part to a method call as a parameter, use the following methods.
Method 1: Pass a message or a message part
When you pass a message part, do not pass the message part as an XLANGPart argument or return a value of type XLANGPart. This can cause reference leaks that are difficult to resolve. Instead, pass the message part as the type of the part. Consider the following code examples.
Code example 1
//static Void test(string s);
message String msg;
Class.test(msg); // passing the string contents
Code example 2
Messagetype mt {
String part;
};
Message mt msg;
Class.test(msg,part);
When you pass a message, do not put an XLANGPart member in a collection that has a lifetime that is longer than the lifetime of the function call. Instead, pass the message itself as an XLANGMessage object. Then, use the XLANGMessage subscript operators to access the message part inside the function call. Consider the following code example.
Void test(XLANGMessage xlm) {
XLANGPart xlp = xlm[0];
String sval = (String) xlp.RetrieveAs(typeof(string));
// You can now use the sval variable, the message properties, and the message part properties
// and not worry about lifetimes
}
Method 2: Pass an XLANGMessage object and an XLANGPart object
Do not use an XLANGMessage object or an XLANGPart object as an orchestration parameter. Instead, use a message type parameter to pass a message. If you want to pass a message part, pass the message. Then, use the part in the function, the subroutine, or the program that is called. If you want only the part value in the function, the subroutine, or the program that is called, use the part type to pass the message.
Method 3: Pass a message to user code
To avoid reference leaks, do not return an XLANGMessage parameter for a method call. If you return an XLANGMessage parameter that is passed in, you cannot call the
Dispose method on the parameter inside the method call. If you do this, lifetimes are violated and an exception error occurs.
When you pass a message through an XLANGMessage parameter to user code, the message is referenced to the root context. Typically, messages are not referenced to the root context. The lifetime of the root context is the lifetime of the orchestration instance. The root context is used to make sure that the user code does not hold the message after the orchestration instance finishes and closes.
If you have to release a message reference in a loop when the message has been passed through an XLANGMessage argument, you can use the
XLANGMessage.Dispose method. You can use the
XLANGMessage.Dispose method to release the reference to the root context if the following conditions are true:
- The XLANGMessage parameter is used locally.
- The lifetime of the XLANGMessage parameter is contained in the lifetime of the function call.
When you release the reference to the root context, the typical lifetime of the message is restored. Consider the following code example.
void test(XLANGMessage xlm) {
try {
// purely local party on the XLANGMessage
} finally {
xlm.Dispose();
}
}
However, you can use a
Dispose method for the class to clean up a collection of XLANGMessage messages. Consider the following code example.
Public class A {
Hashtable h = new Hashtable();
Public test(XLANGMessage xlm) {
h[xlm] = 1;
}
// more methods
Public Dispose() {
Foreach (XLANGMessage xlm in h.Keys)
Xlm.Dispose();
}
}