这个例子是C#服务调用RFC函数,获取物料的信息,并生成Excel文件
上接文章:C#服务
创建函数
创建结构
编写源代码
创建批处理文件
echo %~dp0
%SystemRoot%\Microsoft.NET\Framework64\v4.0.30319\installUtil.exe %~dp0WindowsService1_SAP_RFC.exe
Net Start C#ServerTest
sc config C#ServerTest=auto
pause
%SystemRoot%\Microsoft.NET\Framework64\v4.0.30319\InstallUtil.exe /u %~dp0WindowsService1_SAP_RFC.exe
pause
运行结果-成功
部署服务器
C#代码
using SAP.Middleware.Connector;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Data.OleDb;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Threading.Tasks;
using System.Timers;
namespace WindowsService1_SAP_RFC
{
//创建人:LiuHongyu
//创建日期:2024年12月20日
//描述:测试C#服务的创建以及和SAP进行通讯,并生成Excel文档
public partial class Service1 : ServiceBase
{
public Service1()
{
InitializeComponent();
}
//文件路径
private string filePath = ConfigurationManager.AppSettings["FilePath"];
//Excel文件路径
private string excelFilePath = ConfigurationManager.AppSettings["ExcelFilePath"];
//Excel模板文件路径
private string excelFileTemplatePath = ConfigurationManager.AppSettings["ExcelFileTemplatePath"];
private int n = 1;
//计时器
Timer timer;
//rfc的地址
private RfcDestination _rfcDestination;
//每天定时器开关
private bool dayScheduleSwitch;
//每天定时执行的时间
private string dayScheduleTime;
//每月定时器开关
private bool monthScheduleSwitch;
//服务启动时调用
protected override void OnStart(string[] args)
{
WriteLog(filePath, "启动服务------");
InitTimer();
}
private void InitTimer()
{
try
{
WriteLog(filePath, "开始计数------");
timer = new Timer(60000);//1分钟
//时间间隔到了之后,执行的操作为Execute
timer.Elapsed += Execute;
//是否重复执行
timer.AutoReset = true; //如果不设置就是只会执行一次
//是否执行Elapsed中的事件
timer.Enabled = true;
}
catch (Exception ex)
{
WriteLog(filePath, "计时器错误: " + ex.Message);
throw;
}
}
//执行的操作
private void Execute(object sender, ElapsedEventArgs e)
{
//注册S