tags:

views:

57

answers:

1

I have tried to deploy a WebORB .NET C# ASP.NET (C#.NET) application, but I am unable to get it to work. It will run successfully, but it doesn't do anything, and I get the feeling I am making some silly mistake. I have a Flex client which should read the data coming from the WebORB server, and the WebORB console shows that the Flex client is connected so that part is fine. The C#.net server application is what is not working.

I have posted the C#.asp server application code below as I believe the client works fine. This application should capture the CPU usage of the machine it is running on, and send it to the WEBORB server to allow access by the Flex client. The code is from an example provided on the WebORB website.

Default.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="aspNetCCPU._Default" %>

<%
    // Load a new instance of the class
    aspNetCCPU.Class1 jiifjio = new aspNetCCPU.Class1();
    Response.Write("Class loaded");

     %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;

<html xmlns="http://www.w3.org/1999/xhtml"&gt;
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>

    </div>
    </form>
</body>
</html>

Class1.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text;
using System.Diagnostics;
using System.Timers;
using Weborb.Util; 

using Weborb.Messaging.Api.Service;
using Weborb.Messaging.Api;
using Weborb.Messaging.Server.Adapter;

namespace aspNetCCPU
{
    public class Class1 : ApplicationAdapter
    {
        private Timer cpuReadingTimer;
        private PerformanceCounter cpuCounter;

        // invoked when WebORB for .NET starts up
        public override bool appStart(IScope app)
        {
            bool appStarted = base.appStart(app);

            // if application could not start for any reason, do not proceed further
            if (!appStarted)
                return appStarted;

            // initialize performance counter
            cpuCounter = new PerformanceCounter();
            cpuCounter.CategoryName = "Processor";
            cpuCounter.CounterName = "% Processor Time";
            cpuCounter.InstanceName = "_Total";

            // start thread to get CPU readings
            cpuReadingTimer = new Timer(1000);
            cpuReadingTimer.Elapsed += new ElapsedEventHandler(cpuReadingTimer_Elapsed);
            return appStarted;
        }

        void cpuReadingTimer_Elapsed(object sender, ElapsedEventArgs e)
        {
            // ignore timer event, if there are no connected clients to the scope
            if (scope.getClients().Count == 0)
                return;

            // get the CPU reading
            float cpuUtilization = cpuCounter.NextValue();

            // create an array of values to deliver to the client.
            // there is only one value, but the API requires it to be an array
            object[] args = new object[] { cpuUtilization };

            // get an enumeration of connections to this application
            IEnumerator<IConnection> connections = scope.getConnections();

            while (connections.MoveNext())
            {
                IConnection connection = connections.Current;

                // invoke client-side function to deliver CPU reading
                if (connection is IServiceCapableConnection)
                    ((IServiceCapableConnection)connection).invoke("processCPUReading", args);
            }
        }
    }
}
A: 

Joel --

The Mistake: Forgetting that, by default, a new instance of the service class is instantiated (by WebORB) each time any of its methods is called.

The Fix: Decorating the service class with the attribute [ApplicationActivation()], so that the same instance of the service class is used across the life of the invoking application.

For details, including sample code, please see http://blog.themidnightcoders.com/index.php/2010/10/28/server-side-cpu-usage-in-weborb.

Hope that helps! :-)

Jim Plamondon Technology Evangelist The Midnight Coders (makers of WebORB)

P.S.: I apologize for the slowness of my response; I first found your question today.

JimPlamondon