title: Error calling Azure Forms Recognizer from within an Azure Blob Trigger Function
body: <p>I get an error (BadRequest) when calling the Azure Forms Recognizer .NET SDK from within an Azure Function (Blob Trigger)</p>
  
  <p>I'm basically using the example code in the C# quickstart from the documentation. I have trained the model and can successfully call Form Recognizer from a .NET Core command line app, and also using cURL. I now want to trigger the same flow but when a PDF is uploaded to an Azure Storage Blob. Using the same modelId, subscription and endpoint in the Function I get a BadRequest error when calling AnalyzeWithCustomModelAsync(). I've tried many variations on the myBlob Stream, creating a FileStream, MemoryStream, creating a new Stream using the uri passed into the Function and more.</p>
  
  <pre><code>[FunctionName("ImportInvoice")]
          public static async System.Threading.Tasks.Task RunAsync([BlobTrigger("originals/{name}", Connection = "invoiceConn")]Stream myBlob, string name, Uri uri, string blobTrigger, ILogger log)
          {
              log.LogInformation($"ImportInvoice Function triggered by new invoice: {name} size: {myBlob.Length} Bytes");
              log.LogInformation($"Invoice Uri: {uri.AbsoluteUri}");
              log.LogInformation($"System.IO.Stream.CanRead is: {myBlob.CanRead}");
  
              try
              {
                  IFormRecognizerClient formClient = new FormRecognizerClient(new ApiKeyServiceClientCredentials(subscriptionKey))
                  {
                      Endpoint = formRecognizerEndpoint
                  };
  
                  using (Stream invoiceStream = myBlob)
                  {
                      log.LogInformation($"About to analyse with custom model {modelId}");
  
                      // Fails here with BadRequest
                      AnalyzeResult result = await formClient.AnalyzeWithCustomModelAsync(modelId, myBlob, contentType: "application/pdf");
  
                      log.LogInformation("Invoice analysed");
  
                      foreach (var page in result.Pages)
                      {
                          foreach (var kv in page.KeyValuePairs)
                          {
                              if (kv.Key.Count &gt; 0 &amp;&amp; kv.Value.Count &gt; 0)
                                  log.LogInformation(kv.Key[0].Text + ": " + kv.Value[0].Text);
                          }
                      }
                  }
              }
              catch (ErrorResponseException e)
              {
                  log.LogInformation("Analyze PDF form : " + e.Message);
              }
              catch (Exception ex)
              {
                  log.LogInformation("Analyze PDF form : " + ex.Message);
              }
          }
  </code></pre>
  
  <p>I'm passing in the modelId, the Stream passed to the function (myBlob) and the correct content type (application/pdf). I expect the formClient.AnalyzeWithCustomModelAsync(modelId, myBlob, contentType: "application/pdf") call to succeed and to see the key and values (the same code works outside a function) but I get the following:</p>
  
  <pre><code>Executing 'ImportInvoice' (Reason='New blob detected: originals/NewCareInvoice5.pdf', Id=63f26c79-f269-4ea8-b6e8-8b4f6d6eb6e2)
  ImportInvoice Function triggered by new invoice: NewCareInvoice5.pdf size: 134507 Bytes
  Invoice Uri: https://myinvoices.blob.core.windows.net/originals/NewCareInvoice5.pdf
  System.IO.Stream.CanRead is: True
  About to analyse with custom model &lt;correct model id shown here&gt;
  Analyze PDF form : Operation returned an invalid status code 'BadRequest'
  </code></pre>
  
  <p>I'm assuming the issue is the Stream but I've gone round in circles trying to reference it or create or copy a new stream and seem to be missing something. I can't get any more details on the error (e.g. from InnerException).</p>
  
  <p>Any help much appreciated!</p>