无法部署和使用启用WebORB的C#程序

时间:2010-09-17 09:41:13

标签: c# asp.net weborb

我曾尝试部署一个WebORB .NET C#ASP.NET(C#.NET)应用程序,但我无法让它工作。它会成功运行,但它没有做任何事情,我感觉我犯了一些愚蠢的错误。 我有一个Flex客户端,它应该读取来自WebORB服务器的数据,并且WebORB控制台显示Flex客户端已连接,因此该部分正常。 C#.net服务器应用程序无法正常工作。

我已经发布了下面的C#.asp服务器应用程序代码,因为我相信客户端工作正常。此应用程序应捕获其运行的计算机的CPU使用情况,并将其发送到WEBORB服务器以允许Flex客户端访问。该代码来自WebORB网站上提供的示例。

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">

<html xmlns="http://www.w3.org/1999/xhtml">
<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);
            }
        }
    }
}

1 个答案:

答案 0 :(得分:1)

乔尔 -

错误:忘记默认情况下,每次调用任何方法时,都会(通过WebORB)实例化服务类的新实例。

修复:使用[ApplicationActivation()]属性修饰服务类,以便在调用应用程序的整个生命周期中使用相同的服务类实例。

有关详细信息,包括示例代码,请参阅http://blog.themidnightcoders.com/index.php/2010/10/28/server-side-cpu-usage-in-weborb

希望有所帮助! : - )

吉姆普拉蒙顿 技术传播者 午夜编码员(WebORB的制作者)

P.S。:我为我的反应缓慢而道歉;我今天第一次找到你的问题。