Reputation: 603
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); }
}
}
}
}
Upvotes: 0
Views: 393
Reputation: 234
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):
sc config "NameOfYourService" depend= DependentServiceName
In your case the dependent service name may be the Fax Service.
Upvotes: 0
Reputation: 3985
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.
Upvotes: 0
Reputation: 32438
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.
Upvotes: 1