Scriptico

Handle Pre- and Post Invocation in WebORB for .NET

To demonstrate this simple, but really helpful feature of WebORB for .NET let’s create a sample issue. An application has a simple service. Developers should be able to monitor all invocations from the client, and depending on the invoked method, the developer must create an entry in the log file. In this example, instead of a client’s application, I will use WebORB console. A server-side code is written in the Visual Studio 2010, and you will the find zipped project at the bottom of this article.

First of all, I’ve created a simple service class with a few methods:

Now, let’s add a little bit of information on how we can accomplish our goal. The WebORB for .NET API has the Weborb.IWebORBAttribute interface. This interface defines two methods:

void HandlePreInvoke( MethodInfo method, object obj, object[] arguments );
Hashtable HandlePostInvoke( MethodInfo method, object obj, object[] arguments, ref object returnValue, bool isException );

So, I should create a custom attribute class, which implements the Weborb.IWebORBAttribute interface, implement methods, and apply the created attribute to my service.

using System;
using System.Collections.Generic;
using System.Text;

namespace Scriptico.Sample
{
    public class SimpleService
    {
        [SampleService.InvocationInspector]
        public String getSimpleMessage()
        {
            return "This is a simple message!";
        }

        [SampleService.InvocationInspector]
        public String getComplicatedMessage()
        {
            return "This is a complicated message!";
        }
    }
}

Ok, my custom attribute class is shown below:

using System;
using System.Collections.Generic;
using System.Text;
using Weborb;
using System.Collections;
using System.Reflection;
using Weborb.Util.Logging;

namespace SampleService
{
    [System.AttributeUsage(System.AttributeTargets.Method,
                       AllowMultiple = true)]
    public class InvocationInspectorAttribute : Attribute, IWebORBAttribute
    {
        public void HandlePreInvoke(MethodInfo method, object obj, object[] arguments)
        {
            Log.log(LoggingConstants.INFO, "Pre Invoke. Client invocation:" + method.Name);
        }

        public Hashtable HandlePostInvoke(MethodInfo method, object obj, object[] arguments, ref object returnValue, bool isException)
        {
            Log.log(LoggingConstants.INFO, "Post Invoke. Method completed:" + method.Name);

            return null;
        }
    }
}

As you can see, I am using the WebORB logger, and we will find all messages in the log file, which is located in the log directory of the root folder of WebORB installation. For example, in my environment the WebORB log file is located here:
C:\Program Files\WebORB for .NET\4.3.0.2\logs

Now, let’s try to apply this attribute to my service on the method level, deploy compiled assembly to the WebORB application, and invoke the getComplicatedMessage method from the console. If you are not familiar with WebORB console just look at the following screenshot.
WebORB Console

In the log file I found the following:

[Thread-8] WEBORB INFO:3/10/2011 10:38 PM:Pre Invoke. Client invocation:getComplicatedMessage
[Thread-8] WEBORB INFO:3/10/2011 10:38 PM:Post Invoke. Method completed:getComplicatedMessage

That’s awesome! Now, let’s change the InvocationInspectorAttribute class a little bit. I want to make this attribute also available on the class level, so I just need to change the System.AttributeUsage attribute as shown below:

[System.AttributeUsage(System.AttributeTargets.Class | System.AttributeTargets.Method, AllowMultiple = true)]

Now, I removed all our attribute entries in the service class, and specified the SampleService.InvocationInspector attribute for the whole service. Compile the code, deploy it, and invoke getSimpleMessage() and getComplicatedMessage() methods. What do I have in the log file?


[Thread-22] WEBORB INFO:3/10/2011 10:51 PM:Pre Invoke. Client invocation:getSimpleMessage
[Thread-22] WEBORB INFO:3/10/2011 10:51 PM:Post Invoke. Method completed:getSimpleMessage

[Thread-31] WEBORB INFO:3/10/2011 10:52 PM:Pre Invoke. Client invocation:getComplicatedMessage
[Thread-31] WEBORB INFO:3/10/2011 10:52 PM:Post Invoke. Method completed:getComplicatedMessage

Yeha! It is working well.

That’s it!

Resources

0. Source Code

1. WebORB for .NET
2. Attributes (C# and Visual Basic)
3. Creating Custom Attributes (C# and Visual Basic)

Category: Examples, WebORB (.NET)

Tagged: , ,

Leave a Reply

ERROR: si-captcha.php plugin: GD image support not detected in PHP!

Contact your web host and ask them to enable GD image support for PHP.

ERROR: si-captcha.php plugin: imagepng function not detected in PHP!

Contact your web host and ask them to enable imagepng for PHP.