'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.
Solution 1:[1]
Thanks @anand. I upgraded System.Text.Json to version 6.0.2 and the problem went away.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
