Minimizing performance impact from managed SIP applications

The Managed SIP Application API, which I discussed in the previous post, allows you to manipulate the content and routing of SIP messages in some potentially very useful ways by running an application on a Front End Server. However, when the volume of messages is very large, dispatching them to managed code can have a significant effect on performance. For managed code applications that simply monitor messages, and don''t modify them or change the routing, there is a way to mitigate this performance impact by sending the message details to the application from the MSPL script in a sort of "fire-and-forget" mode, using the DispatchNotification method. Normally, when you dispatch a message to a managed SIP application using the Dispatch method in MSPL, the application "takes over" the routing of the message from that point forward. Execution of the MSPL script stops, and the application must proxy the request or response in some way in managed code. The DispatchNotification method, on the other hand,simply passes one or more pieces of information to the application,and then the execution of the MSPL script continues. The MSPL script remains responsible for routing the message or responding to it. This means that the managed code does not affect the speed of the message routing in any way.

Here''s an example of an MSPL script that uses DispatchNotification:

[xml] <?xml version="1.0"?> <r:applicationManifest r:appUri="" xmlns:r="">

<r:requestFilter methodNames="MESSAGE,INVITE,ACK,BYE" strictRoute="true" domainSupported="false"/>

<r:responseFilter reasonCodes="ALL"/>

<r:splScript><![CDATA[ if (sipRequest) { DispatchNotification("OnRequest", sipRequest.Method); ProxyRequest(); } else { DispatchNotification("OnResponse", sipResponse.StatusCode); ProxyResponse(); } ]]></r:splScript> </r:applicationManifest> [/xml]

A couple of things to note here: first, the script calls ProxyRequest or ProxyResponse after dispatching the notification, effectively routing the message to where it was going to go before the MSPL script got its hands on it. This is an important difference from the sample script from the previous post. Another difference is that the second parameter of DispatchNotification is required (for Dispatch, it''s optional). The reason is that the only information that gets passed to the managed SIP application is the parameters that you specify when calling DispatchNotification. The first parameter is the method name to call in the managed code, and all the subsequent parameters (you can have as many as you want) contain information to be passed to the method as part of the event arguments.

Here''s a class that you might use as your handler in the managed SIP application:

[csharp] using Microsoft.Rtc.Sip;

public class Logger { public void OnRequest(object sender, Microsoft.Rtc.Sip.NotificationReceivedEventArgs e) { foreach (var o in e.Parameters) { System.Console.WriteLine(o.ToString()); } }

public void OnResponse(object sender, NotificationReceivedEventArgs e) { foreach (var o in e.Parameters) { System.Console.WriteLine(o.ToString()); } } } [/csharp]

This is a trivially simple example, but of course you could do much more ambitious things here, cross-referencing with in-memory state, writing to a database, etc. The main thing to note is that these methods use a different event arguments object: NotificationReceivedEventArgs. This is important; you need to use a different method for DispatchNotification from the one you use for Dispatch, because the event arguments are different. This one does not give you access to an object representing the message. All you have is a collection called Parameters, which contains all of the data you passed in the parameters of DispatchNotification. So you need to pick out the information you need in the MSPL script.

In this case, I''m simply writing the method name (for requests) and the status code (for responses) to the console, but you can pass more parameters to get other information from the message to do things like call detail reporting, logging, auditing, and so forth. This may already be clear, but I want to point out also that you can mix Dispatch and DispatchNotification in a single MSPL script and a single managed SIP application. You just need to create separate methods in the managed code portion. So, your script can send notifications for messages where the application just needs to know about them, but doesn''t need to make any changes (perhaps to maintain its own session state information); and can use Dispatch when the application actually needs to take action. This helps minimize the effect that your managed SIP application has on the performance of message routing in the Lync environment.