Hello folks,
I appologize for the cross post, but I really need an answer on this:
I do not think that I am seeing the whole picture here. I would like to
create a windows service and a management console, using Visual Basic 2003.
The windows service part, I think, is easy enough. I am more concerned with
the remoting aspect of the project. Below is the general idea of my
approach, please correct my where I am wrong.
I would really appreciate any assistance on this, thanks.
First, let me ask some questions:
1. Why have I not seen a model that uses Delegate functions, it would seem
to be better than wrapping everything inside eventargs?
2. Must I use events to ask the server for infomation from the client?
3. Must I specify <Serializable()> on all eventargs classes?
4. What precuations must I take to prevent conflicts caused when two
clients ask to modify the same information? Say for example, a request of
the server to modify the same row in a database at the same time.
5. How do I return a value from the server?
For this illustration, lets pretend that I am creating a service that
processes tasks based on a schedule. The service would simple have a loop
that waits for work to be done:
While (Not bStop and not bPause)
'Check for tasks to perform at this time
'Create entry, task starting 'Database Log Entry
'Process the task 'Calls StartTask <-- Invokes Plug-in's Start()
'Create entry, task complete + (success value) 'Success Result is
returned by task object
'Check if task is repeating task 'Read database schedule, to see if this
is a recurring task
'Update next run time/date
Wend
Meanwhile, administrators may wish to interact with the service:
--Run Task
--Cancel Task
--Create Task
--Enable Task
--Disable Task
--Edit Task
--Delete Task
--View Task History
Assmebly: SericeRemoting.dll
Public Interface ITaskServer
Delegate Sub RunTask(ByVal TaskID As Integer)
Event OnRunTask As RunTask
Delegate Sub StopTask(ByVal TaskID As Integer)
Event OnStopTask As StopTask
Delegate Sub NewTask(ByVal TI As TaskInfo)
Event OnNewTask As NewTask
Delegate Sub DeleteTask(ByVal TaskID As Integer)
Event OnDeleteTask As DeleteTask
Delegate Sub DisableTask(ByVal TaskID As Integer)
Event OnDisableTask As DisableTask
Delegate Sub EnableTask(ByVal TaskID As Integer)
Event OnEnableTask As EnableTask
Delegate Sub TaskItem(ByVal TIEA As TaskInfoEventArgs)
Event OnTaskItem As TaskItem
Delegate Sub ClearHistory(ByVal TaskID As Integer)
Event OnClearHistory As ClearHistory
Delegate Sub ViewHistory(ByVal THs As TaskHistories)
Event OnViewHistory As ViewHistory
End Interface
Public Interface ITaskClient
Sub RunTask(ByVal TaskID As Integer)
Sub StopTask(ByVal TaskID As Integer)
Function NewTask(ByVal TI As TaskInfo) As Integer
Sub DeleteTask(ByVal TaskID As Integer)
Sub DisableTask(ByVal TaskID As Integer)
Sub EnableTask(ByVal TaskID As Integer)
Function TaskItem(ByVal TaskID As Integer) As TaskInfo
Sub EditTaskItem(ByVal TI As TaskInfo)
Function ViewHistory(ByVal TaskID As Integer) As TaskHistories
Sub ClearHistory(ByVal TaskID As Integer)
End Interface
Public Class RemotedObject
Implements ITaskServer
Implements ITaskClient
Public Event OnClearHistory(ByVal TaskID As Integer) Implements
ITaskServer.OnClearHistory
Public Event OnDeleteTask(ByVal TaskID As Integer) Implements
ITaskServer.OnDeleteTask
Public Event OnDisableTask(ByVal TaskID As Integer) Implements
ITaskServer.OnDisableTask
Public Event OnEnableTask(ByVal TaskID As Integer) Implements
ITaskServer.OnEnableTask
Public Event OnNewTask(ByVal TI As TaskInfo) Implements
ITaskServer.OnNewTask
Public Event OnRunTask(ByVal TaskID As Integer) Implements
ITaskServer.OnRunTask
Public Event OnStopTask(ByVal TaskID As Integer) Implements
ITaskServer.OnStopTask
Public Event OnTaskItem(ByVal TIEA As TaskInfoEventArgs) Implements
ITaskServer.OnTaskItem
Public Event OnViewHistory(ByVal THs As TaskHistories) Implements
ITaskServer.OnViewHistory
#Region " ITaskClient"
Public Sub ClearHistory(ByVal TaskID As Integer) Implements
ITaskClient.ClearHistory
RaiseEvent OnClearHistory(TaskID)
End Sub
Public Sub DeleteTask(ByVal TaskID As Integer) Implements
ITaskClient.DeleteTask
RaiseEvent OnDeleteTask(TaskID)
End Sub
Public Sub DisableTask(ByVal TaskID As Integer) Implements
ITaskClient.DisableTask
RaiseEvent OnDisableTask(TaskID)
End Sub
Public Sub EditTaskItem(ByVal TI As TaskInfo) Implements
ITaskClient.EditTaskItem
Dim TIEA As TaskInfoEventArgs
TIEA = New TaskInfoEventArgs(TI)
RaiseEvent OnTaskItem(tiea)
End Sub
Public Sub EnableTask(ByVal TaskID As Integer) Implements
ITaskClient.EnableTask
RaiseEvent OnEnableTask(TaskID)
End Sub
Public Function NewTask(ByVal TI As TaskInfo) As Integer Implements
ITaskClient.NewTask
RaiseEvent OnNewTask(TI)
Return TI.TaskID
End Function
Public Sub RunTask(ByVal TaskID As Integer) Implements ITaskClient.RunTask
RaiseEvent OnRunTask(TaskID)
End Sub
Public Sub StopTask(ByVal TaskID As Integer) Implements
ITaskClient.StopTask
RaiseEvent OnStopTask(TaskID)
End Sub
Public Function TaskItem(ByVal TaskID As Integer) As TaskInfo Implements
ITaskClient.TaskItem
Dim TIEA As TaskInfoEventArgs
TIEA = New TaskInfoEventArgs(TaskID)
RaiseEvent OnTaskItem(TIEA)
Return TIEA.TaskInfo
End Function
Public Function ViewHistory(ByVal TaskID As Integer) As TaskHistories
Implements ITaskClient.ViewHistory
Dim THs As TaskHistories
THs = New TaskHistories(TaskID)
RaiseEvent OnViewHistory(THs)
Return THs
End Function
#End Region
End Class
Assmebly: ClientConsole.exe
'Code truncated for brevity
Public Class ClientConsumer
Private mRemoteService As ITaskClient
Private Delegate Sub ClearHistoryDelegate(ByVal TaskID As Integer)
Private Delegate Function NewTaskDelegate(ByVal TaskInfo As TaskInfo) As
Integer
Public Sub New()
Dim ServerPort As Integer = 5555
ChannelServices.RegisterChannel(New TcpChannel)
mRemoteService =
Activator.GetObject(GetType(SericeRemoting.Remoted Object),
String.Format("tcp://localhost:{0}/RemotedObject", ServerPort.ToString))
End Sub
Public Sub ClearHistory(ByVal TaskID As Integer)
Dim cb As AsyncCallback = New AsyncCallback(AddressOf ClearHistoryBack)
Dim d As ClearHistoryDelegate = New ClearHistoryDelegate(AddressOf
mRemoteService.ClearHistory)
Dim ar As IAsyncResult = d.BeginInvoke(TaskID, cb, Nothing)
End Sub
Private Sub ClearHistoryBack(ByVal ar As IAsyncResult)
Dim d As ClearHistoryDelegate = CType((CType(ar,
System.runtime.Remoting.Messaging.AsyncResult)).As yncDelegate,
ClearHistoryDelegate)
Try
d.EndInvoke(ar)
Trace.WriteLine("Success for Clear History")
Catch ex As Exception
Trace.WriteLine(ex.ToString)
End Try
End Sub
Public Function NewTask(ByVal TaskInfo As TaskInfo) As Integer
Dim cb As AsyncCallback = New AsyncCallback(AddressOf NewTaskBack)
Dim d As NewTaskDelegate = New NewTaskDelegate(AddressOf
mRemoteService.NewTask)
Dim ar As IAsyncResult = d.BeginInvoke(TaskInfo, cb, Nothing)
'how do I return the value?
End Function
Public Sub NewTaskBack(ByVal ar As IAsyncResult)
Dim d As NewTaskDelegate = CType((CType(ar,
System.runtime.Remoting.Messaging.AsyncResult)).As yncDelegate,
NewTaskDelegate)
Try
d.EndInvoke(ar)
Trace.WriteLine("Success for new task")
' How do I return the value?
'
'
Catch ex As Exception
Trace.WriteLine(ex.ToString)
End Try
End Sub
End Class