Search code examples
c#.netazure-functionsazure-cosmosdbazure-http-trigger

The type or namespace name 'Id' could not be found (are you missing a using directive or an assembly reference?) CosmosDBOutput Problem


So the problem that I am encoutring is that I can't use "ID" again, it keeps giving me error for my CosmosDBOutput, I'm not sure why that is but I need help with this if anyone can help fix it.

Here is the Code i have so far and I am using .Net 6 LTS using C#

using System;
using System.IO;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using Microsoft.Azure.Functions.Worker;
using System.Net.Http;
using System.Text;

namespace Company.Function
{
    public static class GetResumeCounter
    {
        [FunctionName("GetResumeCounter")]
        public static HttpResponseMessage Run(
            [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
            [CosmosDBInput(databaseName:"AzureResume", containerName: "Counter", Connection = "AzureResumeConnectionString", Id = "1", PartitionKey = "1")] Counter counter,
            [CosmosDBOutput(databaseName:"AzureResume", containerName: "Counter", Connection = "AzureResumeConnectionString", Id = "1", PartitionKey = "1")] out Counter updatedcounter,

            ILogger log)
        {
            log.LogInformation("C# HTTP trigger function processed a request.");

            updatedcounter = counter;
            updatedcounter.Count += 1;

            var jsonToRetun = JsonConvert.SerializeObject(counter);

            return new HttpResponseMessage(System.Net.HttpStatusCode.OK)
            {
                Content = new StringContent(jsonToRetun, Encoding.UTF8, "application/json")
            };
        }
    }
}

I am trying to get it to build using dotnet build but I keep getting the error: The type or namespace name 'Id' could not be found (are you missing a using directive or an assembly reference?)

for my CosmosDBOutput please help


Solution

  • Thanks @David Makogon

    As David Mentioned Id property does not exist in [CosmosDbOutput]

    You can use CosmosClient to update the existing data, as [CosmosDbOutput] overrides the data.

    This worked for me.

    My Code :

    using System.Threading.Tasks;
    using Microsoft.AspNetCore.Mvc;
    using Microsoft.Azure.WebJobs;
    using Microsoft.Azure.WebJobs.Extensions.Http;
    using Microsoft.Extensions.Logging;
    using Microsoft.Azure.Cosmos;
    using System.Net;
    using Microsoft.AspNetCore.Http;
    
    
    namespace FunctionApp13
    {
        public static class Function
        {
            private static readonly string cosmosEndpoint = "https://resumedbcount.documents.azure.com:443/";
            private static readonly string cosmosKey = "xxxxxxxxxxxxxxxxxxxxxxx";
            private static readonly string databaseName = "Resume";
            private static readonly string containerName = "Counter";
    
            private static readonly CosmosClient cosmosClient = new CosmosClient(cosmosEndpoint, cosmosKey);
            private static readonly Container container = cosmosClient.GetContainer(databaseName, containerName);
    
    
            [FunctionName("Function")]
            public static async Task<IActionResult> RunAsync(
                [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
                ILogger log)
            {
                log.LogInformation("C# HTTP trigger function processed a request.");
    
                // Get the document by ID
                var counter = await GetCounterById("1", "1");
    
                if (counter == null)
                {
                    counter = new Counter { id = "1", PartitionKey = "1", Count = 0 };
                }
    
                // Update the document
                counter.Count++;
    
                // Replace the document in Cosmos DB
                var response = await container.ReplaceItemAsync(counter, counter.id, new PartitionKey(counter.PartitionKey));
    
                return new OkObjectResult("replaced successfully");
            }
    
            private static async Task<Counter> GetCounterById(string id, string partitionKey)
            {
                try
                {
                    var response = await container.ReadItemAsync<Counter>(id, new PartitionKey(partitionKey));
                    return response.Resource;
                }
                catch (CosmosException ex) when (ex.StatusCode == HttpStatusCode.NotFound)
                {
                    return null;
                }
            }
        }
    
            public class Counter
        {
            public string id { get; set; }
            public string PartitionKey { get; set; }
            public int Count { get; set; }
        }
    
    }
    

    Before data:

    {
        "id": "1",
        "PartitionKey": "1",
        "Count": 11
    }
    

    OUTPUT:

    After executing function:

    {
        "id": "1",
        "PartitionKey": "1",
        "Count": 12
    }