Skip to content

fix: use minimum 6.0.0 System.Text.Json version#2720

Merged
mxschmitt merged 1 commit into
mainfrom
System.Text.Json-version
Oct 13, 2023
Merged

fix: use minimum 6.0.0 System.Text.Json version#2720
mxschmitt merged 1 commit into
mainfrom
System.Text.Json-version

Conversation

@mxschmitt
Copy link
Copy Markdown
Contributor

@mxschmitt mxschmitt commented Oct 9, 2023

@mxschmitt
Copy link
Copy Markdown
Contributor Author

mxschmitt commented Oct 10, 2023

@campersau seems like a bug in .NET Azure Functions to me in the way how they bundle their assemblies. (they don't publish a custom System.Text.Json package). Curious about your input here.

@campersau
Copy link
Copy Markdown
Contributor

The problem is that the AzureFunctionsTools which hosts the local functions does include the System.Text.Json.dll with version 6.0.xx as you can see in %LOCALAPPDATA%\AzureFunctionsTools\Releases\4.55.0\cli_x64. I don't know if the same is true when the function gets actually deployed.

In .NET Framework you can work around that by using <bindingRedirect> and in .NET you can workaround that with this code which tries to load assemblies without doing a version check:

AppDomain.CurrentDomain.AssemblyResolve += CurrentDomain_AssemblyResolve;
private Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
{
    var name = new AssemblyName(args.Name);
    var dll = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, name.Name + ".dll");
    if (File.Exists(dll))
    {
        return Assembly.LoadFrom(dll);
    }
    return null;
}

E.g. .NET will try to load System.Text.Json = 7.0.0 and fails so the custom CurrentDomain_AssemblyResolve is called and will resolve System.Text.Json = 6.0.0 from the AzureFunctionsTools directory and .NET will just use the returned assembly without additional checks and everything works*.

In my opinion libraries should always try to use the lowest dependency version possible since the users can then "override" it by specifying it explicitly in their project.
E.g. if Playwright requires System.Text.Json >= 6.0.0 and I explicitly include a PackageReference to System.Text.Json = 7.0.0 then Playwright will also use 7.0.0 without any hacks*.

* This of course only works at runtime if there aren't any breaking changes which Playwirght uses.

I am not sure what #2408 was about exactly as there isn't enough information for me to reproduce it, maybe there are other solutions for this too.

@mxschmitt
Copy link
Copy Markdown
Contributor Author

Thanks for your great investigation! 💯

I don't know if the same is true when the function gets actually deployed.

From my experiments System.Text.Json was not included in the zip which got uploaded to the Azure Function.

I am not sure what #2408 was about exactly as there isn't enough information for me to reproduce it, maybe there are other solutions for this too.

Yeah for me it also wasn't clear anymore, I made a quick .NET 7 test where it looked good.

I guess I'll merge this for now (which is like a revert of #2410 so it should be fine). Thanks again!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] Azure Function Setup Issue: System.Text.Json version issue

3 participants