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(ByVa l TaskID As Integer)
Event OnDeleteTask As DeleteTask
Delegate Sub DisableTask(ByV al TaskID As Integer)
Event OnDisableTask As DisableTask
Delegate Sub EnableTask(ByVa l TaskID As Integer)
Event OnEnableTask As EnableTask
Delegate Sub TaskItem(ByVal TIEA As TaskInfoEventAr gs)
Event OnTaskItem As TaskItem
Delegate Sub ClearHistory(By Val TaskID As Integer)
Event OnClearHistory As ClearHistory
Delegate Sub ViewHistory(ByV al 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(ByVa l TaskID As Integer)
Sub DisableTask(ByV al TaskID As Integer)
Sub EnableTask(ByVa l TaskID As Integer)
Function TaskItem(ByVal TaskID As Integer) As TaskInfo
Sub EditTaskItem(By Val TI As TaskInfo)
Function ViewHistory(ByV al TaskID As Integer) As TaskHistories
Sub ClearHistory(By Val TaskID As Integer)
End Interface
Public Class RemotedObject
Implements ITaskServer
Implements ITaskClient
Public Event OnClearHistory( ByVal TaskID As Integer) Implements
ITaskServer.OnC learHistory
Public Event OnDeleteTask(By Val TaskID As Integer) Implements
ITaskServer.OnD eleteTask
Public Event OnDisableTask(B yVal TaskID As Integer) Implements
ITaskServer.OnD isableTask
Public Event OnEnableTask(By Val TaskID As Integer) Implements
ITaskServer.OnE nableTask
Public Event OnNewTask(ByVal TI As TaskInfo) Implements
ITaskServer.OnN ewTask
Public Event OnRunTask(ByVal TaskID As Integer) Implements
ITaskServer.OnR unTask
Public Event OnStopTask(ByVa l TaskID As Integer) Implements
ITaskServer.OnS topTask
Public Event OnTaskItem(ByVa l TIEA As TaskInfoEventAr gs) Implements
ITaskServer.OnT askItem
Public Event OnViewHistory(B yVal THs As TaskHistories) Implements
ITaskServer.OnV iewHistory
#Region " ITaskClient"
Public Sub ClearHistory(By Val TaskID As Integer) Implements
ITaskClient.Cle arHistory
RaiseEvent OnClearHistory( TaskID)
End Sub
Public Sub DeleteTask(ByVa l TaskID As Integer) Implements
ITaskClient.Del eteTask
RaiseEvent OnDeleteTask(Ta skID)
End Sub
Public Sub DisableTask(ByV al TaskID As Integer) Implements
ITaskClient.Dis ableTask
RaiseEvent OnDisableTask(T askID)
End Sub
Public Sub EditTaskItem(By Val TI As TaskInfo) Implements
ITaskClient.Edi tTaskItem
Dim TIEA As TaskInfoEventAr gs
TIEA = New TaskInfoEventAr gs(TI)
RaiseEvent OnTaskItem(tiea )
End Sub
Public Sub EnableTask(ByVa l TaskID As Integer) Implements
ITaskClient.Ena bleTask
RaiseEvent OnEnableTask(Ta skID)
End Sub
Public Function NewTask(ByVal TI As TaskInfo) As Integer Implements
ITaskClient.New Task
RaiseEvent OnNewTask(TI)
Return TI.TaskID
End Function
Public Sub RunTask(ByVal TaskID As Integer) Implements ITaskClient.Run Task
RaiseEvent OnRunTask(TaskI D)
End Sub
Public Sub StopTask(ByVal TaskID As Integer) Implements
ITaskClient.Sto pTask
RaiseEvent OnStopTask(Task ID)
End Sub
Public Function TaskItem(ByVal TaskID As Integer) As TaskInfo Implements
ITaskClient.Tas kItem
Dim TIEA As TaskInfoEventAr gs
TIEA = New TaskInfoEventAr gs(TaskID)
RaiseEvent OnTaskItem(TIEA )
Return TIEA.TaskInfo
End Function
Public Function ViewHistory(ByV al TaskID As Integer) As TaskHistories
Implements ITaskClient.Vie wHistory
Dim THs As TaskHistories
THs = New TaskHistories(T askID)
RaiseEvent OnViewHistory(T Hs)
Return THs
End Function
#End Region
End Class
Assmebly: ClientConsole.e xe
'Code truncated for brevity
Public Class ClientConsumer
Private mRemoteService As ITaskClient
Private Delegate Sub ClearHistoryDel egate(ByVal TaskID As Integer)
Private Delegate Function NewTaskDelegate (ByVal TaskInfo As TaskInfo) As
Integer
Public Sub New()
Dim ServerPort As Integer = 5555
ChannelServices .RegisterChanne l(New TcpChannel)
mRemoteService =
Activator.GetOb ject(GetType(Se riceRemoting.Re motedObject),
String.Format(" tcp://localhost:{0}/RemotedObject", ServerPort.ToSt ring))
End Sub
Public Sub ClearHistory(By Val TaskID As Integer)
Dim cb As AsyncCallback = New AsyncCallback(A ddressOf ClearHistoryBac k)
Dim d As ClearHistoryDel egate = New ClearHistoryDel egate(AddressOf
mRemoteService. ClearHistory)
Dim ar As IAsyncResult = d.BeginInvoke(T askID, cb, Nothing)
End Sub
Private Sub ClearHistoryBac k(ByVal ar As IAsyncResult)
Dim d As ClearHistoryDel egate = CType((CType(ar ,
System.runtime. Remoting.Messag ing.AsyncResult )).AsyncDelegat e,
ClearHistoryDel egate)
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(A ddressOf NewTaskBack)
Dim d As NewTaskDelegate = New NewTaskDelegate (AddressOf
mRemoteService. NewTask)
Dim ar As IAsyncResult = d.BeginInvoke(T askInfo, cb, Nothing)
'how do I return the value?
End Function
Public Sub NewTaskBack(ByV al ar As IAsyncResult)
Dim d As NewTaskDelegate = CType((CType(ar ,
System.runtime. Remoting.Messag ing.AsyncResult )).AsyncDelegat e,
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