none
WPF Composite - binary serialization issues RRS feed

  • Question

  • Hi,
    I keep getting strange exception during deserialization in one of WPF Composite modules.

    I've created a sample solution showing the problem

    http://www.drivehq.com/file/df.aspx/publish/serializationproblem/PublicFolder/SampleToolSolution.zip

    it is based on quick startups.

    there is a single module that trys to save and then load collection (when you push "Go" button).
    In Load method (please reffer to code i've pasted below) an exception occures.

    Unable to find assembly 'Tool.Model, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'.""

    As alternative i created Console application which has reference to my model. It does exactly the same as
    WPF composite App but there is no exception and it works correctly.

    It looks to me that references are ok.

    I would appreciate any help. :)

    //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    ModuleAView.xaml.cs
    ...
            private string FullFileName = "coll.dat";
            private void btnGo_Click(object sender, RoutedEventArgs e)
            {
                SampleObjectCollection coll = new SampleObjectCollection();
                coll.Add(new SampleObject {Name = "name"});
                Save(coll);
                SampleObjectCollection coll2 = Load();
            }

            public void Save(SampleObjectCollection collection)
            {
                FileStream fileStream = new FileStream(FullFileName, FileMode.Create);
                BinaryFormatter formatter = new BinaryFormatter();
                formatter.Serialize(fileStream, collection);
                fileStream.Close();
            }

            public SampleObjectCollection Load()
            {
                SampleObjectCollection coll;
                if (!File.Exists(FullFileName))
                {
                    coll = new SampleObjectCollection();
                    return coll;
                }
                FileStream fileStream = new FileStream(FullFileName, FileMode.Open);
                BinaryFormatter formatter = new BinaryFormatter();
                coll = (SampleObjectCollection)formatter.Deserialize(fileStream); //this throws exception
                fileStream.Close();
                return coll;
            }
    ...


    SampleObject.cs
    ...
        [Serializable]
        public class SampleObject
        {
            public string Name { get; set; }
            public int otherThing { get; internal set; }
        }
    ...

    SampleObjectCollection.cs

    ...
        [Serializable]
        public class SampleObjectCollection:List<SampleObject>
        {
        }
    ...
    Saturday, September 20, 2008 5:13 PM

Answers

  • I can add that assembly serializer is looking for is loaded into the current app domain, and following code solves the problem:

          AppDomain.CurrentDomain.AssemblyResolve += 
            delegate(object sender, ResolveEventArgs args) 
            { 
              foreach (var asm in AppDomain.CurrentDomain.GetAssemblies()) 
                if (asm.FullName == args.Name) 
                  return asm; 
              return null
            }; 
     

    Although it's highly unlikely that problem is connected with WPF.

    Developer

    P.S. Still I don't understand neither why default resolving algorithm cannot find assembly.
    Sunday, September 21, 2008 4:08 PM

All replies

  • I can add that assembly serializer is looking for is loaded into the current app domain, and following code solves the problem:

          AppDomain.CurrentDomain.AssemblyResolve += 
            delegate(object sender, ResolveEventArgs args) 
            { 
              foreach (var asm in AppDomain.CurrentDomain.GetAssemblies()) 
                if (asm.FullName == args.Name) 
                  return asm; 
              return null
            }; 
     

    Although it's highly unlikely that problem is connected with WPF.

    Developer

    P.S. Still I don't understand neither why default resolving algorithm cannot find assembly.
    Sunday, September 21, 2008 4:08 PM
  • Hi!
    Thanks for help. This code works just fine and solves the problem :)

    Regards,
    PK
    Saturday, September 27, 2008 12:33 AM