Service won't start (udp listener)

S

Snedker

Hi folks,
If I outcomment the last four lines the service will start.
If I in-comment the line

"Dim recv As Integer = sock.ReceiveFrom(data, ep)"

the service will fail starting with error code 3534 (the service
reported no errors). Neither are errors written to the event log.

If I run the same code from a console application, everything works
fine.

The code:
Protected Overloads Overrides Sub OnStart(args As String())
Dim iep As New IPEndPoint(IPAddress.Any, 9050)
sock.Bind(iep)
Dim ep As EndPoint = DirectCast(iep, EndPoint)
Dim data As Byte() = New Byte(1024) {}
Dim recv As Integer = sock.ReceiveFrom(data, ep)
Dim stringData As String = Encoding.ASCII.GetString(data, 0, recv)
data = New Byte(1024) {}
End Sub

Any ideas why that particular line prevents service from starting?

Regards
Morten Snedker
 
S

sloan

Who is running the service? Who is running the Console app?

Here is some crappy code to figure it out, its C# but the translation should
be simple.

private string FindIIdentity()

{

try

{

//'Dim user As WindowsPrincipal =
CType(System.Threading.Thread.CurrentPrincipal, WindowsPrincipal)

//'Dim ident As IIdentity = user.Identity

string returnValue = string.Empty;



WindowsIdentity ident = WindowsIdentity.GetCurrent();

returnValue = ident.Name;

try

{

returnValue += " on " + System.Environment.MachineName;

}

catch (Exception ex)

{

Conditionals.Trace.WriteMessage(ex.Message);

}

return returnValue;

}



catch (Exception ex)

{

Conditionals.Trace.WriteMessage(ex.Message);

return "Error Finding Identity";

}

}
 
S

Snedker

Who is running the service?  Who is running the Console app?

The console app is run with my login account (K1\Snedker).
The service is run with the built-in SYSTEM-account.

This short piece of code makes the service fail starting, as well:

Protected Overloads Overrides Sub OnStart(args As String())
Dim ident As WindowsIdentity = WindowsIdentity.GetCurrent()
Dim s As String = ident.Name

Dim sr As New StreamWriter("e:\tmp.txt")
End Sub

Out-commenting the StreamWriter line will make it work.

So what I suppose you're getting at, is that the SYSTEM account may
lack the necessary credentials? If so, how ought I to get around it?

Thanks for your time and effort!


Regards /Snedker
 
S

sloan

//So what I suppose you're getting at, is that the SYSTEM account may
lack the necessary credentials? //

There ya go.

Provide the account with the necessary permissions.






Who is running the service? Who is running the Console app?

The console app is run with my login account (K1\Snedker).
The service is run with the built-in SYSTEM-account.

This short piece of code makes the service fail starting, as well:

Protected Overloads Overrides Sub OnStart(args As String())
Dim ident As WindowsIdentity = WindowsIdentity.GetCurrent()
Dim s As String = ident.Name

Dim sr As New StreamWriter("e:\tmp.txt")
End Sub

Out-commenting the StreamWriter line will make it work.

So what I suppose you're getting at, is that the SYSTEM account may
lack the necessary credentials? If so, how ought I to get around it?

Thanks for your time and effort!


Regards /Snedker
 
S

Snedker

//So what I suppose you're getting at, is that the SYSTEM account may
lack the necessary credentials? //

There ya go.

Provide the account with the necessary permissions.

The problem persists even when service is run with administrative
rights...? I feel like i'm on a wild goose chase here.

/Snedker
 
S

sloan

You probably need to capture the exception and log it to a txt file or
something.

Below is sample code.



Create a directory called
C:\WUTEMP\

Give read/write rights to SYSTEM account.

Change your code to the below:



Try '' <<NEW CODE

Dim iep As New IPEndPoint(IPAddress.Any, 9050)
sock.Bind(iep)
Dim ep As EndPoint = DirectCast(iep, EndPoint)
Dim data As Byte() = New Byte(1024) {}
Dim recv As Integer = sock.ReceiveFrom(data, ep)
Dim stringData As String = Encoding.ASCII.GetString(data, 0, recv)
data = New Byte(1024) {}

Catch ex as Exception '' <<NEW CODE
''Hey, something bad happened, lets log it. '' <<NEW CODE
LogException(ex) '' <<NEW CODE

End Try '' <<NEW CODE




Private Sub LogException(ex As Exception)
Dim popTextFile As Boolean = True ''read this value from app.config in
the future maybe(??)
Dim generateLogFile As Boolean = True''read this value from app.config in
the future maybe(??)


Dim longExceptionMessage As String = String.Empty

Dim nestedEx As Exception = ex
While Not (nestedEx Is Nothing)
longExceptionMessage += " ::: " + ex.Message + " :: " + ex.StackTrace
nestedEx = nestedEx.InnerException
End While


If popTextFile Or generateLogFile Then
Dim fileName As String = WriteToTempFile(String.Empty,
longExceptionMessage)
If popTextFile Then
System.Diagnostics.Process.Start(fileName)
Else
Console.WriteLine(fileName)
End If
End If
End Sub 'LogException




Private Function WriteToTempFile(prefix As String, toWrite As String) As
String
' string fileName = System.IO.Path.GetTempFileName();
' fileName = fileName.Replace(".tmp", ".txt");

Dim fileName As String = "C:\WUTEMP\" + Guid.NewGuid().ToString("N") +
".txt" '' give it a guaranteed unique name
Return WriteAFile(fileName, prefix, toWrite)

End Function 'WriteToTempFile



Private Function WriteAFile(fileName As String, prefix As String, toWrite As
String) As String

Dim writer As System.IO.StreamWriter = Nothing
Dim fs As System.IO.FileStream = Nothing

Try


fs = New System.IO.FileStream(fileName, System.IO.FileMode.Append,
System.IO.FileAccess.Write)
' Opens stream and begins writing
writer = New System.IO.StreamWriter(fs)
writer.BaseStream.Seek(0, System.IO.SeekOrigin.End)
writer.WriteLine(toWrite)
writer.Flush()

Return fileName

Finally
If Nothing <> writer Then
writer.Close()
End If
If Nothing <> fs Then
fs.Close()
End If
End Try
End Function 'WriteAFile
 
C

Chris

Hi folks,
If I outcomment the last four lines the service will start.
If I in-comment the line

"Dim recv As Integer = sock.ReceiveFrom(data, ep)"

the service will fail starting with error code 3534 (the service
reported no errors). Neither are errors written to the event log.

If I run the same code from a console application, everything works
fine.

The code:
Protected Overloads Overrides Sub OnStart(args As String())
Dim iep As New IPEndPoint(IPAddress.Any, 9050)
sock.Bind(iep)
Dim ep As EndPoint = DirectCast(iep, EndPoint)
Dim data As Byte() = New Byte(1024) {}
Dim recv As Integer = sock.ReceiveFrom(data, ep)
Dim stringData As String = Encoding.ASCII.GetString(data, 0, recv)
data = New Byte(1024) {}
End Sub

Any ideas why that particular line prevents service from starting?

Regards
Morten Snedker

You should not be doing any work in your OnStart event. The OnStart
event should normally kick off a thread to do the actual work and then
quit. Otherwise, the Service Control Manager will throw an error
because the OnStart method did not return in a timely manner.

Chris
 
S

Snedker

Please do not multi-post:

Well, there was a 24-hour time span between the postings, and being
near deadline I was getting a bit frustrated. But your point is noted.

/Snedker
 
S

Snedker

On Jul 27, 10:02 am, Snedker <[email protected]> wrote:
You should not be doing any work in your OnStart event.  The OnStart
event should normally kick off a thread to do the actual work and then
quit.  Otherwise, the Service Control Manager will throw an error
because the OnStart method did not return in a timely manner.

Chris

That was exactly it. Thanks!

/Snedker
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top