简体   繁体   English

在控制台应用程序中测试的c#代码将无法在服务中使用

[英]tested c# code in console app / will not work in service

I wrote this code in C# and it works as a console application. 我在C#中编写了这段代码,它可以作为控制台应用程序使用。 However, when I transfer the code into a service application and install using installutil, I cannot get it to work. 但是,当我将代码传输到服务应用程序并使用installutil进行安装时,我无法使其工作。 Also, the console does not pop up. 此外,控制台不会弹出。 Will Console.ReadLine(); 将Console.ReadLine(); not work in a service program? 不在服务计划中工作?

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Timers;
using MySql.Data.MySqlClient;
using FAXCOMLib;
using FAXCOMEXLib;

namespace ProcessFaxes
{
    public partial class Service1 : ServiceBase
    {
    public Service1()
    {
        InitializeComponent();
    }
    public static Timer timer = new Timer();

    protected override void OnStart(string[] args)
    {

        timer.Elapsed += new ElapsedEventHandler(Tick);
        timer.Interval = 5000; // every 5 seconds
        timer.Enabled = true;
        Console.ReadLine();
     }

    protected override void OnStop()
    {

    }

    public static void Tick(object source, ElapsedEventArgs e)
    {
        string connString = "Server=localhost;Port=3306;Database=communications;Uid=myuser;password=mypass;";
        MySqlConnection conn = new MySqlConnection(connString);
        MySqlCommand command = conn.CreateCommand();

        MySqlConnection connupdate = new MySqlConnection(connString);
        MySqlCommand commandupdate = connupdate.CreateCommand();

        command.CommandText = "SELECT * FROM outbox WHERE `faxstat` = 'Y' AND `fax` <> '' AND `faxpro` = 'PENDING'";
        //command.CommandText = "UPDATE blah blah";
        //conn.Open();
        //conn.ExecuteNonQuery();
        //conn.Close();

        try
        {

            conn.Open();
            connupdate.Open();

        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }
        MySqlDataReader reader = command.ExecuteReader();

        if (reader.HasRows)
        {

            while (reader.Read())
            {
                Console.WriteLine(reader["filepath"].ToString());
                SendFax(reader["id"].ToString(), reader["filepath"].ToString(), @"C:\FAXDOC\" + reader["filepath"].ToString(), reader["account"].ToString(), reader["fax"].ToString());
                string id = reader["id"].ToString();
                commandupdate.CommandText = "UPDATE outbox SET `faxpro` = 'DONE' WHERE `id` = '" + id + "'";
                commandupdate.ExecuteNonQuery();

            }
        }

        conn.Close();
        connupdate.Close();
    }

    public static void SendFax(string DocumentId, string DocumentName, string FileName, string RecipientName, string FaxNumber)
    {
        if (FaxNumber != "")
        {
            try
            {
                FAXCOMLib.FaxServer faxServer = new FAXCOMLib.FaxServerClass();
                faxServer.Connect(Environment.MachineName);


                FAXCOMLib.FaxDoc faxDoc = (FAXCOMLib.FaxDoc)faxServer.CreateDocument(FileName);

                faxDoc.RecipientName = RecipientName;
                faxDoc.FaxNumber = FaxNumber;
                faxDoc.BillingCode = DocumentId;


                int Response = faxDoc.Send();


                faxServer.Disconnect();

            }
            catch (Exception Ex) { Console.WriteLine(Ex.Message); }
        }



    }
}

} }

Ok, I removed the Console.ReadLine(); 好的,我删除了Console.ReadLine(); and the mysql is working and also updating, but now SendFax() is not working. 并且mysql正在工作并且还在更新,但是现在SendFax()无法正常工作。 There are not any faxes being sent to the fax console. 没有任何传真发送到传真控制台。

本质上,服务无法与用户界面交互,因此您无法在不实施某些解决方法的情况下从服务中打开Windows等。

服务在后台执行,不是交互式的,因此console.read不起作用。

Yes Console.ReadLine will not work in Service code. 是Console.ReadLine在服务代码中不起作用。 When you write code for your service, I suggest you divide it such that the main part of your code goes into a dynamic library that you use both in the service and a console application. 当您为服务编写代码时,我建议您将其划分为使代码的主要部分进入您在服务和控制台应用程序中使用的动态库。 The console application is meant for testing. 控制台应用程序用于测试。 The service code must not perform any kind of user interaction. 服务代码不得执行任何类型的用户交互。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM