运行 自定义模块即服务而不是启动 WinForms 应用程序

Run Custom Module as Service instead of launching a WinForms application

我为 Kofax 创建了一个自定义模块。该模块是一个 WinForms 应用程序,带有一个 运行time Form 和一个 setup form(管理模块)。

对于 运行time 应用程序,我使用此代码


Program.cs - 启动运行时间表格

internal static class Program
{
    [STAThread]
    private static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new FrmMain());
    }
}

FrmMain.cs - 初始化 UI 并创建所需的实例

public partial class FrmMain : Form
{
    private BatchProcessor batchProcessor;
    private BatchManager batchManager;
    private SessionManager sessionManager;

    public FrmMain()
    {
        InitializeComponent();
    }

    private void FrmMain_Load(object sender, EventArgs e)
    {
        try
        {
            batchProcessor = new BatchProcessor();
            sessionManager = new SessionManager();
            batchManager = new BatchManager(batchProcessor, sessionManager);
            // UpdateUI();              
            timerBatchPolling.Enabled = true;
        }
        catch (Exception exception)
        {
            throw exception;
        }
    }

    private void FrmMain_FormClosed(object sender, FormClosedEventArgs e)
    {
        timerBatchPolling.Enabled = false;

        try
        {
            sessionManager.Logout();
        }
        catch (Exception exception)
        {
            throw exception;
        }
    }

    private void timerBatchPolling_Tick(object sender, EventArgs e)
    {
        timerBatchPolling.Enabled = false;
        batchManager.BatchPolling();
        // UpdateUI();
        timerBatchPolling.Enabled = true;
    }
}

BatchManager.cs - 请求下一批处理

internal class BatchManager
{
    private BatchProcessor batchProcessor;
    private SessionManager sessionManager;

    public IBatch CurrentActiveBatch { get; private set; }

    public BatchManager(BatchProcessor batchProcessor, SessionManager sessionManager)
    {
        this.batchProcessor = batchProcessor;
        this.sessionManager = sessionManager;
        this.sessionManager.LoginToRuntimeSession();
    }

    public void BatchPolling()
    {
        CurrentActiveBatch = sessionManager.GetNextBatch();

        if (CurrentActiveBatch != null)
        {
            batchProcessor.ProcessBatch(CurrentActiveBatch);
        }
        else
        {
            sessionManager.Logout();
        }
    }
}

BatchProcessor.cs - 处理批次

internal class BatchProcessor
{
    public void ProcessBatch(IBatch batch)
    {
        // ...
    }
}

我刚刚看到也可以 运行 自定义模块作为服务。我不知道如何注册它们或如何设置代码,所以我想问是否有办法更改我的代码以将模块变成服务而不是表单应用程序。

感谢帮助!

创建一个从 ServiceBase. The two relevant methods are OnStart and OnStop, read more about them here 派生的新 class。这是一个示例,请注意 CustomModule 是我的自定义 classes 之一,负责登录 KC、处理批次等。

protected override void OnStart(string[] args)
{
    // TODO: Add code here to start your service.
    cm = new CustomModule();
    cm.Login("", "");

    if (CustomModule.BatchNotificationEnabled == true)
    {
        cm.ListenForNewBatches();
    }
    else
    {
        cm.PollForNewBatches();
    }
}

为了安装该服务,您可以添加另一个派生自 Installer 的 class,但这完全是可选的。但是,我建议您多做一些努力,因为这样您就可以按照与所有 Kofax 模块相同的方式安装和卸载您的 CM:MyCustomModule.exe -install.

最后,在您的 Program.cs 中,检查用户是否以交互方式启动模块:

if (Environment.UserInteractive)
{
    // run as module
    Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(false);
    Application.Run(new RuntimeForm(args));
}
else
{
    // run as service
    ServiceBase.Run(new CustomModuleService());
}