I have created an add-in for VS.NET and when it starts I want the add-in to
read the options from file.
I have created a class (with the [Serializable] attribute set) that contains
the options and implemented GetObjectData(S erializationInf o info,
StreamingContex t context) as well as a copy constructor: protected
Options(Seriali zationInfo info, StreamingContex t context).
The serialization process definately works. However, on startup I try to
create an instance of my Options class and deserialize the file, but I get a
SerializationEx ception as follows:
Engine.LoadOpti ons: System.Runtime. Serialization.S erializationExc eption:
Cannot find the assembly CodeStore, Version=2.0.0.0 , Culture=neutral ,
PublicKeyToken= null.
at
System.Runtime. Serialization.F ormatters.Binar y.BinaryAssembl yInfo.GetAssemb l
y()
at
System.Runtime. Serialization.F ormatters.Binar y.ObjectReader. GetType(BinaryA s
semblyInfo assemblyInfo, String name)
at System.Runtime. Serialization.F ormatters.Binar y.ObjectMap..ct or(String
objectName, String[] memberNames, BinaryTypeEnum[] binaryTypeEnumA , Object[]
typeInformation A, Int32[] memberAssemIds, ObjectReader objectReader, Int32
objectId, BinaryAssemblyI nfo assemblyInfo, SizedArray
assemIdToAssemb lyTable)
at
System.Runtime. Serialization.F ormatters.Binar y.ObjectMap.Cre ate(String name,
String[] memberNames, BinaryTypeEnum[] binaryTypeEnumA , Object[]
typeInformation A, Int32[] memberAssemIds, ObjectReader objectReader, Int32
objectId, BinaryAssemblyI nfo assemblyInfo, SizedArray
assemIdToAssemb lyTable)
at
System.Runtime. Serialization.F ormatters.Binar y.__BinaryParse r.ReadObjectWit h
MapTyped(Binary ObjectWithMapTy ped record)
at
System.Runtime. Serialization.F ormatters.Binar y.__BinaryParse r.ReadObjectWit h
MapTyped(Binary HeaderEnum binaryHeaderEnu m)
at System.Runtime. Serialization.F ormatters.Binar y.__BinaryParse r.Run()
at
System.Runtime. Serialization.F ormatters.Binar y.ObjectReader. Deserialize(Hea d
erHandler handler, __BinaryParser serParser, Boolean fCheck,
IMethodCallMess age methodCallMessa ge)
at
System.Runtime. Serialization.F ormatters.Binar y.BinaryFormatt er.Deserialize( S
tream serializationSt ream, HeaderHandler handler, Boolean fCheck,
IMethodCallMess age methodCallMessa ge)
at
System.Runtime. Serialization.F ormatters.Binar y.BinaryFormatt er.Deserialize( S
tream serializationSt ream)
at Salamander.Addi ns.CodeStore.En gine.Engine.Loa dOptions() in
c:\development\ codestore\engin e\engine.cs:lin e 78
Why can't it find the assembly? It is not calling out to a separate library,
it's a single dll for the add-in and that's where the code is being called
from. The file it is trying to read is inthe same location as the dll, too.
The LoadOptions function looks like this:
internal void LoadOptions()
{
try
{
string path = GetAddInPath() + this.optionsFil eName;
Stream stream = new FileStream(path + ".options", FileMode.Open,
FileAccess.Read , FileShare.Read) ;
try
{
IFormatter formatter = new BinaryFormatter ();
this.options = (Options)format ter.Deserialize (stream); // <-- This is
line 78 where the exception is raised.
}
catch (System.Excepti on exc)
{
System.Diagnost ics.Trace.Write Line(exc.ToStri ng(),
"Engine.LoadOpt ions");
}
finally
{
stream.Close();
}
}
catch (System.Excepti on exc)
{
if (exc is FileNotFoundExc eption)
{
// No options file, so write the current default options to file.
SaveOptions();
}
else
{
System.Diagnost ics.Trace.Write Line(exc.Messag e, "Engine.LoadOpt ions");
}
}
}
read the options from file.
I have created a class (with the [Serializable] attribute set) that contains
the options and implemented GetObjectData(S erializationInf o info,
StreamingContex t context) as well as a copy constructor: protected
Options(Seriali zationInfo info, StreamingContex t context).
The serialization process definately works. However, on startup I try to
create an instance of my Options class and deserialize the file, but I get a
SerializationEx ception as follows:
Engine.LoadOpti ons: System.Runtime. Serialization.S erializationExc eption:
Cannot find the assembly CodeStore, Version=2.0.0.0 , Culture=neutral ,
PublicKeyToken= null.
at
System.Runtime. Serialization.F ormatters.Binar y.BinaryAssembl yInfo.GetAssemb l
y()
at
System.Runtime. Serialization.F ormatters.Binar y.ObjectReader. GetType(BinaryA s
semblyInfo assemblyInfo, String name)
at System.Runtime. Serialization.F ormatters.Binar y.ObjectMap..ct or(String
objectName, String[] memberNames, BinaryTypeEnum[] binaryTypeEnumA , Object[]
typeInformation A, Int32[] memberAssemIds, ObjectReader objectReader, Int32
objectId, BinaryAssemblyI nfo assemblyInfo, SizedArray
assemIdToAssemb lyTable)
at
System.Runtime. Serialization.F ormatters.Binar y.ObjectMap.Cre ate(String name,
String[] memberNames, BinaryTypeEnum[] binaryTypeEnumA , Object[]
typeInformation A, Int32[] memberAssemIds, ObjectReader objectReader, Int32
objectId, BinaryAssemblyI nfo assemblyInfo, SizedArray
assemIdToAssemb lyTable)
at
System.Runtime. Serialization.F ormatters.Binar y.__BinaryParse r.ReadObjectWit h
MapTyped(Binary ObjectWithMapTy ped record)
at
System.Runtime. Serialization.F ormatters.Binar y.__BinaryParse r.ReadObjectWit h
MapTyped(Binary HeaderEnum binaryHeaderEnu m)
at System.Runtime. Serialization.F ormatters.Binar y.__BinaryParse r.Run()
at
System.Runtime. Serialization.F ormatters.Binar y.ObjectReader. Deserialize(Hea d
erHandler handler, __BinaryParser serParser, Boolean fCheck,
IMethodCallMess age methodCallMessa ge)
at
System.Runtime. Serialization.F ormatters.Binar y.BinaryFormatt er.Deserialize( S
tream serializationSt ream, HeaderHandler handler, Boolean fCheck,
IMethodCallMess age methodCallMessa ge)
at
System.Runtime. Serialization.F ormatters.Binar y.BinaryFormatt er.Deserialize( S
tream serializationSt ream)
at Salamander.Addi ns.CodeStore.En gine.Engine.Loa dOptions() in
c:\development\ codestore\engin e\engine.cs:lin e 78
Why can't it find the assembly? It is not calling out to a separate library,
it's a single dll for the add-in and that's where the code is being called
from. The file it is trying to read is inthe same location as the dll, too.
The LoadOptions function looks like this:
internal void LoadOptions()
{
try
{
string path = GetAddInPath() + this.optionsFil eName;
Stream stream = new FileStream(path + ".options", FileMode.Open,
FileAccess.Read , FileShare.Read) ;
try
{
IFormatter formatter = new BinaryFormatter ();
this.options = (Options)format ter.Deserialize (stream); // <-- This is
line 78 where the exception is raised.
}
catch (System.Excepti on exc)
{
System.Diagnost ics.Trace.Write Line(exc.ToStri ng(),
"Engine.LoadOpt ions");
}
finally
{
stream.Close();
}
}
catch (System.Excepti on exc)
{
if (exc is FileNotFoundExc eption)
{
// No options file, so write the current default options to file.
SaveOptions();
}
else
{
System.Diagnost ics.Trace.Write Line(exc.Messag e, "Engine.LoadOpt ions");
}
}
}
Comment