title: MultipartMemoryStream in Dot Net Core
body: <p>I have a .Net 4.5.2 App that I am moving to Dot Net Core. This app allows the user to upload a file with Metadata (Angular Client Side) and the Api will handle the request and process the file. Here is the existing code that does that.</p>
  
  <p><strong>Api</strong></p>
  
  <pre><code>[HttpPost]
  [Route("AskQuestions")]
  public void ProvideClarifications(int id)
  {
    var user = base.GetUserLookup();
    if (user != null)
    {
      var streamProvider = new MultiPartStreamProvider();
      IEnumerable&lt;HttpContent&gt; parts = null;
      Task.Factory
          .StartNew(() =&gt; parts = Request.Content.ReadAsMultipartAsync(streamProvider).Result.Contents,
              CancellationToken.None,
              TaskCreationOptions.LongRunning,
              TaskScheduler.Default)
          .Wait();
  
          // do some stuff with streamProvider.FormData
    }
  }
  </code></pre>
  
  <p><strong>Provider to Handle File and MetaData</strong></p>
  
  <pre><code>public class MultiPartStreamProvider : MultipartMemoryStreamProvider
  {
      private string _originalFileName = string.Empty;
      public Dictionary&lt;string, object&gt; FormData { get; set; }
  
      public byte[] ByteStream { get; set; }
  
      public string FileName
      {
          get
          {
              return _originalFileName.Replace("\"", "");
          }
      }
      public MultiPartStreamProvider()
      {
          this.FormData = new Dictionary&lt;string, object&gt;();
      }
      public override Task ExecutePostProcessingAsync()
      {
          foreach (var content in Contents)
          {
              var contentDispo = content.Headers.ContentDisposition;
              var name = UnquoteToken(contentDispo.Name);
  
              if (name.Contains("file"))
              {
                  _originalFileName = UnquoteToken(contentDispo.FileName);
                  this.ByteStream = content.ReadAsByteArrayAsync().Result;
              }
              else
              {
                  var val = content.ReadAsStringAsync().Result;
                  this.FormData.Add(name, val);
              }
          }
          return base.ExecutePostProcessingAsync();
      }
      private static string UnquoteToken(string token)
      {
          if (String.IsNullOrWhiteSpace(token))
          {
              return token;
          }
  
          if (token.StartsWith("\"", StringComparison.Ordinal) &amp;&amp; token.EndsWith("\"", StringComparison.Ordinal) &amp;&amp; token.Length &gt; 1)
          {
              return token.Substring(1, token.Length - 2);
          }
  
          return token;
      }
  }
  static class FormDataExtensions {
      public static Object GetObject(this Dictionary&lt;string, object&gt; dict, Type type)
      {
          var obj = Activator.CreateInstance(type);
  
          foreach (var kv in dict)
          {
              var prop = type.GetProperty(kv.Key);
              if (prop == null) continue;
  
              object value = kv.Value;
              var targetType = IsNullableType(prop.PropertyType) ? Nullable.GetUnderlyingType(prop.PropertyType) : prop.PropertyType;
  
              if (value is Dictionary&lt;string, object&gt;)
              {
                  value = GetObject((Dictionary&lt;string, object&gt;)value, prop.PropertyType); // &lt;= This line                   
              }
              value = Convert.ChangeType(value, targetType);
              prop.SetValue(obj, value, null);
          }
          return obj;
      }
      public static T GetObject&lt;T&gt;(this Dictionary&lt;string, object&gt; dict)
      {
          return (T)GetObject(dict, typeof(T));
      }
      private static bool IsNullableType(Type type)
      {
          return type.IsGenericType &amp;&amp; type.GetGenericTypeDefinition().Equals(typeof(Nullable&lt;&gt;));
      }
  }
  </code></pre>
  
  <p>So this works totally fine targeting framework, but in Core I get an exception on this line</p>
  
  <blockquote>
    <p>.StartNew(() => parts = Request.Content.ReadAsMultipartAsync(streamProvider).Result.Contents,</p>
  </blockquote>
  
  <p><strong>Exception</strong></p>
  
  <blockquote>
    <p>'HttpRequest' does not contain a definition for 'Content' and no extension method 'Content' accepting a first argument of type 'HttpRequest' could be found (are you missing a using directive or an assembly reference?)</p>
  </blockquote>
  
  <p>What do I need to make sure I can get this file and metadata? Is there a way in Core of getting HttpContent from a HttpRequest</p>