控制台代码在服务中不起作用
该代码在控制台应用程序中进行了测试。它将向 microsoft 传真控制台发送传真并向 mysql 数据库发送更新查询。我已将代码转移到服务应用程序。 mysql 更新正在工作并将我的 mysql 表中的行更新为“完成”,但传真未发送到传真控制台。
有什么想法吗?
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); }
}
}
}
}
This code was tested in a console application. It would send a fax to the microsoft Fax Console and send an update query to the mysql database. I have since transferred the code to a service application. The mysql update is working and updating rows in my mysql table as "DONE", but the faxes are not being sent to the Fax Console.
Any ideas?
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); }
}
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
鉴于您已经说过代码作为控制台应用程序工作(因此代码中可能没有任何内容导致它无法工作),我们几乎不可能告诉您。
我的直觉告诉我这是一个权限问题,因为 Windows 服务以比普通用户更少权限的用户运行,调试和遇到任何引发的异常应该会相对较快地告诉您问题是什么。
请参阅 MSDN 上的页面,调试 Windows 服务< /a> 或研究日志记录异常;目前您正在写入控制台,您看不到它,因为它是一项服务。
对于您应该整理的一些事情有一些评论,遵循这些建议将是一个好主意,但不应该与传真未发送的原因相关。如前所述,也许可以重用您的连接,或者将其包装在
using
语句确保它在不再需要时立即被处置。It's pretty much impossible for us to tell given you've said the code works as a console application (so probably nothing in code is causing it not to work).
My instinct says it's a rights issue, as a windows service runs as a user with fewer rights than a normal user, debugging and encountering any exceptions raised should tell you what the issue is relatively quickly.
See the page on MSDN, Debugging a Windows Service or research logging exceptions; at the moment you are writing to console, which you can't see as it's a service.
There have been a few comments on a few things you should tidy up, and following that advice would be a good idea, but shouldn't be related to why faxes aren't getting sent. As has been said, maybe reuse your connection, or wrap it in a
using
statement to ensure it gets disposed of as soon as it's no longer needed.我无法从上面的代码判断传真服务器是否是在您的用户名下运行的桌面应用程序。无论哪种方式,默认情况下,您的 Windows 服务都将安装在本地系统帐户下,并且该帐户无权访问您的用户帐户,因此它无法访问仅为您的用户名安装的任何内容。您可以尝试更改 Windows 服务面板中的服务设置,以便您的服务在您的用户名下运行。这会给你一个线索。
I can't tell from your code above if the fax server is a desktop application that runs under your user name or not. Either way, your Windows service will install under Local System account by default, and that account does not have access to YOUR user account, so it can't access anything that was installed just for your user name. You could try changing the service settings in the Windows service panel so your service runs under your user name. That will give you a clue.
由于它在控制台应用程序中运行良好,因此您的代码可能没有任何问题。这可能与服务依赖性有关。什么是 FaxCOMLib?如果这是您计算机中运行的另一个服务,那么您必须将其注册为您的服务的依赖项。
您可以按如下方式注册服务(在命令行中):
在您的情况下,依赖的服务名称可能是传真服务。
As it's working fine in a console app, there could be nothing wrong with your code. This might be something related to service dependencies. What is the FaxCOMLib? if that's another service running in your machine then you must register that as a dependency to your service.
You can register a service as follows (in command line):
In your case the dependent service name may be the Fax Service.