tested c# code in console app / will not work in s

2019-09-08 18:10发布

I wrote this code in C# and it works as a console application. However, when I transfer the code into a service application and install using installutil, I cannot get it to work. Also, the console does not pop up. Will 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(); and the mysql is working and also updating, but now SendFax() is not working. There are not any faxes being sent to the fax console.

标签: c# service
3条回答
爱情/是我丢掉的垃圾
2楼-- · 2019-09-08 18:39

Services by nature can not interact with the User Interface so you can not open windows etc from a service without implementing some work around.

查看更多
来,给爷笑一个
3楼-- · 2019-09-08 18:40

Yes Console.ReadLine will not work in Service code. 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.

查看更多
Bombasti
4楼-- · 2019-09-08 18:49

services are executed in background and are not interactive, therefore console.read will not work.

查看更多
登录 后发表回答