max
max

Reputation: 10454

Unittesting Azure System.IO.FileLoadException: Could not load file or assembly 'System.Text.Json'

I have a library that retrieves secrets from Azure. I can use it without a problem from a console app, but when I use it from unittest, I get an error:

Test method UnitTests.UnitTest1.TestCredentials threw exception: System.IO.FileLoadException: Could not load file or assembly 'System.Text.Json, Version=4.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040) at Common.Credentials.GetDbConnStr(String databaseName) at UnitTests.UnitTest1.TestCredentials()

using System;
using System.Configuration;
using System.Text.Json;
using Azure.Identity;
using Azure.Security.KeyVault.Secrets;

namespace Common
{
    public class Credentials
    {
        public static string GetDbConnStr(string databaseName)
        {
            string keyVaultUrl = "https://cat.vault.azure.net/";
            Console.WriteLine($"Retrieving credentials for {databaseName}");
            var client = new SecretClient(new Uri(keyVaultUrl), new DefaultAzureCredential());
            var res = client.GetSecret("conn");
            return res.Value.Value.ToString() + $"Database={databaseName}";
        }
    }
}

and here is the test:

using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Net;
using System.Data.SqlClient;
using Common;

namespace UnitTests
{
    [TestClass]
    public class UnitTest1
    {
        [TestMethod]
        public void TestCredentials()
        {
            string conn_str = Credentials.GetDbConnStr("test");
        }
    }
}

Note that I can see System.Text.Json.dll in the bin/Debug folder of the test project.

Upvotes: 0

Views: 7142

Answers (2)

Sunny
Sunny

Reputation: 2507

I fixed this issue at my end by installing the nuget package with latest version of System.Text.Json

Upvotes: 0

max
max

Reputation: 10454

Thanks to Anand Sowmithiran who posted the comment

Check your versions of Function runtime, .NET, etc. - refer this Github issue

I upgraded System.Text.Json to version 6.0.2 as suggested in the issue and the problem went away.

Upvotes: 3

Related Questions