i have a problem when i try to run my windows service ..which is "Error 1053: The service did not respond to the start or control request in a timely fashion"
>after this i cannot anything with the service and have to restart the computer inorder for the service to be deleted.
>1) my service program is basically a client tht is listening on a port to a request from a server and establishing a new connection.
2) reading and extracting the zip file...deserial izing the objects and parameters of the function in the zip file sent
3)invoking the method using the object sent...and sending back a zipped result archive....
the code for my service is as follows...
using System;
using System.Collecti ons.Generic;
using System.Componen tModel;
using System.Data;
using System.Diagnost ics;
using System.ServiceP rocess;
using System.Text;
using System.IO;
using ServerApp;
using mytestservice.r eference1;
using ICSharpCode.Sha rpZipLib.Zip;
namespace mytestservice
{
public partial class Service1 : ServiceBase
{
protected override void OnStart(string[] args)
{
ServerProgram ts = new ServerProgram() ;
ts.registre();
}
protected override void OnStop()
{
}
}
}
the function registre and class ServerProgram are defined in a separate c#code file within the project which is......
using System;
using System.Collecti ons.Generic;
using System.Text;
using System.Diagnost ics;
using System.Reflecti on;
using System.Runtime. Serialization.F ormatters.Binar y;
using System.IO;
using System.Net;
using System.Net.Sock ets;
using ICSharpCode.Sha rpZipLib.Zip;
//using NSRemoteExecuto rTestObject;
namespace ServerApp
{
class ServerProgram
{
private const string SERIALIZED_OBJE CT_FILE_NAME = "SerializedObje ct.tlp";
private const string SERIALIZED_FUNC TION_PARAMETERS _FILE_NAME = "FunctionParame ters.tlp";
public void registre()
{
do
{
try
{
// Read the callback details to establish a new socket connection
//Console.WriteLi ne("hello");
string callbackMachine Name;
int callbackPortNum ber;
TcpListener listener = new TcpListener(440 0);
listener.Start( );
using (Socket serverSocket = listener.Accept Socket())
{
using (StreamReader callbackDetails Reader = new StreamReader(ne w NetworkStream(s erverSocket)))
{
// Read the machine name to which to send the result
callbackMachine Name = callbackDetails Reader.ReadLine ();
// Read the callback port number
callbackPortNum ber = Int32.Parse(cal lbackDetailsRea der.ReadLine()) ;
}
}
listener.Stop() ;
// Open a new connection on the port specified
char[] buffer = null;
string functionName;
StreamReader reader;
StreamWriter writer;
using (TcpClient connectionToRem oteProcessor = new TcpClient(callb ackMachineName, callbackPortNum ber))
{
reader = new StreamReader(co nnectionToRemot eProcessor.GetS tream());
//using (StreamReader reader = new StreamReader(co nnectionToRemot eProcessor.GetS tream()))
//{
// Read the function name to be invoked
functionName = String.Empty;
functionName = reader.ReadLine ();
Console.WriteLi ne("Function name : {0}", functionName);
// Read the size of the incoming file
int fileSize = Int32.Parse(rea der.ReadLine()) ;
Console.WriteLi ne("Filesize : {0}", fileSize);
// Read the entire file
buffer = new char[fileSize];
reader.Read(buf fer, 0, fileSize);
Console.WriteLi ne("File read");
//}
// Save the file to disk
string zipFileName = String.Format(" {0}.zip", DateTime.Now.Ti cks);
FileStream zipFileStream = File.Open(zipFi leName, FileMode.Create );
foreach (char c in buffer)
zipFileStream.W riteByte((byte) c);
zipFileStream.C lose();
Console.WriteLi ne("Zip file saved");
// Extract the contents
string currentExecutin gDirectory = Directory.GetPa rent(Assembly.G etExecutingAsse mbly().Location ).ToString();
FastZip fastZip = new FastZip();
fastZip.Extract Zip(zipFileName , currentExecutin gDirectory, "");
Console.WriteLi ne("Zip file extracted");
// Create a new binary formatter
BinaryFormatter bf = new BinaryFormatter ();
// Deserialize the invoking object
object invokingObject;
using (FileStream invokingObjectF ileStream = File.OpenRead(S erverProgram.SE RIALIZED_OBJECT _FILE_NAME))
{
invokingObject = bf.Deserialize( invokingObjectF ileStream);
}
Console.WriteLi ne("Object deserialized");
// Deserialize the parameters
Console.Write(" Parameters : ");
int numberOfParamet ers = Directory.GetFi les(currentExec utingDirectory, String.Format(" *_{0}", ServerProgram.S ERIALIZED_FUNCT ION_PARAMETERS_ FILE_NAME)).Len gth;
object[] parameters = new object[numberOfParamet ers];
for (int index = 0; index < numberOfParamet ers; ++index)
{
using (FileStream parametersFileS tream = File.OpenRead(S tring.Format("{ 0}_{1}", index, ServerProgram.S ERIALIZED_FUNCT ION_PARAMETERS_ FILE_NAME)))
{
parameters[index] = bf.Deserialize( parametersFileS tream);
Console.Write(" {0},", parameters[index]);
}
}
Console.WriteLi ne("\nParameter s deserialized");
// Invoke the method on the object
object result = invokingObject. GetType().Invok eMember(functio nName, BindingFlags.In vokeMethod, null, invokingObject, parameters);
Console.WriteLi ne("Method invoked: Result = {0}", result);
// Serialize the result
using (FileStream resultFileStrea m = File.Create("Re sultObject.tlp" ))
{
bf.Serialize(re sultFileStream, result);
}
Console.WriteLi ne("Result serialized");
// Compress the file
FastZip compressResult = new FastZip();
string resultZipFileNa me = String.Format(" Result_{0}", zipFileName);
compressResult. CreateZip(resul tZipFileName, currentExecutin gDirectory, false, "ResultObject.t lp");
Console.WriteLi ne("Compressed result");
// Send the file back to the requesting machine
byte[] writeBuffer;
using (FileStream zipFileReader = File.OpenRead(r esultZipFileNam e))
{
writeBuffer = new byte[zipFileReader.L ength];
zipFileReader.R ead(writeBuffer , 0, (int)zipFileRea der.Length);
}
char[] transmitBuffer = new char[writeBuffer.Len gth];
for (int index = 0; index < writeBuffer.Len gth; ++index)
transmitBuffer[index] = (char)(writeBuf fer[index]);
// Read the zip file containing serialized result object into memory
using (writer = new StreamWriter(co nnectionToRemot eProcessor.GetS tream()))
{
// Send the filesize of the entire package across
//writer.Write(wr iteBuffer.Lengt h);
//writer.Flush();
//Console.WriteLi ne("Sent filesize : {0}", writeBuffer.Len gth);
// Send the zip file containing the DLL's and serialized objects
writer.Write(tr ansmitBuffer);
writer.Flush();
Console.WriteLi ne("Sent result archive");
}
}
Console.WriteLi ne("Success!\n\ n");
}
catch (Exception exc)
{
EventLog.WriteE ntry("Paralleli zer", String.Format(" {0}\n{1}\n{2}\n {3}", exc.Message, exc.Data, exc.GetBaseExce ption().InnerEx ception, exc.StackTrace) , EventLogEntryTy pe.Error);
Console.WriteLi ne(exc.Message + "@" + exc.StackTrace) ;
Console.ReadKey ();
}
}
while (1 != 0);
}
}
}