开发者

C# Reading Serialized Objects between Applications

I have two different applications and I am using GroupLab Networking to communicate between them. The idea is there is a shared dictionary and when something changes in this shared dictionary, the other ap开发者_C百科plication gets a notification. The notification part works. Here is the problem. I have the following code in the first application.

TouchInfo t = new TouchInfo();
int.TryParse(txtXCoord.Text, out t.X);
int.TryParse(txtYCoord.Text, out t.Y);
this.sharedDictionary1["/pointOne"] = t;

Where TouchInfo is a struct. This object stored in the shared dictionary can be accessed by both applications. The code looks like this:

TouchInfo val = (TouchInfo)this.sharedDictionary1["/pointOne"]

While this code works in the application that created the object. I get the following error in the second:

{Unserializable object: problem: System.Runtime.Serialization.SerializationException: Unable to find assembly 'NetworkingTestProgramOne, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'.

I realize this problem is because the serialization also stores the assembly information of the program that serialized it. But I do need it to communicate across different programs. How do I make this work?


Have you got TouchInfo declared in two different assemblies? That's a bad idea - you should have one common assembly containing the types required by both applications.

Personally I'd try to avoid the default .NET binary serialization anyway, preferring something more controllable - I'm biased towards Google Protocol Buffers for various reasons. Does GroupLab Networking require "normal" binary serialization?

As an aside, I'd also try to avoid using mutable structs if at all possible.


You need to declare the struct in an assembly that is shared by both applications. The structure should be something like this:

MyApp.Shared
MyApp.ProgramOne
MyApp.ProgramTwo

Both programs reference Shared. Then they'll be able to serialize/deserialize appropriately.


In the past i've used a SerializationBinder to translate a requested type into the equivalent type available in the current app domain.

Sadly, I don't know anything about GroupLab.Networking, so I'm not sure if you can access or override the routines that do the serialization.

An alternate approach might be to use the AppDomain.CurrentDomain.AssemblyResolve event. Assign it a delegate and use that to redirect to a valid assembly containing the type.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜