binary formatter in uwp

Error below:

An exception of type 'System.Runtime.Serialization.SerializationException' occurred in System.Private.DataContractSerialization.dll but was not handled in user code
Additional information: Element 'http://schemas.microsoft.com/2003/10/Serialization/Arrays:Value' contains data of the 'http://schemas.datacontract.org/2004/07/UWPDemo:Message' data contract. The deserializer has no knowledge of any type that maps to this contract. Add the type corresponding to 'Message' to the list of known types - for example, by using the KnownTypeAttribute attribute or by adding it to the list of known types passed to DataContractSerializer.

 

Code here:

http://www.manoli.net/csharpformat/format.aspx

namespace QuickDemo
{
    public interface IMessage
    {
        int Id { get; set; }
    }

    public class Message : IMessage
    {
        public int Id
        {
            get;
            set;
        }

        public string Name { get; set; }
    }

    public class SerializationSample
    {
        public Dictionary<int, IMessage> Data;

        public void TestRun()
        {
            byte[] content;

            // Populate data
            Data = new Dictionary<int, IMessage>();
            Data.Add(1, new Message() { Id = 100, Name = "Name 100" });

            // Define known types
            List<Type> ktypes = new List<Type>();
            ktypes.Add(typeof(Message));

            // Serialize content
            DataContractSerializer ds = new DataContractSerializer(typeof(Dictionary<int, IMessage>), ktypes);
            using (var stream = new MemoryStream())
            {
                using (var writer = XmlDictionaryWriter.CreateBinaryWriter(stream))
                {
                    ds.WriteObject(writer, Data);
                }

                content = stream.ToArray(); // Convert to bytes
            }

            // Deserialize content
            // Convert bytes back to respective type
            using (var reader = XmlDictionaryReader.CreateBinaryReader(content, XmlDictionaryReaderQuotas.Max))
            {
                var x = (Dictionary<int, IMessage>)ds.ReadObject(reader);  // Do relevant casting here.
            }
        }
    }
}

 

Fix: Start menu, Cortana, Notification Area not launching in windows 10

From Day 1 after upgrading to Windows 10, exactly 1 year before, there was an annoying bug which stabs me randomly when I try to use my Windows 10 PC.

Below were various symptoms:

  1. Start menu not launching
  2. Cortana UI not launching
  3. Notification area not launching
  4. Start menu and Notification area launches, but Cortana UI not launching

In symptom #4, if you try to perform a search after launching Start menu, nothing will happen since Cortana UI can’t be launched.

Searching through Microsoft Answers forums showed that this bug is dated back before the RTM release of Windows 10 which means they exists during Preview build days but neither of the solution worked perfectly. It’s still an open bug as far as I am concerned. Most of the solution suggested are:

  1. Run a lengthy PowerShell script
  2. Create new local account and copy settings
  3. Series of registry edits
  4. Scan for file system errors
  5. Restore your PC to previous state
  6. Fresh install Windows 10

All the above solution mentioned, involves manual edits, running scripts, changing states, etc. which is a No-No. Because you are spending your times to fix a problem which again takes time to bring your machine back to it’s current state. That’s not an ideal solution at all since this issue can comes back again anytime and you are gonna spend more time on it.

Luckily, after many frustrated days and trial-and-error, I somehow devised solution to fix the above issue.

This is one of the event message that’s logged in Event Viewer;

immersive-shell-evt

“Activation of app Microsoft.Windows.Cortana_cw5n1h2txyewy!CortanaUI failed with error: The app didn't start in the required time. See the Microsoft-Windows-TWinUI/Operational log for additional information.”

One main reason for this was, Windows Updates was downloading in background. If that’s the case, let the download complete and reboot your system and see whether it’s fixed. If that solved you problem, cool.

 

If that didn’t helped, keep reading.

  1. Open This PC (Ctrl+E), select Settings.

    this-pc-settings
  2. Select Upgrade and Recovery.

    settings-upgrade-recovery
  3. Select Recovery –> Advanced Settings –> Restart now.

    settings-recovery-advance-settings
  4. After reboot, you’ll be taken to Choose an option screen, select Troubleshoot.

    WP_20160731_001
  5. Select Advanced options.

    WP_20160731_003
  6. Select System Restore.

    WP_20160731_004
  7. System will prepare for restoring. Enter you account password when prompted.

    WP_20160731_005

    WP_20160731_006
  8. You’ll get System Restore prompt, choose Next.

    WP_20160731_007
  9. Once your restore points are listed, stop right there. Now, CLOSE the System Restore prompt.

    WP_20160731_008
  10. Turn OFF your PC.

Now, turn ON your PC, wait for system tray items to load.

Check you Start menu, Cortana and Notification area. Everything works fine and your system is back to normal.

Hope this helps.
Thanks.

Error: Document element did not appear. Line 1, position 1 in Unity3d

I was trying to serialize and deserialize a class instance that saves some scene details in Unity3d. Though the instance was serializes successfully deserialization was failing with error – “Error: Document element did not appear.  Line 1, position 1.” every time. This was driving me nuts because the same serialization/deserialization code works fine in a console c# application.

The issue was related to BOM that’s included at the very beginning of the text file to know it’s encoding. This struck me because whenever I try to open c# script file from Unity, my Visual Studio always prompted to correct the encoding and line spacing of the file.

The error is annoying, but fix is relatively very simple. Do specify UTF8 encoding when you deserialize the file which will do the trick.

using (StreamReader reader = new StreamReader("filename", new UTF8Encoding(false)))

Hope this helps.