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

发布于 2024-09-19 19:29:37 字数 3393 浏览 4 评论 0原文

我尝试部署 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);
            }
        }
    }
}

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

<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);
            }
        }
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

初见你 2024-09-26 19:29:37

Joel -

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

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

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

希望有帮助! :-)

吉姆·普拉蒙登
技术传播者
The Midnight Coders(WebORB 的创建者)

PS:对于我的回复缓慢,我深表歉意;我今天第一次发现你的问题。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文