.Net event not firing when run in Windows service app.

EricTViking
EricTViking used Ask the Experts™
on
Hi,

I have a bit of code that (vb.net VS2008 SP1) that connects to a Queue in MSMQ and creates an event handler using the asynchronous BeginPeek method.

When an item appears in the queue, the MyPeekCompleted event doesn't get called.

If I run the code in a Win Forms app it works fine, if I run the same code within a Windows service the MyPeekCompleted event never gets called.

Any ideas why it fails when running as a service?
Public Class MyClass
  Public q As MessageQueue = Nothing

  public sub New()
      q = New MessageQueue(".\Private$\MyQueue")
      AddHandler q.PeekCompleted, AddressOf MyPeekCompleted
      q.BeginPeek()
  end sub

  Public Sub MyPeekCompleted(ByVal [source] As [Object], _
                             ByVal asyncResult As   PeekCompletedEventArgs)
    ' **** THIS NEVER GETS CALLED WHEN RUNNING AS A WINDOWS SERVICE ****
  End Sub 
End Class

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Most Valuable Expert 2012
Top Expert 2014

Commented:
The service runs under system account. May be the queue is not available under that account especially since its a private queue.
Top Expert 2008

Author

Commented:
Thanks.

Private just means the queue is on the local machine, not that it's private from anyone.

Have created the queue with everone set to full permissions and also tried running the service under various accounts including administrator.
Most Valuable Expert 2012
Top Expert 2014
Commented:
What code do you have in the event handler?
Expert Spotlight: Joe Anderson (DatabaseMX)

We’ve posted a new Expert Spotlight!  Joe Anderson (DatabaseMX) has been on Experts Exchange since 2006. Learn more about this database architect, guitar aficionado, and Microsoft MVP.

Top Expert 2008

Author

Commented:
can't reprodocude all the code but it's basically this:

Public Sub MyPeekCompleted(ByVal [source] As [Object], ByVal asyncResult As PeekCompletedEventArgs)
    EventLog.WriteEntry("Application", _
    "Service.MyPeekCompleted() GotQMessage", EventLogEntryType.Warning)

    ' Connect to the queue.
    Dim mq As MessageQueue = CType([source], MessageQueue)

    ' ...Do other stuff
End Sub


But the first line doesn't fire to write to the event log.
Most Valuable Expert 2012
Top Expert 2014

Commented:
Yeah I just wanted to see you have code other than messagebox.show :-)

Try writing to file instead.
Top Expert 2008

Author

Commented:
No UI code in there, just a bit of code that moves some data around.

Have attached the debugger and the MyPeekCompleted method doesn't get called at all, writing to file won;t help there.
Mike TomlinsonHigh School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
Top Expert 2009

Commented:
Where are you instantiating and storing your reference to MyClass in your service?
Top Expert 2008

Author

Commented:
The class is declared in the main service class, and is instantiated in the OnStart() event.
Public Class svcMain
    Private MySvc As MyClass

    Protected Overrides Sub OnStart(ByVal args() As String)
        Try
             MySvc = New MyClass
        Catch ex As Exception
            EventLog.WriteEntry("Application", _
                                "OnStart() " & ex.Message, EventLogEntryType.Error)
            Throw ex
        End Try
    End Sub

    Protected Overrides Sub OnStop()
        ' Add code here to perform any tear-down necessary to stop your service.
    End Sub
End Class

Open in new window

Top Expert 2008

Author

Commented:
Some more info:

I get the same problem on Windows 7 Professional 64 bit, and on Windows XP Professional 32 bit. Thought it might be a 64 bit issue but no luck there.

I believe that MSMQ actually makes the callback to the MyPeekCompleted method in my class from the thread it is running in, so it's possible that there is a security issue there? However running the service with different permissions doesn't help. And like I said the code works perfectly when run within a Winforms app.

Please keep the suggestions coming.
Top Expert 2008

Author

Commented:
Another update:

This problem didn't make much sense and everything looked liek it should work so I created a new service project with just the code needed to reproduce the bug. The new service works perfectly and the MyPeekCompleted gets called when an item hits the queue.

So looks like the problem is some king of corruption in the project itself. Will carry on re-creating the project and let you knwo if I finally nail what was causing the problem.

Thanks for all your help so far.
Top Expert 2008
Commented:
Finally solved it after much brute force debugging.

The problem turned out to be a DLL missing from the workign directory of the service executable.

In the MyPeekCompleted event there is a reference to an object in another DLL. When the project was run as a Winforms app it was run in the VS IDE and had the reference to the DLL. However when the project was deployed as a service the DLL was not present causing the problem.

The reason the bug was difficult to find is that because the MyPeekCompleted event is actually called by MSMQ. I'm guessing that MSMQ must have to link the required DLLs *before* it calls the MyPeekCompleted event, and if a DLL is missing it just doesn't call the event.

Problem is that MSMQ doesn't throw or log any errors, and neither does the Windows service.

In summary, if your MyPeekCompleted event isn't being fired, try commentign out all the code it contains to eliminate any silent MSQM exceptions.

Thanks to all that contributed!
Top Expert 2008

Author

Commented:
Ended up solving it myself, but thanks to all that participated.

500 points to CodeCruiser for getting closest to the root of the problem =)

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial