Azure / Azure/azure-functions-dotnet-worker

Unable to bind HTTP JSON body for CosmosDB input query

Open
#929 8 comments 1 reaction 2 assignees Claimed by @Ved2806 View on GitHub
needs-investigation
Dominant language
C#
Stars
466
Forks
215
Avg merge
3d 10h
Merged PRs (30d)
7

Description

Hello,

I'm trying to write an isolated C# function that looks something like this:

```
public class MyResponse
{
[CosmosDBOutput("MyCosmosDB", "MyCosmosDBContainer")]
public MyCosmosDBDocument UpdatedItem {get; set;}
public HttpResponseData Response {get; set;}
}
```

```
[Function("UpdateDynamicConfigurationStatus")]
public async Task UpdateMyCosmosDBItem(
[HttpTrigger(AuthorizationLevel.Function, "post")]HttpRequestData req,
[CosmosDBInput(
databaseName:"MyCosmosDB",
collectionName:"MyCosmosDBContainer",
SqlQuery = "select * from MyItemCollection dc where dc.Name={Name}",
ConnectionStringSetting = "CosmosDBConnectionString")] IReadOnlyList documents
)
{
///... do some work ...
}
```

In this case the request will contain a JSON body that looks something like this:

```
{
"Name": "Item1",
"IsAvailable":"True"
}
```

With Name being a unique ID and IsAvailable being a value I wish to set on the CosmosDB document.

However, I am unable to figure out the bindings to make this work and [the only documentation exists for In-Process functions](https://docs.microsoft.com/en-us/azure/azure-functions/functions-bindings-cosmosdb-v2-input?tabs=in-process%2Cfunctionsv2&pivots=programming-language-csharp). The [binding expression documentation](https://docs.microsoft.com/en-us/azure/azure-functions/functions-bindings-expressions-patterns#json-payloads) tells me that if I modify function.json I can use it but function.json is generated only at runtime based on the attributes outlined in the code. I've looked at developing an [imperative binding](https://docs.microsoft.com/en-us/azure/azure-functions/functions-dotnet-class-library?tabs=v2%2Ccmd#binding-at-runtime) at runtime but that requires using the In-Process/WebJob SDK. Every time I try to import a class from the WebJob SDK I get a build error stating that I cannot mix function SDKs like that.

It seems like my remaining options are to either leverage dependency injection with the CosmosDB SDK or to put the value(s) I wish to query into the HTTP Trigger Route and make the payload only the JSON body. The latter isn't desireable as I've found that the Azure Function VSCode extension doesn't really like HTTP Trigger Routes. The former is doable but kind of messy.

Doing the filtering at runtime using LINQ also isn't desireable as the CosmosDB is large enough that getting all of the items causes the function to time out. I could try to define a static filter, like only looking back X days, but would prefer to be more flexible than that.

Is there another solution I'm missing or maybe some documentation somewhere that details how I can achieve what I want?

Thanks!

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.