Bonjour,
j'ai un serveur qui distribue un objet SharedObject qui a comme attribut un objet sérializable SerializableObject.
Le client récupère cet objet distribué et appele une méthode sur celui-ci afin de récupérer l'attribut.
Et là, erreur de cast sur la valeur de retour :
Je ne comprends pas pourquoi le client ne parvient pas à récupérer l'objet sérializé...mono Client.exe
Deserialization...
Unhandled Exception: System.InvalidCastException: Return value has an invalid type
at System.Runtime.Remoting.Proxies.RealProxy.ProcessResponse (IMethodReturnMessage mrm, System.Runtime.Remoting.Messaging.MonoMethodMessage call) [0x00000] in <filename unknown>:0
at System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke (System.Runtime.Remoting.Proxies.RealProxy rp, IMessage msg, System.Exception& exc, System.Object[]& out_args) [0x00000] in <filename unknown>:0
make: *** [run-client] Error 1
Une idée?
Merci.
Le code :
Server.cs
Client.cs
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 using System; // for the Console class using System.Runtime.Remoting; // for the RemoteConfiguration class using System.Runtime.Remoting.Channels; // for the ChannelServices class using System.Runtime.Remoting.Channels.Tcp; // for the TcpChannel class namespace Sample { public class Server { public static void Main() { TcpChannel tcpChannel = new TcpChannel(8089); ChannelServices.RegisterChannel(tcpChannel, false); RemotingConfiguration.RegisterWellKnownServiceType(typeof(SharedObject), "SharedObject", WellKnownObjectMode.Singleton); Console.WriteLine("Push <Enter> to exit..."); Console.ReadLine(); } } }
SharedObject.cs
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 using System; // for the Console and Activator classes using System.Runtime.Remoting.Channels; // for the ChannelServices class using System.Runtime.Remoting.Channels.Tcp; // for the TcpChannel class namespace Sample { public class Client { public static void Main() { TcpChannel tcpChannel = new TcpChannel(); ChannelServices.RegisterChannel(tcpChannel, false); SharedObject sharedObject = (SharedObject)Activator.GetObject(typeof(SharedObject), "tcp://localhost:8089/SharedObject"); sharedObject.printHello(); sharedObject.getSerializableObject(); Console.WriteLine("Push <Enter> to exit..."); Console.ReadLine(); } } }
SerializableObject
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 using System; // for the Console and MarshalByRefObject classes namespace Sample { public class SharedObject: MarshalByRefObject { private SerializableObject serializableObject; public SharedObject() { this.serializableObject = new SerializableObject(); } public void printHello() { Console.WriteLine("Hello"); } public SerializableObject getSerializableObject() { return this.serializableObject; } } }
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 using System; // for the Console and MarshalByRefObject classes using System.Runtime.Serialization; // for the SerializationInfo and StreamingContext using System.Security.Permissions; // for the SecurityPermissionAttribute namespace Sample { [Serializable] public class SerializableObject: ISerializable { public SerializableObject() { } public void printWorld() { Console.WriteLine("World"); } // Déserialization public SerializableObject(SerializationInfo info, StreamingContext context) { Console.WriteLine("Deserialization..."); } // Sérialization [SecurityPermissionAttribute( SecurityAction.Demand, SerializationFormatter = true)] public void GetObjectData(SerializationInfo info, StreamingContext context) { Console.WriteLine("Serialization..."); } } }
Partager