次の Windows サービス コードはcopyej()
、必要なタスクを完了するメソッドを呼び出そうとしていますが、タイマーでこの関数を呼び出すと、次のエラーがスローされます。
名前 copyej() は現在のコンテキストに存在しません
public クラスで定義されているため、この問題が発生する理由は不明ですが、完全なコードは次のとおりです。
/** Using Config File for Directories **/
using System.Configuration;
//====Read From Access namespace===============
using System.Data.Common;
using System.Data.OleDb;
//using System.IO;
//=============================================
using System.ServiceModel;
using System.ServiceProcess;
using System.Configuration.Install;
namespace Microsoft.ServiceModel.Samples
{
// Define a service contract.
[ServiceContract(Namespace = "http://Microsoft.ServiceModel.Samples")]
public interface Copytask
{
[OperationContract]
void copyej();
}
public class CopyejService : Copytask
{
// Implement the Copy EJ methods.
// CopyejService copyej = new CopyejService();
public void copyej()
{
// Functionality Derivation
string target_dir_file = System.Configuration.ConfigurationSettings.AppSettings["TargetDir_File"];
string target_dir = System.Configuration.ConfigurationSettings.AppSettings["TargetDir"];
string text_dir = System.Configuration.ConfigurationSettings.AppSettings["TextDir"];
string file_name = System.Configuration.ConfigurationSettings.AppSettings["FileName"];
string source_dir_file = System.Configuration.ConfigurationSettings.AppSettings["SourceDir_File"];
// Check if Target Directory Exists
DirectoryInfo theFolder = new DirectoryInfo(target_dir);
if (!theFolder.Exists)
{
theFolder.Create();
}
/*
if (!File.Exists(target_dir_file))
{
// MessageBox.Show("Function Exited");
return;
}*/
// Delet if EJ file exists
if (File.Exists(target_dir_file))
{
File.Delete(target_dir_file);
}
// Copy the EJ file in Target Directory
File.Copy(source_dir_file, target_dir_file);
//=============Extract contents in Access and save it as Text File Format==========================================
string connectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\test\TestEJFolder\BWC_Ejournal.mdb";
OleDbConnection conn = new OleDbConnection(connectionString);
OleDbConnection conn1 = new OleDbConnection(connectionString);
OleDbConnection conn2 = new OleDbConnection(connectionString);
string sql = "SELECT * FROM Events";
string dt = "SELECT TOP 1 [Date] FROM Events";
string count = "SELECT COUNT(ID) FROM Events";
OleDbCommand cmd = new OleDbCommand(sql, conn);
conn.Open();
OleDbDataReader reader;
reader = cmd.ExecuteReader();
OleDbCommand cmd1 = new OleDbCommand(dt, conn1);
conn1.Open();
OleDbCommand cmd2 = new OleDbCommand(count, conn2);
conn2.Open();
string time_stmp = Convert.ToDateTime(cmd1.ExecuteScalar()).ToString("yyyyMMddhhmmss");
string s_path = text_dir + "\\" + time_stmp + "_" + "Session" + "_" + file_name;
FileInfo oFileInfo = new FileInfo(source_dir_file);
string fname = "File Name: \"" + oFileInfo.Name + "|" + " ";
string fsize = "File total Size: " + oFileInfo.Length.ToString() + "|" + " ";
string fdts = "Date and Time File Created: " + oFileInfo.CreationTime.ToString() + "|" + " ";
Int32 r_count = (Int32)cmd2.ExecuteScalar();
StreamWriter sp = File.CreateText(s_path);
sp.WriteLine(fname + fsize + fdts + "Record Count " + r_count);
sp.Close();
conn1.Close();
conn2.Close();
string path = text_dir + "\\" + time_stmp + "_" + file_name;
StreamWriter sw = File.CreateText(path);
const string format = "{0,-22} {1,-4} {2,-4} {3,-4} {4,-20} {5,-22}";
string line;
while (reader.Read())
{
line = string.Format(format, reader.GetDateTime(5).ToString(@"dd-MM-yyyy HH:mm:ss").Trim(),
reader.GetInt32(0).ToString().Trim(),
reader.GetInt32(1).ToString().Trim(),
reader.GetInt32(2).ToString().Trim(),
reader.GetString(3).ToString().Trim(),
reader.GetString(4).ToString().Trim());
sw.WriteLine(line);
}
reader.Close();
conn.Close();
sw.Close();
sw.Dispose();
//====End Of Extract Access contents and save it as Text Format=========================================================
}
}
public class CopyEJWindowsService : ServiceBase
{
public ServiceHost serviceHost = null;
public CopyEJWindowsService()
{
// Name the Windows Service
ServiceName = "CopyEJWindowsService";
}
public static void Main()
{
ServiceBase.Run(new CopyEJWindowsService());
}
private System.Timers.Timer serviceTimer = new System.Timers.Timer();
protected override void OnStart(string[] args)
{
InitTimer();
}
protected override void OnStop()
{
serviceTimer.Stop();
}
private void InitTimer()
{
serviceTimer.Start();
serviceTimer.Enabled = true;
serviceTimer.Interval = 5000;
serviceTimer.Elapsed += new ElapsedEventHandler(OnServiceTimerElapsed);
}
public void OnServiceTimerElapsed(object sender, ElapsedEventArgs args)
{
serviceTimer.Enabled = false;
try
{
copyej();
}
catch (Exception ex)
{
//Handle exception
}
finally
{
serviceTimer.Enabled = true;
}
}
}
// Start the Windows service.
/*
protected override void OnStart(string[] args)
{
if (serviceHost != null)
{
serviceHost.Close();
}
// Create a ServiceHost for the CopyEJService type and
// provide the base address.
serviceHost = new ServiceHost(typeof(CopyejService));
// Open the ServiceHostBase to create listeners and start
// listening for messages.
serviceHost.Open();
}
protected override void OnStop()
{
if (serviceHost != null)
{
serviceHost.Close();
serviceHost = null;
}
}
}*/
[RunInstaller(true)]
public class ProjectInstaller : Installer
{
private ServiceProcessInstaller process;
private ServiceInstaller service;
public ProjectInstaller()
{
process = new ServiceProcessInstaller();
process.Account = ServiceAccount.LocalSystem;
service = new ServiceInstaller();
service.ServiceName = "CopyEJWindowsService";
Installers.Add(process);
Installers.Add(service);
}
}
}