468,161 Members | 1,952 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,161 developers. It's quick & easy.

Deserializing in a different assembly... how to?

Hi!

I have 2 different applications/assembly. The first one creates an
object and serializes it to a textfile on disk.

The second one has the the exact same class (copied/pasted). It reads
the file that is on disk and attempts to deserialize it and create a new
object.

The process works fine if I serialize/deserialize in the first
application, but it fails when I try to deserialize in the second
application. Here's the exception:

Parse Error, no assembly associated with Xml key
a1:http://schemas.microsoft.com/clr/nsa...eyToken%3Dnull
AdonisConfigDisk

Although it is generally not a good idea to copy/paste code like I did
(I should have 1 dll with the code I need), I am forced to do so for
some reasons I won't go into right now.

Here's my code... Does anyone have an idea of what I'm doing wrong? How
can I strip the assembly information from the serialized object?
public MemoryStream Serialize() {
MemoryStream stream = New MemoryStream();
SoapFormatter sf = New SoapFormatter(null, New
StreamingContext(StreamingContextStates.All));
sf.Serialize(stream, this);
stream.Seek(0, 0);
return stream;
}

public AdonisConfigDisk Deserialize(MemoryStream ms) {
SoapFormatter sf = New SoapFormatter(null, New
StreamingContext(StreamingCOntextStates.All));
return DirectCast(sf.Deserialize(ms), AdonisConfigDisk);
}
Thanks!

Carl

Nov 15 '05 #1
2 9985
This is an answer I posted a few days ago on a similar issue. Short answer:
factor the common data into an assembly that can be shared. Longer answer
below:

From the runtime's point of view, the true name of a type includes the name
of the assembly that contains it. If you're using C#, and create two classes
that have the same source-level name identity (including the namespaces),
you're going to have issues. The runtime believes that type F.O.G in
assembly A is not the same type as F.O.G in assembly B. Unfortunately, (or
fortunately depending on your POV) there's no way to express that difference
in C#. The custom of my people is to factor the common types into a common
assembly.

--
Mickey Williams
Author, "Microsoft Visual C# .NET Core Reference", MS Press
www.servergeek.com
"Carl Mercier" <in**@carl-mercier.nojunkplease.com> wrote in message
news:OH**************@TK2MSFTNGP12.phx.gbl...
Hi!

I have 2 different applications/assembly. The first one creates an
object and serializes it to a textfile on disk.

The second one has the the exact same class (copied/pasted). It reads
the file that is on disk and attempts to deserialize it and create a new
object.

The process works fine if I serialize/deserialize in the first
application, but it fails when I try to deserialize in the second
application. Here's the exception:

Parse Error, no assembly associated with Xml key
a1:http://schemas.microsoft.com/clr/nsa...%3D1.0.1419.30
011%2C%20Culture%3Dneutral%2C%20PublicKeyToken%3Dn ull AdonisConfigDisk

Although it is generally not a good idea to copy/paste code like I did
(I should have 1 dll with the code I need), I am forced to do so for
some reasons I won't go into right now.

Here's my code... Does anyone have an idea of what I'm doing wrong? How
can I strip the assembly information from the serialized object?
public MemoryStream Serialize() {
MemoryStream stream = New MemoryStream();
SoapFormatter sf = New SoapFormatter(null, New
StreamingContext(StreamingContextStates.All));
sf.Serialize(stream, this);
stream.Seek(0, 0);
return stream;
}

public AdonisConfigDisk Deserialize(MemoryStream ms) {
SoapFormatter sf = New SoapFormatter(null, New
StreamingContext(StreamingCOntextStates.All));
return DirectCast(sf.Deserialize(ms), AdonisConfigDisk);
}
Thanks!

Carl

Nov 15 '05 #2
It should be noted that one can get around this using custom
serialization. Basically, if both implement the same custom serialization
scheme, then you would be able to get around this (implement ISerializable,
and have the custom serialization constructor).

However, I agree with Mickey, the common types should be factored out
into another assembly and that assembly referenced from both assemblies
sharing in the serialization/deserialization.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Mickey Williams" <my first name at servergeek.com> wrote in message
news:ej**************@TK2MSFTNGP09.phx.gbl...
This is an answer I posted a few days ago on a similar issue. Short answer: factor the common data into an assembly that can be shared. Longer answer
below:

From the runtime's point of view, the true name of a type includes the name of the assembly that contains it. If you're using C#, and create two classes that have the same source-level name identity (including the namespaces),
you're going to have issues. The runtime believes that type F.O.G in
assembly A is not the same type as F.O.G in assembly B. Unfortunately, (or
fortunately depending on your POV) there's no way to express that difference in C#. The custom of my people is to factor the common types into a common
assembly.

--
Mickey Williams
Author, "Microsoft Visual C# .NET Core Reference", MS Press
www.servergeek.com
"Carl Mercier" <in**@carl-mercier.nojunkplease.com> wrote in message
news:OH**************@TK2MSFTNGP12.phx.gbl...
Hi!

I have 2 different applications/assembly. The first one creates an
object and serializes it to a textfile on disk.

The second one has the the exact same class (copied/pasted). It reads
the file that is on disk and attempts to deserialize it and create a new
object.

The process works fine if I serialize/deserialize in the first
application, but it fails when I try to deserialize in the second
application. Here's the exception:

Parse Error, no assembly associated with Xml key

a1:http://schemas.microsoft.com/clr/nsa...%3D1.0.1419.30 011%2C%20Culture%3Dneutral%2C%20PublicKeyToken%3Dn ull
AdonisConfigDisk

Although it is generally not a good idea to copy/paste code like I did
(I should have 1 dll with the code I need), I am forced to do so for
some reasons I won't go into right now.

Here's my code... Does anyone have an idea of what I'm doing wrong? How
can I strip the assembly information from the serialized object?
public MemoryStream Serialize() {
MemoryStream stream = New MemoryStream();
SoapFormatter sf = New SoapFormatter(null, New
StreamingContext(StreamingContextStates.All));
sf.Serialize(stream, this);
stream.Seek(0, 0);
return stream;
}

public AdonisConfigDisk Deserialize(MemoryStream ms) {
SoapFormatter sf = New SoapFormatter(null, New
StreamingContext(StreamingCOntextStates.All));
return DirectCast(sf.Deserialize(ms), AdonisConfigDisk);
}
Thanks!

Carl


Nov 15 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Carl Mercier | last post: by
reply views Thread by Zsolt Domokos | last post: by
4 posts views Thread by Brett Romero | last post: by
1 post views Thread by somequestion | last post: by
reply views Thread by Jason Madison | last post: by
1 post views Thread by gcdp | last post: by
reply views Thread by kamranasdasdas | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.