.Net Framework 4.8, Azure.Storage.Blobs 12.8.0, UploadBlob could not load file or assembly System.Buffers 4.0.2.0

AdamLeffert-5988 71 Reputation points
2021-02-26T18:50:20.513+00:00

.Net Framework 4.8 is my target platform (WinForms).

I created a .Net Framework 4.8 unit test project, to isolate the code.

C# project. VS 2019 Pro, latest (as of 2/26/21).

Azure subscription. Created a storage account.

Test project has auto generate binding redirects enabled. I tried disabling it and creating specific binding redirects. Didn't help.

Code to allocate a BlobContainerClient works. Properties of this object are populated.

using (var stream = new MemoryStream(byteArray))
{
client.UploadBlob("test blob", stream);
}

fails. byteArray is allocated and filled.

Exception:

System.IO.FileLoadException: 'Could not load file or assembly 'System.Buffers, Version=4.0.2.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)'

Excluded any other code from the unit test project that could be causing a conflict.

If Azure.Storage.Blobs needs this version of System.Buffers, shouldn't ASB include this reference automatically?

Trying to add this System.Buffers reference via NuGet doesn't work. There is no publicly available version 4.0.2 of this package. Earlier or later packages don't work.

How to get this code to work?

Azure Blob Storage
Azure Blob Storage
An Azure service that stores unstructured data in the cloud as blobs.
2,427 questions
0 comments No comments
{count} votes

Accepted answer
  1. deherman-MSFT 33,306 Reputation points Microsoft Employee
    2021-02-26T19:04:15.863+00:00

    @AdamLeffert-5988
    Here is a Stack Overflow thread where they were able to resolve a similar issue with the same System.Buffers 4.0.2.0 error. Try this out and see if it resolves your issue. If not, I recommend filing this as a bug on the SDK issues page.

    -------------------------------

    Please don’t forget to "Accept the answer" and “up-vote” wherever the information provided helps you, this can be beneficial to other community members.


2 additional answers

Sort by: Most helpful
  1. AdamLeffert-5988 71 Reputation points
    2021-02-26T22:59:01.543+00:00

    Thank you for your quick reply.

    Three parts to this reply:

    1) It works now.
    2) What I think happened.
    3) We need better Visual Studio tooling around this.

    1) It works now.

    As I started to work through the suggestions on the StackOverflow page, some seemed like good ideas and some seemed like setting up fragile situations that will just break later. I will explain in item #3.

    As I worked through the suggestions, I was carefully writing up a reply stating that there were only 3 NuGet packages in my test project:

    a) MSTest.TestAdapter
    b) MSTest.TestFramework
    c) Azure.Storage.Blobs, v. 12.8.0

    As I started to type out "The two NuGet packages for unit testing couldn't be the problem" I realized there was no logical reason this must be true. I had unconsciously assumed that unit test packages were clean or special in some way that would prevent them from causing conflicts. Turns out this isn't true.

    I took the exact same code, moved it to a .Net Framework 4.8 console app and it runs fine.

    2) What I think happened.

    NuGet UI (in Visual Studio) says that MSTest.TestFramework has no dependencies.

    MSTest.TestAdapter depends on
    .NetCoreApp
    NETStandardLibrary
    System.Diagnostics.TextWriterTraceListener

    if I'm reading the NuGet dialog correctly.

    Note that none of these words mention System.Buffers. Is there some chain of dependencies that includes System.Buffers? No reasonable way for me to know.

    What I think happened, and please read this closely, is that MSTest.TestAdapter caused Azure.Storage.Blobs to try to load an OLDER version of System.Buffers (4.0.2.0), which is not even available via NuGet or through the command-line package manager, as far as I can tell.

    Bad enough if MSTest needs a new version and Azure.Storage.Blobs needs an old version, except that's not what happened, and both of these packages are new.

    Please correct me if I'm misinterpreting, but I think I just spent an entire day (actually more) determining that the unit test package causes the Azure blobs package to try to use an outdated version of System.Buffers. This info is not available at any top-level UI that I can find in VS or NuGet. Maybe it is imputable through some amount of sleuthing of dependencies.

    Which brings me to #3.

    3) We need better tooling around this.

    Or, failing that, we need a video explaining how to troubleshoot these situations.

    I tried auto-generating redirects. I tried manual redirects. Even if manual redirects worked, I'm concerned that I'd only be kicking the can down the road, and that adding another NuGet package would cause a new conflict.

    I get that dependencies exist. I'm OK with some amount of fiddling. But having two brand-new packages force the code to find an old one that isn't published is scary.

    If I'm misunderstanding how this stuff works, please correct me.

    Otherwise, we need to make this easier. I've been a dev for over 40 years. I've been a MS dev for 20 years. I architect/code/mentor over 40 hours a week, every week. This is too much to put on devs, from junior to senior.


  2. Yogesh Gugale 0 Reputation points
    2023-03-27T09:38:57.19+00:00

    Getting the below error, when try to upload a blob.

    Azure storage blob v12.15.0

    Could not load file or assembly 'System.Buffers, Version=4.0.2.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)