我有一个c#控制台应用程序,计划在Win 2003服务器上运行,读取磁盘空间,CPU使用率等值。
现在我想在aspx页面上在线或实时显示这些值。
到目前为止我的(非工作)尝试:
WCF服务:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
namespace ServerMonitoring
{
public class Service1 : IService1
{
public void InsertDataValues(DataValues server)
{
FreeDiskSpace = server.FreeDiskSpace;
Data.FreeDiskSpace = FreeDiskSpace;
}
[DataMember]
public decimal FreeDiskSpace
{
get { return Data.FreeDiskSpace; }
set { Data.FreeDiskSpace = value; }
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
namespace ServerMonitoring
{
[ServiceContract]
public interface IService1
{
[OperationContract]
void InsertDataValues(DataValues server);
}
[DataContract]
public class DataValues
{
[DataMember]
public decimal FreeDiskSpace;
/*
[DataMember]
public decimal TotalDiskSpace;
*/
}
}
尝试从代码后面更新文本框:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace ServerMonitoring
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
TextBox1.Attributes.Add("onchange", "DefaultValue();");
HiddenFieldFreeDiskSpace.Value = Data.FreeDiskSpace.ToString();
}
}
}
Default.aspx的:
<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master"
AutoEventWireup="true"
CodeBehind="Default.aspx.cs" Inherits="ServerMonitoring._Default" %>
<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="head">
<script type="text/javascript">
function DefaultValue() {
var data = $('#<%=HiddenFieldFreeDiskSpace.ClientID%>').val();
var txt1 = document.getElementById("TextBox1");
txt1.setAttribute("value", data);
}
</script>
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="ContentPlaceHolder1">
<div class="extra-wide-column">
<h2>
FIC Server Monitoring Services
</h2>
<br />
<br />
<br />
<h3>System</h3>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:HiddenField runat="server" ID="HiddenFieldFreeDiskSpace" />
</ContentTemplate>
</asp:UpdatePanel>
</div>
</asp:Content>
要写入文本框的值来自我的控制台应用程序,并引用了我的WCF服务
static void UpdateList(string columnName, decimal diskSpaceValue)
{
Service1 serv = new Service1();
DataValues dv = new DataValues();
dv.FreeDiskSpace = diskSpaceValue;
serv.InsertDataValues(dv);
}
但是,该值不会发送到asp页面
BR, 对等
答案 0 :(得分:0)
我真的建议使用带有RESTful WCF服务的jQuery。在CodeProject上查看这篇很棒的文章:
http://www.codeproject.com/Articles/128478/Consuming-WCF-REST-Services-Using-jQuery-AJAX-Call
起初看起来似乎是额外的工作,但对于你想要达到的目标,这绝对是值得的。