Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 控制台应用程序中测试的c代码/在服务中无法工作_C#_Service - Fatal编程技术网

C# 控制台应用程序中测试的c代码/在服务中无法工作

C# 控制台应用程序中测试的c代码/在服务中无法工作,c#,service,C#,Service,我用C编写了这段代码,它可以作为控制台应用程序使用。但是,当我将代码传输到服务应用程序中并使用installutil安装时,我无法使其正常工作。此外,控制台也不会弹出。Will Console.ReadLine;不在服务项目中工作 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Diagnostics; using System

我用C编写了这段代码,它可以作为控制台应用程序使用。但是,当我将代码传输到服务应用程序中并使用installutil安装时,我无法使其正常工作。此外,控制台也不会弹出。Will Console.ReadLine;不在服务项目中工作

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); }
        }



    }
}
}


好的,我删除了Console.ReadLine;mysql正在工作,也在更新,但现在SendFax不工作了。没有任何传真被发送到传真控制台。

服务本质上无法与用户界面交互,因此,如果不实施一些变通措施,您无法从服务打开windows等。

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

是console.ReadLine在服务代码中不起作用。当您为您的服务编写代码时,我建议您对其进行划分,以便将代码的主要部分放入一个动态库中,您可以在服务和控制台应用程序中使用该库。控制台应用程序用于测试。服务代码不得执行任何类型的用户交互。

可能是OnStart中的Console.ReadLine?我删除了Console.ReadLine;mysql正在工作,也在更新,但现在SendFax不工作了。没有任何传真发送到传真控制台。当我将它作为一个控制台应用程序进行测试时,它工作得很好。你认为它是控制台吗。代码可能是阻止我的代码工作的原因?是的,Console对象是一个用户界面组件,因此在您有Console.Read/Write调用的地方,服务将无法运行。如果愿意,您可以改为登录到系统日志或将条目写入文件。