Hoe kan ik mvb Remoting een complex object retourneren.
Ik zie alleen maar eenvoudige voorbeelden met strings en int's maar ik wil een eigen object van een server terug krijgen. In dit geval probeer ik een collection object (complex type) te ontvangen. Maar het zou natuurlijk ook met een eigen class-object moeten kunnen.
Voorbeeld server:
De Class
Het Form
VOORBEELD CLIENT
Dezelfde class als van Server
Het Form
Ik zit al de hele dag te zoek naar goed voorbeelden van remoting in VB.NET maar ik kom overal hetzelfde simpele voorbeeld tegen. Deze heb ik geprobeerd aan te passen.
Kan iemand me vertellen waar ik op moet letten of een aantal termen geven waarop ik verder kan zoeken.
Ik zie alleen maar eenvoudige voorbeelden met strings en int's maar ik wil een eigen object van een server terug krijgen. In dit geval probeer ik een collection object (complex type) te ontvangen. Maar het zou natuurlijk ook met een eigen class-object moeten kunnen.
Voorbeeld server:
De Class
code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
| Public Class SimpleClass : Inherits MarshalByRefObject
Public Function GetDateTime() As Date
Return Now()
End Function
Public Function Hello() As Collection
Dim lvobjCollection As New Collection
lvobjCollection.Add("Test")
lvobjCollection.Add("Test2")
Return lvobjCollection
End Function
End Class |
Het Form
code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
| Imports System.Runtime.Remoting
Imports System.Runtime.Remoting.Channels
Imports System.Runtime.Remoting.Channels.Tcp
Public Class Form1
Inherits System.Windows.Forms.Form
#Region " Windows Form Designer generated code "
Public Sub New()
MyBase.New()
'This call is required by the Windows Form Designer.
InitializeComponent()
'Add any initialization after the InitializeComponent() call
End Sub
'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
Friend WithEvents Label1 As System.Windows.Forms.Label
'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer
'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.Label1 = New System.Windows.Forms.Label
Me.SuspendLayout()
'
'Label1
'
Me.Label1.Location = New System.Drawing.Point(8, 8)
Me.Label1.Name = "Label1"
Me.Label1.Size = New System.Drawing.Size(360, 40)
Me.Label1.TabIndex = 1
Me.Label1.Text = "Label1"
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(392, 54)
Me.Controls.Add(Me.Label1)
Me.Name = "Form1"
Me.Text = "Simple Remoting Server"
Me.ResumeLayout(False)
End Sub
#End Region
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
Try
Dim channel As New TcpChannel(9999)
ChannelServices.RegisterChannel(channel)
RemotingConfiguration.ApplicationName = "SimpleRemotingServer"
Dim t As Type = Type.GetType("Server.SimpleClass")
RemotingConfiguration.RegisterWellKnownServiceType(t, "SimpleClass", WellKnownObjectMode.SingleCall)
Label1.Text = "SimpleClass registered." & vbCrLf & "Remoting running..."
Catch ex As Exception
Label1.Text = "Remoting initilization failed."
End Try
End Sub
End Class |
VOORBEELD CLIENT
Dezelfde class als van Server
Het Form
code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
| Imports System.Runtime.Remoting
Imports System.Runtime.Remoting.Channels
Imports System.Runtime.Remoting.Channels.Tcp
Public Class Form1
Inherits System.Windows.Forms.Form
#Region " Windows Form Designer generated code "
Public Sub New()
MyBase.New()
'This call is required by the Windows Form Designer.
InitializeComponent()
'Add any initialization after the InitializeComponent() call
End Sub
'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
Friend WithEvents DateTimeButton As System.Windows.Forms.Button
Friend WithEvents HelloButton As System.Windows.Forms.Button
'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer
'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents Button1 As System.Windows.Forms.Button
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.DateTimeButton = New System.Windows.Forms.Button
Me.HelloButton = New System.Windows.Forms.Button
Me.Button1 = New System.Windows.Forms.Button
Me.SuspendLayout()
'
'DateTimeButton
'
Me.DateTimeButton.Location = New System.Drawing.Point(60, 17)
Me.DateTimeButton.Name = "DateTimeButton"
Me.DateTimeButton.Size = New System.Drawing.Size(85, 25)
Me.DateTimeButton.TabIndex = 1
Me.DateTimeButton.Text = "GetDateTime"
'
'HelloButton
'
Me.HelloButton.Location = New System.Drawing.Point(171, 17)
Me.HelloButton.Name = "HelloButton"
Me.HelloButton.Size = New System.Drawing.Size(85, 25)
Me.HelloButton.TabIndex = 2
Me.HelloButton.Text = "Hello"
'
'Button1
'
Me.Button1.Location = New System.Drawing.Point(280, 24)
Me.Button1.Name = "Button1"
Me.Button1.Size = New System.Drawing.Size(24, 24)
Me.Button1.TabIndex = 3
Me.Button1.Text = "Button1"
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(324, 57)
Me.Controls.Add(Me.Button1)
Me.Controls.Add(Me.HelloButton)
Me.Controls.Add(Me.DateTimeButton)
Me.Name = "Form1"
Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen
Me.Text = "Remoting Client"
Me.ResumeLayout(False)
End Sub
#End Region
Dim sc As SimpleClass
Private Sub DateTimeButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DateTimeButton.Click
If Not Initialize() Then Return
MsgBox(sc.GetDateTime(), MsgBoxStyle.DefaultButton1, "GetDateTime()")
End Sub
Private Sub HelloButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles HelloButton.Click
If Not Initialize() Then Return
MsgBox(sc.Hello.Item(1), MsgBoxStyle.DefaultButton1)
'MsgBox(sc.Hello(), MsgBoxStyle.DefaultButton1, "Hello()")
End Sub
Public Function Initialize() As Boolean
If sc Is Nothing Then
Dim chan As New TcpChannel()
ChannelServices.RegisterChannel(chan)
Dim t As Type = Type.GetType("Server.SimpleClass")
sc = Activator.GetObject(t, "tcp://localhost:9999/SimpleClass")
If sc Is Nothing Then
MsgBox("Could not initialize the remoting client. Check your configuration.")
Return False
End If
End If
Return True
End Function
End Class |
Ik zit al de hele dag te zoek naar goed voorbeelden van remoting in VB.NET maar ik kom overal hetzelfde simpele voorbeeld tegen. Deze heb ik geprobeerd aan te passen.
Kan iemand me vertellen waar ik op moet letten of een aantal termen geven waarop ik verder kan zoeken.