Page Index Toggle Pages: [1] 2  Send TopicPrint
Hot Topic (More than 10 Replies) Exception in LoadFromStream when strong named key file changed (Read 10531 times)
David Long
Full Member
***
Offline


I love YaBB 1G - SP1!

Posts: 125
Location: England
Joined: Oct 23rd, 2006
Exception in LoadFromStream when strong named key file changed
Feb 6th, 2017 at 3:36pm
Print Post  
If I change my strong named key file (perhaps to one provided by an authority that provides Code Signing Certificates) then this causes an exception when calling Diagram.LoadFromStream(stream) on older files that were Serialized with code signed with an older key file.

Code…
try
{
using (Stream stream = new FileStream(pathname, FileMode.Open, FileAccess.Read, FileShare.Read))
{
flowChart.LoadFromStream(stream);

Exception: "The located assembly's manifest does not match the assembly reference (Exception from HRESULT: 0x80131040)"

Is there any way around this?
DavidL

  
Back to top
 
IP Logged
 
Slavcho
YaBB Moderator
*****
Offline


tech.support

Posts: 3147
Joined: Oct 19th, 2005
Re: Exception in LoadFromStream when strong named key file changed
Reply #1 - Feb 6th, 2017 at 5:20pm
Print Post  
Try setting Diagram.SerializationBinder to an instance that maps types from your old assembly to new assembly. We added that to handle renamed types (http://mindfusion.eu/Forum/YaBB.pl?num=1254163218) but it should work for changed strong name too.

Regards,
Slavcho
Mindfusion
  
Back to top
 
IP Logged
 
David Long
Full Member
***
Offline


I love YaBB 1G - SP1!

Posts: 125
Location: England
Joined: Oct 23rd, 2006
Re: Exception in LoadFromStream when strong named key file changed
Reply #2 - Feb 7th, 2017 at 11:13am
Print Post  
Thanks, I had not noticed that option. However, using it does not seem to solve the problem - I'm not sure why. Running in the debugger my BindToType function is called several times successfully but then the software fails with the same error after a call to my AssemblyResolver...

private static Assembly AssembleyResolveEventHandler(object sender, ResolveEventArgs args)
{
var name = new AssemblyName(args.Name);

if (name.Name == "MyProgramName")
{
// Whatever version is required, just use the current version
return typeof(MyForm).Assembly;
}
return null;
}

I'm not sure how to proceed from here. Perhaps XML serialization would be easier! Smiley
  
Back to top
 
IP Logged
 
Slavcho
YaBB Moderator
*****
Offline


tech.support

Posts: 3147
Joined: Oct 19th, 2005
Re: Exception in LoadFromStream when strong named key file changed
Reply #3 - Feb 7th, 2017 at 1:56pm
Print Post  
I'm not sure what the code above does? You must derive from System.Runtime.Serialization.SerializationBinder, override its BindToType method and assign it to the diagram's property.
  
Back to top
 
IP Logged
 
Slavcho
YaBB Moderator
*****
Offline


tech.support

Posts: 3147
Joined: Oct 19th, 2005
Re: Exception in LoadFromStream when strong named key file changed
Reply #4 - Feb 8th, 2017 at 9:45am
Print Post  
E.g. try this -
Code
Select All
class StrongNameChangedBinder : System.Runtime.Serialization.SerializationBinder
{
    public override Type BindToType(string assemblyName, string typeName)
    {
        if (assemblyName.StartsWith(Assembly.GetExecutingAssembly().GetName().Name))
            return Assembly.GetExecutingAssembly().GetType(typeName);
        return null;
    }
} 



TestApp and "TestApp - Copy" folders here contain same project strong-named with different keys, each should be able to load tag objects saved from the other one -
https://mindfusion.eu/_samples/BinderTest.zip

Regards,
Slavcho
Mindfusion
  
Back to top
 
IP Logged
 
David Long
Full Member
***
Offline


I love YaBB 1G - SP1!

Posts: 125
Location: England
Joined: Oct 23rd, 2006
Re: Exception in LoadFromStream when strong named key file changed
Reply #5 - Feb 8th, 2017 at 8:57pm
Print Post  
I have something very similar which works for everything except a changed strong named key file...

sealed class HtaDeserializationBinder : SerializationBinder
{
public override Type BindToType(string assemblyName, string typeName)
{
// For each assemblyName/typeName that you want to deserialize to
// a different type, set typeName to the new type.

if (typeName == "HierarchicalTaskAnalysis.HTAForm+DataGrids")
{
typeName = "HierarchicalTaskAnalysis.DataGridForm+DataGrids";
}
else
if (typeName == "HierarchicalTaskAnalysis.HTAForm+ColourCoding")
{
typeName = "HierarchicalTaskAnalysis.HfwForm+ColourCoding";
}

// The following lines of code returns the type.
Type t = Type.GetType(string.Format("{0}, {1}", typeName, Assembly.GetExecutingAssembly().FullName));

return t;
}
}

The only difference I can see is that I use LoadFromStream() rather than LoadFromFile() that is in your example...

try
{
using (Stream stream = new FileStream(pathname, FileMode.Open, FileAccess.Read, FileShare.Read))
{
flowChart.SerializationBinder = new HtaDeserializationBinder();
flowChart.LoadFromStream(stream);
.
.

  
Back to top
 
IP Logged
 
Slavcho
YaBB Moderator
*****
Offline


tech.support

Posts: 3147
Joined: Oct 19th, 2005
Re: Exception in LoadFromStream when strong named key file changed
Reply #6 - Feb 9th, 2017 at 9:16am
Print Post  
Are all of your Tag types defined inside the .exe assembly? I think code above won't work if you use types from a DLL. Maybe call Debug.Write for type names and returned Type values and check the VS console to see if the binder finds all types successfully. If it returns null, the control will try to load the original type and still get you same exception.
  
Back to top
 
IP Logged
 
David Long
Full Member
***
Offline


I love YaBB 1G - SP1!

Posts: 125
Location: England
Joined: Oct 23rd, 2006
Re: Exception in LoadFromStream when strong named key file changed
Reply #7 - Feb 9th, 2017 at 9:57am
Print Post  
I can confirm that all types are in the single executable with no other assemblies being involved. With a Debug.WriteLine() in BindToType() I get...

BindToType: HierarchicalTaskAnalysis, Version=4.21.0.0, Culture=neutral, PublicKeyToken=d2910bf39fa9f149, HierarchicalTaskAnalysis.BoxDataL
BindToType: HierarchicalTaskAnalysis, Version=4.21.0.0, Culture=neutral, PublicKeyToken=d2910bf39fa9f149, HierarchicalTaskAnalysis.HfwForm+BoxType2
BindToType: HierarchicalTaskAnalysis, Version=4.21.0.0, Culture=neutral, PublicKeyToken=d2910bf39fa9f149, HierarchicalTaskAnalysis.HfwForm+TypeOfAnalysis
BindToType: System.Drawing, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, System.Drawing.Color
BindToType: HierarchicalTaskAnalysis, Version=4.21.0.0, Culture=neutral, PublicKeyToken=d2910bf39fa9f149, HierarchicalTaskAnalysis.HfwForm+TaskStateType
BindToType: mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089, System.Collections.Generic.List`1[[System.String[], mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]
BindToType: mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089, System.Collections.Generic.Dictionary`2[[System.String, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[HierarchicalTaskAnalysis.PIFAssessmentData, HierarchicalTaskAnalysis, Version=4.21.0.0, Culture=neutral, PublicKeyToken=d2910bf39fa9f149]]
Exception thrown: 'System.IO.FileLoadException' in MindFusion.Diagramming.dll

The only other thing I can think of is that your example targets .NET 4.5 where as my program targets .NET 3.5
  
Back to top
 
IP Logged
 
Slavcho
YaBB Moderator
*****
Offline


tech.support

Posts: 3147
Joined: Oct 19th, 2005
Re: Exception in LoadFromStream when strong named key file changed
Reply #8 - Feb 9th, 2017 at 1:43pm
Print Post  
We might be making some progress if you now get FileLoadException instead of the assembly manifest one. What's the FileLoadException's message text?
  
Back to top
 
IP Logged
 
David Long
Full Member
***
Offline


I love YaBB 1G - SP1!

Posts: 125
Location: England
Joined: Oct 23rd, 2006
Re: Exception in LoadFromStream when strong named key file changed
Reply #9 - Feb 9th, 2017 at 3:54pm
Print Post  
As noted in original post:

"The located assembly's manifest does not match the assembly reference (Exception from HRESULT: 0x80131040)"

  
Back to top
 
IP Logged
 
Slavcho
YaBB Moderator
*****
Offline


tech.support

Posts: 3147
Joined: Oct 19th, 2005
Re: Exception in LoadFromStream when strong named key file changed
Reply #10 - Feb 9th, 2017 at 4:49pm
Print Post  
If the types you are loading are user controls for ControlNode contents, they might be keeping referenced assembly strong names for their resources - check PublicKeyToken values in the project's resx files and update them to new token if necessary. Maybe run a Find All in Files command over *.* files to find any mentions of the old token value.
  
Back to top
 
IP Logged
 
David Long
Full Member
***
Offline


I love YaBB 1G - SP1!

Posts: 125
Location: England
Joined: Oct 23rd, 2006
Re: Exception in LoadFromStream when strong named key file changed
Reply #11 - Feb 10th, 2017 at 10:41am
Print Post  
I'm not using any control nodes in my diagram. If it helps here is the call stack...

Message : The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)
Source : mscorlib
Stack Trace :
at System.Reflection.Assembly._GetType(String name, Boolean throwOnError, Boolean ignoreCase)
at System.Reflection.Assembly.GetType(String name, Boolean throwOnError)
at A.cbfaa775cc8fddff39ae50973e4a9ca26.BindToType(String assemblyName, String typeName)
at System.Runtime.Serialization.Formatters.Binary.ObjectReader.Bind(String assemblyString, String typeString)
at System.Runtime.Serialization.Formatters.Binary.ObjectReader.GetType(BinaryAssemb
lyInfo assemblyInfo, String name)
at System.Runtime.Serialization.Formatters.Binary.ObjectMap..ctor(String objectName, String[] memberNames, BinaryTypeEnum[] binaryTypeEnumA, Object[] typeInformationA, Int32[] memberAssemIds, ObjectReader objectReader, Int32 objectId, BinaryAssemblyInfo assemblyInfo, SizedArray assemIdToAssemblyTable)
at System.Runtime.Serialization.Formatters.Binary.ObjectMap.Create(String name, String[] memberNames, BinaryTypeEnum[] binaryTypeEnumA, Object[] typeInformationA, Int32[] memberAssemIds, ObjectReader objectReader, Int32 objectId, BinaryAssemblyInfo assemblyInfo, SizedArray assemIdToAssemblyTable)
at System.Runtime.Serialization.Formatters.Binary.__BinaryParser.ReadObjectWithMapT
yped(BinaryObjectWithMapTyped record)
at System.Runtime.Serialization.Formatters.Binary.__BinaryParser.ReadObjectWithMapT
yped(BinaryHeaderEnum binaryHeaderEnum)
at System.Runtime.Serialization.Formatters.Binary.__BinaryParser.Run()
at System.Runtime.Serialization.Formatters.Binary.ObjectReader.Deserialize(HeaderHa
ndler handler, __BinaryParser serParser, Boolean fCheck, Boolean isCrossAppDomain, IMethodCallMessage methodCallMessage)
at System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Deserialize(Strea
m serializationStream, HeaderHandler handler, Boolean fCheck, Boolean isCrossAppDomain, IMethodCallMessage methodCallMessage)
at MindFusion.Diagramming.PersistContext.LoadTag()
at MindFusion.Diagramming.DiagramItem.LoadFrom(BinaryReader reader, PersistContext context)
at MindFusion.Diagramming.DiagramNode.LoadFrom(BinaryReader reader, PersistContext context)
at MindFusion.Diagramming.ShapeNode.LoadFrom(BinaryReader reader, PersistContext context)
at MindFusion.Diagramming.ShapeNode.c41660de0e1d957b753ee71d9e7c49241(BinaryReader cc284babc9f08b4a49bd87e797d1c8e60, PersistContext cb4b3f6fc55d53a096d78c7553a2f21d6)
at MindFusion.Diagramming.PersistContext.TryLoadRemainingObjects()
at MindFusion.Diagramming.PersistContext.LoadRemainingObjects()
at MindFusion.Diagramming.Diagram.c66511612f3469a770425e61a057b6f6d(Stream cea15ffc7d481625ae6dde369024ecf7b, Boolean cc3ce9ce7ed78ce864d6da615836d1af9)
at MindFusion.Diagramming.Diagram.LoadFromStream(Stream stream)
  
Back to top
 
IP Logged
 
Slavcho
YaBB Moderator
*****
Offline


tech.support

Posts: 3147
Joined: Oct 19th, 2005
Re: Exception in LoadFromStream when strong named key file changed
Reply #12 - Feb 10th, 2017 at 11:03am
Print Post  
So it's thrown just after you return a Dictionary<string, HierarchicalTaskAnalysis.PIFAssessmentData> type judging from the debug.writes above? Does that PIFAssessmentData class have dependencies on types from external assembly?
  
Back to top
 
IP Logged
 
David Long
Full Member
***
Offline


I love YaBB 1G - SP1!

Posts: 125
Location: England
Joined: Oct 23rd, 2006
Re: Exception in LoadFromStream when strong named key file changed
Reply #13 - Feb 10th, 2017 at 2:15pm
Print Post  
No, it's derived from 'object' and just contains string, int, float and bool.
  
Back to top
 
IP Logged
 
Slavcho
YaBB Moderator
*****
Offline


tech.support

Posts: 3147
Joined: Oct 19th, 2005
Re: Exception in LoadFromStream when strong named key file changed
Reply #14 - Feb 10th, 2017 at 3:03pm
Print Post  
I got same exception in TestApp above after changing Tag value from MyTag to a Dictionary<string, MyTag> - maybe GetExecutingAssembly().GetType() does not work for Dictionary<> since it's not in our assembly. I'm not sure how GetType works with generic types anyway, will investigate it next week if you haven't posted a solution by then Wink
  
Back to top
 
IP Logged
 
Page Index Toggle Pages: [1] 2 
Send TopicPrint