Binding redirects in .NET 6
Asked Answered
D

2

6

I have been trying to fix version conflicts in my .NET 6 console project. I fail to find a way like app.config's BindingRedirect or CodeBase, which worked for this case in .NET Framework.

Is there a BindingRedirect/CodeBase or an alternative in .NET 6? How do I fix assembly version problems?

Disputant answered 3/4, 2022 at 14:22 Comment(7)
I believe you can just add the app.config and add a binding redirect.Genitourinary
@Genitourinary OP "I believe you can just add the app.config and add a binding redirect" - OP has already indicated knowledge of "app.config's BindingRedirect or CodeBase whiuch were in .Net Framework"Kobi
@MickyD: yeah, so the app.config is default included in .net framework templates, but is missing in the .net core templates. Whilst OP indicates to have knowledge about the app.config, it's still ambiguous if OP knows he/she can just add the file.Genitourinary
@Genitourinary I'm pretty certain that's just going to get ignored. Binding redirects don't exist in .NET Core, it's a legacy .NET Framework thing.Enchain
@CharlesMager: than I was incorrect on that part - i.e.: I believed wrong XDGenitourinary
As I understand, app.config is not supported in .net core or laterDisputant
Looks like there is no solution in .net core /5/6: learn.microsoft.com/en-us/nuget/concepts/…Disputant
H
0

There is a way to do this at runtime. Add the following code to your Main or ConfigureServices (asp.net) or wherever, just make sure it's before the assembly is tried to be loaded from you application.

AppDomain.CurrentDomain.AssemblyResolve += (sender, args) => {
    if (args.Name.Contains("IdentityModel") && args.Name.Contains("PublicKeyToken=e7877f4675df049f"))
    {
        string assemblyPath = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "IdentityModel.dll");
        if (System.IO.File.Exists(assemblyPath))
        {
            return Assembly.LoadFrom(assemblyPath);
        }
    }
    return null;
};

At runtime args.Name contains this format: IdentityModel, Version=6.0.0.0, Culture=neutral, PublicKeyToken=e7877f4675df049f

Make sure the dll is present in the bin folder (or any other place you define yourself). Just adding the nuget PackageReference with specific version should be enough.

You can doublecheck the bin folder if the dll is the version you are expecting if you are experiencing issues with this solution.

Hick answered 11/9 at 8:8 Comment(0)
H
-6

To update the version in console application you can go to Explorer project files Project => properties => AssemblyInfo.cs

enter image description here

Heliacal answered 3/4, 2022 at 21:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.