locked
XAMLObjectWriterException when deserialize custom activity RRS feed

  • Question

  • Hi

    I have custom Activity that has Dictionary as its property as shown below:

    public class MyCustomActivity: NativeActivity<IMyExecutionResult>
    {
     public Dictionary<string, object> MyDict { get; set; }
    
     public override object Clone()
     {
      return DeepCopier.DeepCopy(this);
     } 
    
    //other code are not shown for simplicity
    }
    

    Then I perform deep-copy by serialize and deserialize using this helper method:

    public static class DeepCopier<br/>{<br/> public static object DeepCopy(MyCustomActivity myCustomActivity)
     {
      StringBuilder sb = null;
      System.IO.StringWriter sw = null;
      System.Xaml.XamlWriter xw = null;
    
      System.IO.StringReader sr = null;
      System.Xaml.XamlReader xr = null;
    
      try
      {
      sb = new StringBuilder();
      sw = new System.IO.StringWriter(sb);
      xw = System.Activities.XamlIntegration.ActivityXamlServices.CreateBuilderWriter(new System.Xaml.XamlXmlWriter(sw, new System.Xaml.XamlSchemaContext()));
      System.Xaml.XamlServices.Save(xw, myCustomActivity);
    
      sr = new System.IO.StringReader(sb.ToString());
      xr = System.Activities.XamlIntegration.ActivityXamlServices.CreateBuilderReader(new System.Xaml.XamlXmlReader(sr));
      return System.Xaml.XamlServices.Load(xr);
      }
      catch (Exception e)
      {
      throw;
      }
      finally
      {
      if (xw != null)
      {
       xw.Close();
       xw = null;
      }
    
      if (sw != null)
      {
       sw.Dispose();
       sw = null;
      }
    
      if (xr != null)
      {
       xr.Close();
       xr = null;
      }
    
      if (sr != null)
      {
       sr.Dispose();
       sr = null;
      }
      }
     }<br/>}
    

    However, the last statement inside "try-catch" body throws XAMLObjectWriterException. It complains that MyDict is null.

    I initialize this custom activity using statement belows. And it shows that I have already initialized the dictionary

    public void CloneTest()
    {
      MyCustomActivity target = new MyCustomActivity();
    
      target.MyDict = new Dictionary<string, object>();
      target.MyDict.Add("key1", new InArgument<int>(1));
      target.MyDict.Add("key2", new InArgument<string>("asdf"));
    
      object expected = target;
      object actual;
      actual = target.Clone(); //will invoke DeepCopier class
      Assert.AreNotSame(expected, actual);
    }
    So, what have gone wrong in this case?

     

    Regards,


    arinto

    Wednesday, April 13, 2011 9:34 AM

Answers

  • Hi Arinto,

    You need to make sure get_MyDict does not return null. Here's sample code.

    public class MyCustomActivity: NativeActivity<IMyExecutionResult>
    {
        public MyCustomActivity()
        {
            this.MyDict = new Dictionary<string, object>();
        }

        public Dictionary<string, object> MyDict { get; private set; }
    }

    Thanks,
    Tony

    Thursday, April 14, 2011 2:31 AM

All replies

  • Hi Arinto,

    You need to make sure get_MyDict does not return null. Here's sample code.

    public class MyCustomActivity: NativeActivity<IMyExecutionResult>
    {
        public MyCustomActivity()
        {
            this.MyDict = new Dictionary<string, object>();
        }

        public Dictionary<string, object> MyDict { get; private set; }
    }

    Thanks,
    Tony

    Thursday, April 14, 2011 2:31 AM
  • The resulting string after serialization is shown below:

     

    <?xml version="1.0" encoding="utf-16"?>
    <MyCustomActivity 	xmlns="clr-namespace:MyNamespace;assembly=MyAssembly" 
    			xmlns:p="http://schemas.microsoft.com/netfx/2009/xaml/activities" 
    			xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    			<MyCustomActivity.MyDict>
    				<p:InArgument x:TypeArguments="x:Int32" x:Key="key1">1</p:InArgument>
    				<p:InArgument x:TypeArguments="x:String" x:Key="key2">asdf</p:InArgument>
    			</MyCustomActivity.MyDict>
    </MyCustomActivity>
    

     


    arinto
    Thursday, April 14, 2011 2:51 AM