如何在后台运行ASP.NET页面

本文关键字:ASP NET 页面 运行 后台 | 更新日期: 2023-09-27 18:21:00

大家好,我有一个要求需要满足,我创建了一个ASP.NET页面,当页面加载时,它会从sap中获取数据。我已经在PAGE LOAD中编写了获取SAP表的代码。

我需要每天在后台自动运行一次页面(无需用户交互),这样它就可以自动在ASP.NET和SAP之间同步信息。

这是我的代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web; 
using System.Web.UI;
using System.Data;
using System.Web.UI.WebControls;  
using System.Text;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Web.Security;
using System.Security.Authentication;
using First.EHSIMS;
using First.EHSIMS.Dbml;
using First.EHSIMS.Breadcrumb_New;
using SAP.Middleware.Connector;

 public class SAPSystemConnect : IDestinationConfiguration
{
public RfcConfigParameters GetParameters(string destinationName)
{
    RfcConfigParameters parms = new RfcConfigParameters();
    if ("DEV".Equals(destinationName))
    {
        parms.Add(RfcConfigParameters.AppServerHost, "ECC6");
        parms.Add(RfcConfigParameters.SystemNumber, "04");
        parms.Add(RfcConfigParameters.User, "sapuser");
        parms.Add(RfcConfigParameters.Password, "newmaars1");
        parms.Add(RfcConfigParameters.Client, "800");
        parms.Add(RfcConfigParameters.Language, "EN");
        parms.Add(RfcConfigParameters.PoolSize, "5");
        parms.Add(RfcConfigParameters.MaxPoolSize, "10");
        parms.Add(RfcConfigParameters.IdleTimeout, "000");
    }
    return parms;
   }
   public bool ChangeEventsSupported()
  {
    //throw new NotImplementedException();
    return false;
  }
  public event RfcDestinationManager.ConfigurationChangeHandler ConfigurationChanged;
  }

   public partial class Sap_Connection_sapcon : System.Web.UI.Page
   {
  public static DataTable CreateDataTable(IRfcTable rfcTable)
  {
    var dataTable = new DataTable();
    for (int element = 0; element < rfcTable.ElementCount; element++)
    {
        RfcElementMetadata metadata = rfcTable.GetElementMetadata(element);
        dataTable.Columns.Add(metadata.Name);
    }
    foreach (IRfcStructure row in rfcTable)
    {
        DataRow newRow = dataTable.NewRow();
        for (int element = 0; element < rfcTable.ElementCount; element++)
        {
            RfcElementMetadata metadata = rfcTable.GetElementMetadata(element);
            newRow[metadata.Name] = row.GetString(metadata.Name);
        }
        dataTable.Rows.Add(newRow);
    }
    return dataTable;
   }

    protected void Page_Load(object sender, EventArgs e)
    {
    SAPSystemConnect c = new SAPSystemConnect();
    RfcDestinationManager.RegisterDestinationConfiguration(c);
    RfcDestination rfcDest = null;
    rfcDest = RfcDestinationManager.GetDestination("DEV");
    try
    {
        RfcDestination mydestination = RfcDestinationManager.GetDestination("DEV");
        RfcRepository myrepository = mydestination.Repository;
        IRfcFunction position = myrepository.CreateFunction("ZPOSI");       
        IRfcTable positable = position.GetTable("POSITIONTAB1");
        IRfcTable jobtable = position.GetTable("JOBTAB");          
        position.Invoke(mydestination);
        RfcDestinationManager.UnregisterDestinationConfiguration(c); 

        if (jobtable.RowCount > 0)
            {
                DataTable JOBTABLE = CreateDataTable(jobtable);
                using (EHSIMSDataContext db = new EHSIMSDataContext())
                foreach (DataRow newRow1 in JOBTABLE.Rows)
                {
                    string JOB_ID = newRow1["JOB_ID"].ToString();
                    JOB34 _job1 = (from job1 in db.JOB34s
                                  where job1.JOB_NO.Equals(JOB_ID)
                                  select job1).SingleOrDefault<JOB34>();
                    if(_job1 == null)
                    {
                        JOB34 _JOB;
                        _JOB = new JOB34();
                        Guid g = Guid.NewGuid();
                        Guid g1 = Guid.NewGuid();
                        _JOB.JOB_ID = g1;
                        _JOB.JOB_NO = newRow1["JOB_ID"].ToString();
                        _JOB.NAME = newRow1["JOB_TXT"].ToString();
                        db.JOB34s.InsertOnSubmit(_JOB);
                        db.SubmitChanges();
                    }
                    else
                    {
                    }
                }
        }
        else
        {
            posid.Text = "notworking";
        }
        if (positable.RowCount > 0)
        {
            DataTable dataTable = CreateDataTable(positable);
            using (EHSIMSDataContext db1 = new EHSIMSDataContext())
                foreach (DataRow newRow in dataTable.Rows)
                {
                    string POS_ID = newRow["POSITION_ID"].ToString();
                    POSITION34 _pos1 = (from job in db1.POSITION34s
                                  where job.POSITION_NO.Equals(POS_ID)
                                        select job).SingleOrDefault<POSITION34>();
                    if (_pos1 == null)
                    {
                        string JOB_ID = newRow["JOB_ID"].ToString();
                        JOB34 _job = (from job in db1.JOB34s
                                      where job.JOB_NO.Equals(JOB_ID)
                                      select job).SingleOrDefault<JOB34>();

                        POSITION34 _pos;
                        _pos = new POSITION34();
                        Guid g = Guid.NewGuid();
                        Guid g1 = Guid.NewGuid();
                        _pos.POSITION_ID = g;
                        _pos.POSITION_NO = newRow["POSITION_ID"].ToString();
                        _pos.NAME = newRow["POSITION_TXT"].ToString();
                        _pos.REPORTED_TO = newRow["REPORTED_TO"].ToString();
                        _pos.JOB_ID = _job.JOB_ID;

                        db1.POSITION34s.InsertOnSubmit(_pos);
                        db1.SubmitChanges();
                    }
                    else
                    { 
                    }
                }
        }
        else
        {
            posid.Text = "notworking";
        }
        }
        catch (Exception ej)
       {
        posid.Text = ej.Message;
        }
     }
    }  

如何在后台运行ASP.NET页面

有几个选项可供选择:

  • 您可以使用独立的windows服务来完成此任务,或者
  • 在Application start上启动一个线程,该线程将每天执行一次操作,或者
  • 在每个页面上,如果操作在一天前以上完成,请检查数据库并执行此操作

另请参阅:

如何在不使用windows服务的情况下在ASP中进行后台处理

我可以使用线程在IIS上执行长时间运行的作业吗?