views:

212

answers:

3

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 application 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?

A: 

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.

HackedByChinese
+1  A: 

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.

Sam
Thanks, your answer was helpful.
aip.cd.aish
+1  A: 

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.

Jon Skeet
Hey, I have never done this before. Does this mean going into the Project properties of the two projects and changing it so they have the same "Assembly name" but different namespaces? And Creating a third Project (a Class Library project) that contains just the shared classes and setting its assembly name to the same thing?
aip.cd.aish
(to aip.cd.aish) "try to avoid using mutable structs" - oh, and avoid public fields too (`out t.X` tells me it is a public field)
Marc Gravell
Never mind. Got it. I created a new Class Library Project and took the output dll and added it to both projects.
aip.cd.aish