Azure Functions v 2.0 HttpTrigger with CosmosDB Client Tips

I spent several hours to setup .NetCore 2.0 based Azure Functions Environment with CosmosDB. I’d like to share my work around to record what I encountered. These tips are not special. You can find these in the GitHub issues. However, it might be help to reduce the time for some search.

What I tried to do?

I create an Azure Functions app with .Net Core 2.0 with CosmosDB. Currently I don’t use Cosmos Trigger. However, I guess you can do it the same thing to it.

  • NetStandard.Library v 2.0.1
  • Microsoft.Azure.DocumentDB.Core 1.9.1
  • Visual Studio 2017 15.6.0 Preview 7.0
  • Azure Functions and Web Jobs Tool 15.0.40322.0

If you want to use CosmosDB extensions you can use Microsoft.Azure.WebJobs.Extensions.CosmosDB v3.0.0-beta7 It is the same as the DocumentDB.Core 1.5.1

However, I encounter a lot of issues. You can find the version match on this announcement issue.

Runtime V2 with missing assemblies

I encounter a lot of these.

1. Microsoft.AspNetCore.Mvc.Abstractions

[01/04/2018 05:01:07] System.Private.CoreLib: Could not load file or assembly 'Microsoft.AspNetCore.Mvc.Abstractions, Version=2.0.2.0, Culture=neutral, PublicKeyToken=adb9793829ddae60'

When you start Visual Studio debugging, it might happen. On the debugging feature, the Azure Functions runtime of your Visual Studio might old. Currently, we have no way to upgrade it. Instead, we can use the Azure Functions CLI instead. For installing Azure Functions CLI, you can refer this.

Visual Studio 2017 > Project right click > Properties > Debug . Then configure like this. This is just configure to use the Azure Functions CLI.

For more details discussion

System.Net.Http.Formatting

You might get this missing library, You might write HttpTrigger with Old Style. Unfortunately, we have no way to solve until now. However, if you switch it to the new style, You might not encounter this error.

Could not find or load a specific file. (Exception from HRESULT: 0x80131621). System.Private.CoreLib: Could not load file or assembly 'System.Net.Http.Formatting, Version=5.2.4.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'.

This is the old style. (This is the V1 Style)

[FunctionName("HttpTriggerCSharp")] public static async Task<HttpResponseMessage> Run(     [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)]HttpRequestMessage req,      TraceWriter log)
:

The new Style V2

[FunctionName("Function1")]
public static IActionResult Run([HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)]HttpRequest req, TraceWriter log)
{
log.Info("C# HTTP trigger function processed a request.");

string name = req.Query["name"];

string requestBody = new StreamReader(req.Body).ReadToEnd();
dynamic data = JsonConvert.DeserializeObject(requestBody);
name = name ?? data?.name;

return name != null
? (ActionResult)new OkObjectResult($"Hello, {name}")
: new BadRequestObjectResult("Please pass a name on the query string or in the request body");
}

Microsoft.Azure.Documents.ServiceInterop.dll

[01/04/2018 08:50:28] Report Status error: One or more errors occurred. (Unable to load DLL 'Microsoft.Azure.Documents.ServiceInterop.dll': The specified module could not be found. (Exception from HRESULT: 0x8007007E))

(Update! 03/05/2018) Rather than use this strategy, you can install Microsoft.Azure.WebJobs.Extensions.CosmosDB 3.0.0-beta7 works now much better!

(Duplicated!)To solve this issue, you can Install Microsoft.Azure.DocumentDB v1.19.1 nuget package. I’m not sure why it solve the problem. The nuget package is for .NetFramework! However, it install two DLLs and win7-x64 runtime DLL for these. Maybe I need to read code and understand .Net Core 2.0 nuget architecture. If you know a good resource, and why it happens. please let me know.

Conclusion

Now you can play with Azure Functions V2 with CosmosDB. :) In the future, the same thing might happen, however, you can check these issues.