HELP: Serializing and Deserializing

F

fjlaga

I have written an Office Add-in for Excel using VB.NET and the .NET
1.1 Framework (I have Visual Studio 2003 .NET ). All works great. I
want to add a User Settings/Prefereneces dialog and allow the user to
specify some settings and I need to persist these settings between
runs. I made a serializable class which uses the BinaryFormatter to
serialize/deserialize the setttings.

Serialization works great. However, when I try to deserialize the
saved settings on subsequent runs the following exception is thrown:

MyAddin.OnConnection Exception: Cannot find the assembly MyAddin,
Version=2.0.0.0, Culture=neutral, PublicKeyToken=null.
at
System.Runtime.Serialization.Formatters.Binary.BinaryAssemblyInfo.GetAssembly()
...

To get around this, it seems I need to register my assembly in the
GAC. So, I generated a key pair using the following command:

C:> sn -k "D:\MyAddin\SNKey.snk"

and added the following line to my AssemblyInfo.vb file:

<Assembly: AssemblyKeyFile("D:\MyAddin\SNKey.snk")>

When I try to build with this I get the error:

Unable to emit assembly: Referenced assembly
'Interop.Microsoft.Office.Core' does not have a strong name

I'm confused about how to get this to work. I just want to read and
write my user settings to disk.

Any help is greatly appreciated.
-Frank
 
G

Gino

This is a previous response to the problem you are describing.
Hope this helps.

**************************************************
If anyone is interested, I ended up solving this problem.

If you serialize in one app, and want to deserialize in another app, an
exception will be thrown if dotnet can't load the assembly that did the
serializing.

Option 1: register the serializing app in the GAC. I didn't try this,
but I assume it would work
Option 2: use the SerializationBinder class to effectively fool the
deserializing app into thinking that it did the serializing:


when setting your BinaryFormatter up to deserialize, set the Binder
property to the class below:

BinaryFormatter b = new BinaryFormatter();
b.Binder = new FooDeserializationBinder();
myClasss = (MyClass) b.Deserialize(stream);

sealed class FooDeserializationBinder : SerializationBinder
{
public override Type BindToType(string assemblyName, string
typeName)
{
return Type.GetType(typeName + ", " +
Assembly.GetExecutingAssembly().FullName);
}
}
*****************************************************
 
C

Cor Ligthert [MVP]

Gino,

Do you know that this is a VB.Net newsgroup. Although you try to be helpful,
don't think by that only to the OP. This newsgroup is more searched on
Google than only by the OP's.

Therefore please use VB.Net code.

Cor
 
F

fjlaga

Beautiful! It worked like a charm. (the code snippet provided was
easily translated into VB.NET). Thank you.
-Frank
 
G

Gino

Yes.
And the whole programming world knows that it's C# code on a VB.net
newsgroup, without you telling them.
And you don't have to whine about being politically correct (real men don't
whine).
And the vast majority of vb.net programmers can easily read a C# snipet to
get an idea for a solution (as was indicated by the response to my post,
from the original poster)
And if I see this same question again, I'm going to send this same post.
Gino, from the Socialist Republic of Cambridge
Massachusetts.
 
C

Cor Ligthert [MVP]

Gino,

Een grote aantal mensen actief in deze nieuwsgroep kan eenvoudig Nederlands
lezen en toch doen ze dat niet.

Cor
 
G

Gino

"Cor Ligthert [MVP]" wrote
Gino,

Een grote aantal mensen actief in deze nieuwsgroep kan eenvoudig
Nederlands lezen en toch doen ze dat niet.

Cor

That's true.

A large number of people actively in this newsgroup can read simple
Dutch and nevertheless does not do that.

Gino
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top