title: System.AccessViolationException' occurred in System.Net.Http.Formatting.dll on Azure
body: <p>I'm pulling my hair out over this problem, I'm wondering if anyone can help.</p>
  
  <p>I'm attempting to create a POST and PUT http web api method to handle both JSON data and image data. This all works perfectly when running on my local machine using the Azure emulator but as soon as I publish to the server I get an AccessViolationException error saying:</p>
  
  <blockquote>
    <p>Additional information: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.</p>
  </blockquote>
  
  <p>I'm using the following code and when debugging against Azure the code fails when reading the multipart data.</p>
  
  <pre><code>public async Task&lt;bool&gt; BindModelTask(HttpActionContext actionContext, System.Web.Http.ModelBinding.ModelBindingContext bindingContext) {
          try {
              if (actionContext.Request.Content.IsMimeMultipartContent()) {
                  var provider = await actionContext.Request.Content.ReadAsMultipartAsync&lt;InMemoryMultipartFormDataStreamProvider&gt;(new InMemoryMultipartFormDataStreamProvider());
  
                  FormCollection formData = provider.FormData;
                  FileData[] fileData = provider.GetFiles().Result;
  
                  if (formData != null &amp;&amp; formData.Count == 1) {
                      Pet pet = (Pet)Newtonsoft.Json.JsonConvert.DeserializeObject(formData[0], typeof(Pet));
  
                      if (pet != null) {
                          petModel petModel = new petModel(pet);
                          if (fileData != null) {
                              petModel.file = fileData.FirstOrDefault();
                          }
  
                          bindingContext.Model = petModel;
                      }
                  }
                  return true;
              }
          }
          catch (Exception ex) { throw ex; }
      }
  }
  
  
  public class InMemoryMultipartFormDataStreamProvider : MultipartStreamProvider {
      private FormCollection _formData = new FormCollection();
      private List&lt;HttpContent&gt; _fileContents = new List&lt;HttpContent&gt;();
  
      // Set of indexes of which HttpContents we designate as form data
      private Collection&lt;bool&gt; _isFormData = new Collection&lt;bool&gt;();
  
      /// &lt;summary&gt;
      /// Gets a &lt;see cref="NameValueCollection"/&gt; of form data passed as part of the multipart form data.
      /// &lt;/summary&gt;
      public FormCollection FormData {
          get { return _formData; }
      }
  
      /// &lt;summary&gt;
      /// Gets list of &lt;see cref="HttpContent"/&gt;s which contain uploaded files as in-memory representation.
      /// &lt;/summary&gt;
      public List&lt;HttpContent&gt; Files {
          get { return _fileContents; }
      }
  
      /// &lt;summary&gt;
      /// Convert list of HttpContent items to FileData class task
      /// &lt;/summary&gt;
      /// &lt;returns&gt;&lt;/returns&gt;
      public async Task&lt;FileData[]&gt; GetFiles() {
          return await Task.WhenAll(Files.Select(f =&gt; FileData.ReadFile(f)));
      }
  
      public override Stream GetStream(HttpContent parent, HttpContentHeaders headers) {
          // For form data, Content-Disposition header is a requirement
          ContentDispositionHeaderValue contentDisposition = headers.ContentDisposition;
          if (contentDisposition != null) {
              // We will post process this as form data
              _isFormData.Add(String.IsNullOrEmpty(contentDisposition.FileName));
  
              return new MemoryStream();
          }
  
          // If no Content-Disposition header was present.
          throw new InvalidOperationException(string.Format("Did not find required '{0}' header field in MIME multipart body part..", "Content-Disposition"));
      }
  
      /// &lt;summary&gt;
      /// Read the non-file contents as form data.
      /// &lt;/summary&gt;
      /// &lt;returns&gt;&lt;/returns&gt;
      public override async Task ExecutePostProcessingAsync() {
          // Find instances of non-file HttpContents and read them asynchronously
          // to get the string content and then add that as form data
          CloudBlobContainer _container = StorageHelper.GetStorageContainer(StorageHelper.StorageContainer.Temp);
  
          for (int index = 0; index &lt; Contents.Count; index++) {
              if (_isFormData[index]) {
                  HttpContent formContent = Contents[index];
                  // Extract name from Content-Disposition header. We know from earlier that the header is present.
                  ContentDispositionHeaderValue contentDisposition = formContent.Headers.ContentDisposition;
                  string formFieldName = UnquoteToken(contentDisposition.Name) ?? String.Empty;
  
                  // Read the contents as string data and add to form data
                  string formFieldValue = await formContent.ReadAsStringAsync();
                  FormData.Add(formFieldName, formFieldValue);
              }
              else {
                  _fileContents.Add(Contents[index]);
              }
          }
      }
  
      /// &lt;summary&gt;
      /// Remove bounding quotes on a token if present
      /// &lt;/summary&gt;
      /// &lt;param name="token"&gt;Token to unquote.&lt;/param&gt;
      /// &lt;returns&gt;Unquoted token.&lt;/returns&gt;
      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;
      }
  }
  </code></pre>
  
  <p>As I said this code works perfectly when running on my local machine but not when running on Azure.</p>
  
  <p>Any ideas?</p>
  
  <p>Thanks</p>
  
  <p>Neil</p>