此代码已在控制台应用程序中测试。它会将传真发送到Microsoft传真控制台并将更新查询发送到mysql数据库。我已经将代码转移到服务应用程序。 mysql更新正在工作,并将我的mysql表中的行更新为“DONE”,但传真未发送到传真控制台。
有什么想法吗?
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); }
}
}
}
}
答案 0 :(得分:1)
我们几乎不可能告诉您,因为您已经说过代码可以作为控制台应用程序运行(所以代码中没有任何代码可以导致它无法工作)。
我的直觉说这是一个权利问题,因为Windows服务作为用户运行的权限比普通用户少,调试和遇到任何引发的异常都应该告诉你相对较快的问题。
请参阅MSDN上的页面,Debugging a Windows Service或研究日志记录异常;在你写入控制台的那一刻,你无法看到它,因为它是一项服务。
对于你应该整理的一些事情有一些评论,并且遵循这些建议是一个好主意,但不应该与为什么传真没有被发送有关。如前所述,可能重用您的连接,或将其包装在using
语句中,以确保在不再需要时立即将其处理掉。
答案 1 :(得分:0)
我无法从上面的代码中看出传真服务器是否是以您的用户名运行的桌面应用程序。无论哪种方式,默认情况下,您的Windows服务都将安装在本地系统帐户下,并且该帐户无权访问您的用户帐户,因此无法访问仅为您的用户名安装的任何内容。您可以尝试更改Windows服务面板中的服务设置,以便您的服务以您的用户名运行。那会给你一个线索。
答案 2 :(得分:0)
由于它在控制台应用程序中运行良好,因此您的代码可能没有任何问题。这可能与服务依赖性有关。什么是FaxCOMLib?如果这是您机器中运行的另一项服务,那么您必须将其注册为您服务的依赖项。
您可以按如下方式注册服务(在命令行中):
sc config "NameOfYourService" depend= DependentServiceName
在您的情况下,从属服务名称可能是传真服务。