Comment fait-on pour créer un événement qui puisse être intercepté coté client.
J'ai fait des tests mais j'ai l'exection suivante :Le type System.DelegateSerializationHolder et les types qui en dérivent (tel que System.DelegateSerializationHolder) ne peuvent pas être désérialisés avec ce niveau de sécurité.
Interface
Clase sur le serveur
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 Public Interface IRemoteOperation Event Msg(ByVal Message As MessageEventArgs) Sub Start() Sub Arret() End Interface <Serializable()> Public Class MessageEventArgs Inherits MarshalByRefObject Private _Msg As String Public Sub New(ByVal as_Msg As String) _Msg = as_Msg End Sub Public ReadOnly Property Msg() As String Get Return _Msg End Get End Property End Class
Code Coté client
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 Public Class RemoteOperation Inherits MarshalByRefObject Implements RemotingInterfaces.IRemoteOperation Private nombre As Integer = 0 ' sera incrément par Incrementation() ' Indique que l'objet aura une durée de vie illimitée Public Overrides Function InitializeLifetimeService() As Object Return Nothing End Function Public Sub Envoie(ByVal as_Message As String) Dim o As Object = as_Message RaiseEvent Msg(o) End Sub Dim aTimer As New System.Timers.Timer(2000) Public Sub Start() Implements RemotingInterfaces.IRemoteOperation.Start AddHandler aTimer.Elapsed, AddressOf OnTimedEvent ' Only raise the event the first time Interval elapses. aTimer.AutoReset = False aTimer.Enabled = True End Sub Public Sub OnTimedEvent(ByVal source As Object, ByVal e As System.Timers.ElapsedEventArgs) Envoie("Hello World!") 'RaiseEvent Msg("Hello World!") End Sub Public Sub Arret() Implements RemotingInterfaces.IRemoteOperation.Arret aTimer.Enabled = False End Sub Public Event Msg(ByVal Message As RemotingInterfaces.MessageEventArgs) Implements RemotingInterfaces.IRemoteOperation.Msg End Class
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 Private Channel As System.Runtime.Remoting.Channels.tcp.TcpChannel 'New System.Runtime.Remoting.Channels.tcp.TcpChannel(0) Private serverProv As BinaryServerFormatterSinkProvider Private clientProv As BinaryClientFormatterSinkProvider Private props As IDictionary = New Hashtable Dim remoteOperation As RemotingInterfaces.IRemoteOperation 'Friend WithEvents Private Sub SetChannel() serverProv = New BinaryServerFormatterSinkProvider clientProv = New BinaryClientFormatterSinkProvider serverProv.TypeFilterLevel = System.Runtime.Serialization.Formatters.TypeFilterLevel.Full props("port") = 0 'props("name") = privServiceName Channel = New TcpChannel(props, clientProv, serverProv) End Sub Private Sub Init() SetChannel() ChannelServices.RegisterChannel(Channel) Try remoteOperation = CType(Activator.GetObject(GetType(RemotingInterfaces.IRemoteOperation), "tcp://localhost:1069/RemoteOperatio"), RemotingInterfaces.IRemoteOperation) 'CType(Activator.GetObject(GetType(RemotingInterfaces.IRemoteOperation), "tcp://localhost:8000/EasyInvoke"), RemotingInterfaces.IRemoteOperation) If remoteOperation Is Nothing Then MsgBox("Servidor no encontrado") End If ' Attach the event handler AddHandler remoteOperation.Msg, AddressOf remoteOperation_Msg Catch ex As Exception MsgBox(ex.Message) End Try End Sub Private Sub remoteOperation_Msg(ByVal Message As RemotingInterfaces.MessageEventArgs) MessageBox.Show(Message.Msg) End Sub
Partager