Azure functions in .net6.0

I have an azure function in .net6.0 converted from framework 4.72. My code as follows,

 [FunctionName("GetAllProducts")] 
    public static async Task<HttpResponseMessage> GetAllProducts(
        [HttpTrigger(
            AuthorizationLevel.Function, 
            "get", 
            Route = null)]HttpRequestMessage request)
    {
        try
        {
            IDocumentClient documentClient= new DocumentClient(new Uri(EndpointUrl), PrimaryKey);
            var documentsStore = new DbRepository.DocumentDBRepository<ProductsStore>(DatabaseName, documentClient, ProductsCollection);

            var queryable = documentsStore.GetQueryable();
            var result =
                await documentsStore.ExecuteQueryAsync(queryable.Where(a => a.Id == Guid.Parse(DocumentId)));

           var products = result.SelectMany(pr => pr.products).ToList();

            if (!result.Any())
            {
                return req.CreateResponse(HttpStatusCode.NotFound, $"Error: Products not found.");
            }

            return req.CreateResponse(HttpStatusCode.OK, products, "text/json");
        }
        catch (Exception ex)
        {
            return req.CreateResponse(HttpStatusCode.BadRequest, $"Failure: {ex.GetType().Name} occurred {ex.Message}");
        }
    }

The code is working when I run locally within visual studio, but when I upload it to azure it returns 500 OK response. Any ideas?

1

I am able to get the expected response using below code.

using System;
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 Microsoft.Azure.Documents;
using Microsoft.Azure.Documents.Client;
using System.Linq;

namespace _78932604
{
    public static class Function1
    {
        private static readonly string EndpointUrl = "https://{cosmosDBName}.documents.azure.com:443/";
        private static readonly string PrimaryKey = "yYgw*********OaEQ==";
        private static readonly string DatabaseName = "TestDB";
        private static readonly string ProductsCollection = "TestContainer";
        private static readonly string DocumentId = "b1a7b3b8-1f44-4e3f-a8f4-e4e5e4e9c3c5";

        [FunctionName("GetAllProducts")]
        public static async Task<IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = null)] HttpRequest req,
            ILogger log)
        {
            try
            {
                
                IDocumentClient documentClient = new DocumentClient(new Uri(EndpointUrl), PrimaryKey);
                var documentsStore = new DbRepository.DocumentDBRepository<ProductsStore>(DatabaseName, documentClient, ProductsCollection);

                var queryable = documentsStore.GetQueryable();
                var result = await documentsStore.ExecuteQueryAsync(queryable.Where(a => a.Id == Guid.Parse(DocumentId)));

                var products = result.SelectMany(pr => pr.products).ToList();

                if (!products.Any())
                {
                    return new NotFoundObjectResult("Error: Products not found.");
                }

                return new OkObjectResult(products);
            }
            catch (Exception ex)
            {
                return new BadRequestObjectResult($"Failure: {ex.GetType().Name} occurred {ex.Message}");
            }
        }
    }
}

DbRepository

using Microsoft.Azure.Documents.Client;
using Microsoft.Azure.Documents.Linq;
using Microsoft.Azure.Documents;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _78932604
{
    public class DbRepository
    {
        private readonly string databaseName;
        private readonly IDocumentClient client;

        public DbRepository(string databaseName, IDocumentClient client)
        {
            this.databaseName = databaseName;
            this.client = client;
        }

        public class DocumentDBRepository<T> where T : class
        {
            private readonly Uri collectionUri;
            private readonly IDocumentClient client;

            public DocumentDBRepository(string databaseName, IDocumentClient client, string collectionName)
            {
                this.client = client;
                this.collectionUri = UriFactory.CreateDocumentCollectionUri(databaseName, collectionName);
            }

            public IQueryable<T> GetQueryable()
            {
                return client.CreateDocumentQuery<T>(collectionUri, new FeedOptions { MaxItemCount = -1 });
            }

            public async Task<IEnumerable<T>> ExecuteQueryAsync(IQueryable<T> query)
            {
                var documentQuery = query.AsDocumentQuery();
                var results = new List<T>();

                while (documentQuery.HasMoreResults)
                {
                    var response = await documentQuery.ExecuteNextAsync<T>();
                    results.AddRange(response);
                }

                return results;
            }
        }
    }
}

I can get the output in local as well as in Function App.

Recognized by Microsoft Azure Collective

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật