Search code examples
c#azureazure-functionsmongodb-.net-driver.net-7.0

Azure Function throwing error Could not load file or assembly 'System.Collections.Specialized


I have the following azure function that works locally and remotely (in Azure), but when I try to use a System.Web.HttpUtility class to parse a filter, I get the following error:

System.Private.CoreLib: Exception while executing function: GetPetitions. MasterProbaterApi: Could not load file or assembly 'System.Collections.Specialized, Version=7.0.0.0,.... The system cannot find the file specified.

This line is the culprit: "System.Collections.Specialized.NameValueCollection xxx = ..." (see below). With this line commented out, the function works fine.

using MasterProbaterRepo;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using System.Threading.Tasks;

namespace MyNamespaceApi
{
    public static class MyPoco
    {
        private static IMyPocoService _repo = new MongoRepo(); 
        
        [FunctionName("GetMyPoco")]
        public static async Task<IActionResult> GetMyPocoAsync([HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "myPoco")] HttpRequest req, ILogger log)
        {
            //System.Collections.Specialized.NameValueCollection xxx = System.Web.HttpUtility.ParseQueryString("?param1=aaa&param2=bbb"); //causes runtime error
            var myPoco = await _repo.GetMyPocoAsync();
            var json = JsonConvert.SerializeObject(myPoco);
            return new OkObjectResult(json);
        }

    }
}

Solution

  • System.Private.CoreLib: Exception while executing function: GetPetitions. MasterProbaterApi: Could not load file or assembly 'System.Collections.Specialized, Version=7.0.0.0,.... The system cannot find the file specified.

    This indicates that System.Collections.Specialized dependency is missing in the project or the version of the package is not compatible with the project.

    If you are trying to include the assembly 'System.Collections.Specialized' version=7.0.0 in your project, it is still not released yet. As mentioned in the doc, The latest version of the package System.Collections.Specialized is 4.3.0.

    enter image description here

    enter image description here

    So, try to reduce the version of System.Collections.Specialized in your project to 4.3.0 or below versions.


    I have created a Http Trigger Azure function and used the package System.Collections.Specialized:

    Code Snippet:

     [FunctionName("Function1")]
            public static async Task<IActionResult> Run(
                [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
                ILogger log)
            {
                System.Collections.Specialized.NameValueCollection query = System.Web.HttpUtility.ParseQueryString(req.QueryString.Value);
                string param1 = query["param1"];
                string param2 = query["param2"];
                string responseMessage = string.IsNullOrEmpty(param1) || string.IsNullOrEmpty(param2)
                    ? "Please pass param1 and param2 in the query string"
                    : $"Hello, {param1} and {param2}. This HTTP triggered function executed successfully.";
                retur
    

    Dependencies:

    <ItemGroup>
        <PackageReference Include="Microsoft.NET.Sdk.Functions" Version="4.1.1" />
        <PackageReference Include="System.Collections.Specialized" Version="4.3.0" />
      </ItemGroup>
    

    Output: enter image description here

    enter image description here

    enter image description here