Azure Functions: The annotation for nullable reference types should only be used in code within a '#nullable' annotations context
08:13 19 Dec 2025

I get this warning in this line in my Azure Functions project in Visual Studio Code:

Dictionary? jsonDict = null;

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

I have the same code in 3 different functions and therefore I get the same warning 3 times.

I have already tried to add the following line in my .csproj file but then I get much more warnings in many more lines of code.

enable

How can I solve the problem? Because enable Nullable just makes it worse.

using System;
using System.Threading.Tasks;
using System.Text.Json;
using System.Collections.Generic;
using PlayFab;
using PlayFab.AdminModels;
using System.IO;
using System.Net;
using PlayFab.Samples;
using System.Linq;
using Microsoft.Azure.Functions.Worker;
using Microsoft.Azure.Functions.Worker.Http;
using Microsoft.Extensions.Logging;  

namespace My.Functions
{
    public class Result
    {
        public bool FunctionSuccessful { get; set; }
        public string Errorline1 { get; set; }
        public string Errorline2 { get; set; }
        public string Errorline3 { get; set; }
        public string Errorline4 { get; set; }
        public string Errorline5 { get; set; }
        public string Errorline6 { get; set; }
        public bool Isexpired { get; set; }
        public bool Isbanned { get; set; }
    }

    public class NewFunction
    {
      private readonly ILogger log;

   // Constructor injects the logger
   public NewFunction(ILogger logger)
   {
       log = logger;
   }
[Function("GetNameFromClient")]
        public async Task GetNameFromClient(
            [HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = null)] HttpRequestData req)
        {
                  // Read JSON body
            string jsonStr = await new StreamReader(req.Body).ReadToEndAsync();
            Dictionary? jsonDict = null;
            if (!string.IsNullOrWhiteSpace(jsonStr))
            {
                try
                {
                    jsonDict = JsonSerializer.Deserialize>(jsonStr);
                }
                catch (JsonException ex)
                {
                    log.LogWarning(ex, "Invalid JSON body.");
                }
            }
            
            ....

            log.LogInformation($"FunctionSuccessful: = {updateaccountrecsuccessful}");
            var result2 = new Result
            {
                FunctionSuccessful = updateaccountrecsuccessful,
                Errorline1 = string.Empty,
                Errorline2 = string.Empty,
                Errorline3 = string.Empty,
                Errorline4 = string.Empty,
                Errorline5 = string.Empty,
                Errorline6 = string.Empty,
                Isexpired = isexpired,
                Isbanned = isbanned
            };
            var res2 = req.CreateResponse(HttpStatusCode.OK);
            res2.Headers.Add("Content-Type", "application/json");
            await res2.WriteStringAsync(JsonSerializer.Serialize(result2));
            return res2;
        }
      }
    }

In this picture, I enabled Nullable: Nullable enabled

In this picture, I have not enabled Nullable: Nullable is not enabled

c# azure-functions