如何在Webservice上使用其他页面的变量?

时间:2015-08-05 13:09:25

标签: c# asp.net web-services

我发现了一个webservice示例(并为我编辑),如下所示:

MyServiceClass.cs:

[WebService]
public class DBStatus
{
    [WebMethod]
    public Status[] StatusXML()
    {
        Status[] stts = new Status[] {
            new Status() {
                DB = "DB1",
                NODE = "TRUE",
                FTP = "TRUE",
                PROCESS = "TRUE"
            }
        };
        return stts;
    }
}

Status.cs:

public class Status
{
    public string DB { get; set; }
    public string NODE { get; set; }
    public string FTP { get; set; }
    public string PROCESS { get; set; }
}

Status.asmx:

<%@ WebService Language="C#" CodeBehind="~/App_Code/MyServiceClass.cs" Class="DBStatus" %>

我有一个其他页面并做这样的事情(status.aspx.cs):

public partial class StatusPage: System.Web.UI.Page
{
    public char v3;
    try
    {
        OleDbConnection Connection3;
        using (Connection3 = new OleDbConnection("Provider=MSDAORA.1;Data Source=DATABASE1:1521/orcl;Persist Security Info=True;Password=PASSWORD;User ID=USERNAME"))
        {
            Connection3.Open();
            v3 = "online";
            Connection3.Close();
        }
    }
    catch (Exception)
    {
        v3 = "offline";
    }
}

我需要在webservice上使用这个变量,如下所示:

DB="this is v3 variable from status.aspx.cs (like online/offline)"
NODE="this is other variable from status.aspx.cs",
FTP="this is other variable from status.aspx.cs",
PROCESS="this is other variable from status.aspx.cs"

我该怎么做?

1 个答案:

答案 0 :(得分:0)

最简单的方法是将[WebMethod]更改为[WebMethod(EnableSession = true)],然后将共享变量保存在会话中。