1

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

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

默认.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);
            }
        }
    }
}
4

1 回答 1

1

乔尔——

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

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

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

希望有帮助!:-)

Jim Plamondon 技术传播者 The Midnight Coders(WebORB 的制造商)

PS:我为我的回复缓慢表示歉意;我今天第一次发现你的问题。

于 2010-10-28T19:51:30.240 回答