469,945 Members | 1,816 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,945 developers. It's quick & easy.

windows service problem

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...deserializing 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.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.ServiceProcess;
using System.Text;
using System.IO;

using ServerApp;

using mytestservice.reference1;
using ICSharpCode.SharpZipLib.Zip;
namespace mytestservice
public partial class Service1 : ServiceBase

protected override void OnStart(string[] args)

ServerProgram ts = new ServerProgram();


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.Collections.Generic;
using System.Text;

using System.Diagnostics;
using System.Reflection;
using System.Runtime.Serialization.Formatters.Binary;

using System.IO;
using System.Net;
using System.Net.Sockets;

using ICSharpCode.SharpZipLib.Zip;
//using NSRemoteExecutorTestObject;

namespace ServerApp

class ServerProgram

private const string SERIALIZED_OBJECT_FILE_NAME = "SerializedObject.tlp";
private const string SERIALIZED_FUNCTION_PARAMETERS_FILE_NAME = "FunctionParameters.tlp";

public void registre()
// Read the callback details to establish a new socket connection
string callbackMachineName;
int callbackPortNumber;
TcpListener listener = new TcpListener(4400);
using (Socket serverSocket = listener.AcceptSocket())
using (StreamReader callbackDetailsReader = new StreamReader(new NetworkStream(serverSocket)))
// Read the machine name to which to send the result
callbackMachineName = callbackDetailsReader.ReadLine();

// Read the callback port number
callbackPortNumber = Int32.Parse(callbackDetailsReader.ReadLine());

// Open a new connection on the port specified
char[] buffer = null;
string functionName;
StreamReader reader;
StreamWriter writer;
using (TcpClient connectionToRemoteProcessor = new TcpClient(callbackMachineName, callbackPortNumber))
reader = new StreamReader(connectionToRemoteProcessor.GetStream ());
//using (StreamReader reader = new StreamReader(connectionToRemoteProcessor.GetStream ()))
// Read the function name to be invoked
functionName = String.Empty;
functionName = reader.ReadLine();
Console.WriteLine("Function name : {0}", functionName);

// Read the size of the incoming file
int fileSize = Int32.Parse(reader.ReadLine());
Console.WriteLine("Filesize : {0}", fileSize);

// Read the entire file
buffer = new char[fileSize];
reader.Read(buffer, 0, fileSize);
Console.WriteLine("File read");

// Save the file to disk
string zipFileName = String.Format("{0}.zip", DateTime.Now.Ticks);
FileStream zipFileStream = File.Open(zipFileName, FileMode.Create);
foreach (char c in buffer)
Console.WriteLine("Zip file saved");

// Extract the contents
string currentExecutingDirectory = Directory.GetParent(Assembly.GetExecutingAssembly( ).Location).ToString();
FastZip fastZip = new FastZip();
fastZip.ExtractZip(zipFileName, currentExecutingDirectory, "");
Console.WriteLine("Zip file extracted");

// Create a new binary formatter
BinaryFormatter bf = new BinaryFormatter();

// Deserialize the invoking object
object invokingObject;
using (FileStream invokingObjectFileStream = File.OpenRead(ServerProgram.SERIALIZED_OBJECT_FILE _NAME))
invokingObject = bf.Deserialize(invokingObjectFileStream);
Console.WriteLine("Object deserialized");

// Deserialize the parameters
Console.Write("Parameters : ");
int numberOfParameters = Directory.GetFiles(currentExecutingDirectory, String.Format("*_{0}", ServerProgram.SERIALIZED_FUNCTION_PARAMETERS_FILE_ NAME)).Length;
object[] parameters = new object[numberOfParameters];
for (int index = 0; index < numberOfParameters; ++index)
using (FileStream parametersFileStream = File.OpenRead(String.Format("{0}_{1}", index, ServerProgram.SERIALIZED_FUNCTION_PARAMETERS_FILE_ NAME)))
parameters[index] = bf.Deserialize(parametersFileStream);
Console.Write("{0},", parameters[index]);
Console.WriteLine("\nParameters deserialized");

// Invoke the method on the object
object result = invokingObject.GetType().InvokeMember(functionName , BindingFlags.InvokeMethod, null, invokingObject, parameters);
Console.WriteLine("Method invoked: Result = {0}", result);

// Serialize the result
using (FileStream resultFileStream = File.Create("ResultObject.tlp"))
bf.Serialize(resultFileStream, result);
Console.WriteLine("Result serialized");

// Compress the file
FastZip compressResult = new FastZip();
string resultZipFileName = String.Format("Result_{0}", zipFileName);
compressResult.CreateZip(resultZipFileName, currentExecutingDirectory, false, "ResultObject.tlp");
Console.WriteLine("Compressed result");

// Send the file back to the requesting machine
byte[] writeBuffer;
using (FileStream zipFileReader = File.OpenRead(resultZipFileName))
writeBuffer = new byte[zipFileReader.Length];
zipFileReader.Read(writeBuffer, 0, (int)zipFileReader.Length);

char[] transmitBuffer = new char[writeBuffer.Length];
for (int index = 0; index < writeBuffer.Length; ++index)
transmitBuffer[index] = (char)(writeBuffer[index]);

// Read the zip file containing serialized result object into memory
using (writer = new StreamWriter(connectionToRemoteProcessor.GetStream ()))
// Send the filesize of the entire package across
//Console.WriteLine("Sent filesize : {0}", writeBuffer.Length);

// Send the zip file containing the DLL's and serialized objects
Console.WriteLine("Sent result archive");
catch (Exception exc)
EventLog.WriteEntry("Parallelizer", String.Format("{0}\n{1}\n{2}\n{3}", exc.Message, exc.Data, exc.GetBaseException().InnerException, exc.StackTrace), EventLogEntryType.Error);
Console.WriteLine(exc.Message + "@" + exc.StackTrace);

while (1 != 0);


Apr 2 '08 #1
0 1343

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

4 posts views Thread by Kris | last post: by
reply views Thread by Scott Davies | last post: by
10 posts views Thread by Ger | last post: by
reply views Thread by Charles Leonard | last post: by
1 post views Thread by Mahesh Devjibhai Dhola | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.