Ok, for the life of me, I just don't understand what's going on. I want to
use remoting to send messages from a windows service to a windows form app,
but I just can't get it to work. So, here's what I have so far.
I created a small, remotable object that inherits from MarshalByRefObject.
It has a public subroutine that raises an event with a message attached:
Public Delegate Sub TestServiceEventHandler(ByVal Message As String)
Public Class TestServiceMessage
Inherits MarshalByRefObject
Public Event TestServiceMessageEvent As TestServiceEventHandler
Public Overrides Function InitializeLifetimeService() As Object
Return Nothing
End Function
Public Sub SendMessage(ByVal Message As String)
DoMessageEvent(Message)
End Sub
Private Sub DoMessageEvent(ByVal Message As String)
RaiseEvent TestServiceMessageEvent(Message)
End Sub
End Class
I have a Windows Service that hosts the remotable object. The windows
service also has a timer on it, that when fired, creates a new instance of
the remotable object, and calls the send method:
Imports System.Runtime.Remoting
Imports System.Reflection
Imports Test_Service_Library
Imports System.IO
Public Class TestService
Private WithEvents m_Timer As New System.Timers.Timer(10000)
Protected Overrides Sub OnStart(ByVal args() As String)
' Add code here to start your service. This method should set things
' in motion so your service can do its work.
Dim RuntimePath As String =
Path.GetDirectoryName(Assembly.GetExecutingAssembl y.Location)
RemotingConfiguration.Configure(Path.Combine(Runti mePath, "Test
Service.exe.config"), False)
m_Timer.Enabled = True
End Sub
Protected Overrides Sub OnStop()
' Add code here to perform any tear-down necessary to stop your
service.
m_Timer.Enabled = False
End Sub
Private Sub m_Timer_Elapsed(ByVal sender As Object, ByVal e As
System.Timers.ElapsedEventArgs) Handles m_Timer.Elapsed
Dim tsMessage As New TestServiceMessage
tsMessage.SendMessage("Timer Ticked! - " & Now)
End Sub
End Class
Finally, I have a windows form application which acts as a remoting client.
When the form loads, it declares an instance of the remotable object, and
attaches an event handler on the object. Finally, when the event is raised,
it writes the message to a textbox on the form.
Imports Test_Service_Library
Imports System.Reflection
Imports System.IO
Imports System.Runtime.Remoting
Public Class Form1
Private WithEvents tsMessage As TestServiceMessage
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Dim RuntimePath As String =
Path.GetDirectoryName(Assembly.GetExecutingAssembl y.Location)
RemotingConfiguration.Configure(Path.Combine(Runti mePath, "Test
Service App.exe.config"), False)
End Sub
Private Sub tsMessage_TestServiceMessageEvent(ByVal Message As String)
Handles tsMessage.TestServiceMessageEvent
TextBox1.Text &= Message & vbNewLine
End Sub
End Class
The issue is that it doesn't work. I can debug the service once it's
installed, and I have verified that the timer works, and it creates an
instance of the remotable obect, which then raises the event. That seems to
be working fine. The problem is that the windows form application never
receives the event. What could be the possible problems??
Thanks for any help and suggestions.
-Jason