title: Sending a class object with Stream property using Rest
body: <p>I can send a simple <code>class object</code> with some properties like this:  </p>
  
  <pre><code>[DataContract]
  public class DeviceInfo
  {
      [DataMember]
      public string DeviceCode { get; set; }
  
      [DataMember]
      public string ClientKey { get; set; }
  
      [DataMember]
      [XmlIgnore]
      public DateTime Timestamp { get; set; }
  
      [DataMember]
      public string Token { get; set; }
  }  
  </code></pre>
  
  <p>WindowsForm code:  </p>
  
  <pre><code>string baseAddress = "http://some_webservice_url";
  HttpWebRequest request = (HttpWebRequest)WebRequest.Create(baseAddress);
  request.Method = "POST";
  request.ContentType = "application/json";
  
  DeviceInfo customData = new DeviceInfo();
  customData.DeviceCode = "123456";
  customData.ClientKey = "hfhf8djf89dfk9";
  customData.Timestamp = "12-12-2013";
  customData.Token = "444sdf54df544r";
  
  byte[] byteArray = Encoding.UTF8.GetBytes(Serialize&lt;DeviceInfo&gt;(customData));
  request.ContentLength = byteArray.Length;
  Stream dataStream = request.GetRequestStream();
  dataStream.Write(byteArray, 0, byteArray.Length);
  dataStream.Close();
  
  using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
  {
      MessageBox.Show(Utility.ReadResponse(response));
  }  
  
  // SERIALIZATION PART  
  
  public static string Serialize&lt;T&gt;(T obj)
  {
      System.Runtime.Serialization.Json.DataContractJsonSerializer serializer = new System.Runtime.Serialization.Json.DataContractJsonSerializer(obj.GetType());
      MemoryStream ms = new MemoryStream();
      serializer.WriteObject(ms, obj);
      string retVal = Encoding.Default.GetString(ms.ToArray());
      ms.Dispose();
  
      return retVal;
  }
  
  public static byte[] ReadFully(Stream input)
  {
      byte[] buffer = new byte[16 * 1024];
      using (MemoryStream ms = new MemoryStream())
      {
          if (input != null)
          {
              int read;
              while ((read = input.Read(buffer, 0, buffer.Length)) &gt; 0)
              {
                  ms.Write(buffer, 0, read);
              }
          }
          return ms.ToArray();
      }
  }  
  </code></pre>
  
  <p>What it does is sending DeviceInfo to the server.<br>
  But <strong>how could I do this using a class object with <code>Stream property</code>:</strong>  </p>
  
  <pre><code>public class PhotoData
  {
      public string ProductId { get; set; }
      public string PhotoId { get; set; }
      public System.IO.Stream FileData { get; set; }
  }  
  </code></pre>
  
  <p>This is what I have done so far and it is throwing an error, saying   </p>
  
  <pre><code>    Type 'System.IO.FileStream' with data contract name 'FileStream:http://schemas.datacontract.org/2004/07/System.IO' 
  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.  
  
  StreamReader sr = new StreamReader(openFileDialog1.FileName);
  byte[] fileStream = ReadFully(sr.BaseStream);
  
  PhotoData pd = new PhotoData();
  pd.FileData = sr.BaseStream;
  pd.ProductId = "121513542454545";
  pd.PhotoId = textBox4.Text;
  
  baseAddress = "http://someurl/rest/UploadPhotoStream";
  request = (HttpWebRequest)WebRequest.Create(new Uri(baseAddress));
  request.Method = "POST";
  request.ContentType = "application/octet-stream";
  
  byteArray = Encoding.UTF8.GetBytes(Serialize&lt;PhotoData&gt;(pd));
  request.ContentLength = byteArray.Length;
  Stream dataStream2 = request.GetRequestStream();
  dataStream2.Write(byteArray, 0, byteArray.Length);
  dataStream2.Close();
  
  using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
  {
      MessageBox.Show(Utility.ReadResponse(response));
  }
  </code></pre>