title: WCF cannot Serialization for Object (System.Byte[*] Type)
body: <p>All</p>
  
  <p>Basically my WCF Service needs to connect to a DCOM Server and obtain a struct containing a "VARIANT" field and then pass it to my WCF Service client. </p>
  
  <p>This is my WCF Data Contract for this Struct obtained from native DCOM Service </p>
  
  <pre><code>[DataContract]
  [ComVisible(true)]
  [StructLayout(LayoutKind.Sequential)]
  public struct tagProcReadAns
  {
      [DataMember]
      [MarshalAs(UnmanagedType.Struct)]
      public object vItemValue;           //VARIANT: marshalled as object in C# 
      [DataMember]
      public ushort wQuality;
      [DataMember]
      public Int32 Error;}            
  </code></pre>
  
  <p>since i expected this data contract can be registered as COM object and marshal back to C++ code again, that's why i have the [ComVisible] and [MarshalAs] part</p>
  
  <p>Anyway, the vItemValue is the VARIANT object i obtained successfully from DCOM Server, (i have verified the content). However, when i encountered exceptions when i tried to pass it over the WCF.</p>
  
  <p>I expected that the data contained in vItemValue VARIANT is of "byte array type" since i saw that the VARIANT.vt value is "VTUI1|VT_ARRAY", so i modify the DataContract as following</p>
  
  <pre><code>[DataContract]
  [KnownType(TypeOf(byte[])]  //new 
  [ComVisible(true)]
  [StructLayout(LayoutKind.Sequential)]
  public struct tagProcReadAns
  {
      [DataMember]
      [MarshalAs(UnmanagedType.Struct)]
      public object vItemValue;           //VARIANT: marshalled as object in C# 
      [DataMember]
      public ushort wQuality;
      [DataMember]
      public Int32 Error;} 
  </code></pre>
  
  <p>However, when i run the code again, i still ecountered exception, by adding the tracing, i have the following error messages </p>
  
  <pre><code>There was an error while trying to serialize parameter http://tempuri.org/:aryAns. The InnerException message was 'Type 'System.Byte[*]' with data contract name 'ArrayOfunsignedByte:http://schemas.microsoft.com/2003/10/Serialization/Arrays' is not expected. Consider using a DataContractResolver or add any types not known statically to the list of known types - for example, by using the KnownTypeAttribute attribute or by adding them to the list of known types passed to DataContractSerializer.'.  Please see InnerException for more details.
  </code></pre>
  
  <p>It seems that the data type is not byte[] but byte[*] ?? Is there anything wrong i have done?
  and as the exception has suggested, i can use DataContractResolver to somehow solve this problem, can you suggest any solutions?</p>
  
  <p>thank you</p>