Two sub library projects in one solution use same dll but different version

Priyank Ghorecha 1 Reputation point
2022-01-03T13:37:33.943+00:00

161838-libraryissuedescription.png

I have a solution with multiple projects. There is a need that I have to refer to a different version of the same assembly in two different projects.

When building the projects, one DLL is overwriting the other DLL, because they are both being compiled in the same folder. So the DLL that depends on the older version fails with an error.

Could not load file or assembly XXXX or one of its dependencies. The located assembly's manifest definition does not match the assembly reference.  

In my research, I have found that we can load DLL from a different location using the tag in the app config file of the project.

Approach 1: In Project B, I have added the following line of code in the "app.config" file.

<runtime>  
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">  
  <dependentAssembly>  
    <assemblyIdentity name="SampleExample"  
                      publicKeyToken="XXXXXXXXXXXXXX"  
                      culture="neutral" />  
    <codeBase version="2.0.8.0"  
              href="FILE://C:/Users/username/source/repos/Solution/Project A/bin/Debug/DLL/SampleExample.dll"/>  
  </dependentAssembly>  
</assemblyBinding>  

Using the above approach I am still facing the same error and the project is not able to load DLL with the desired version.

Approach 2: In Project A, I have added the following line of code in the "app.config" file.

<runtime>  
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">  
      <dependentAssembly>  
        <assemblyIdentity name="SampleExample"  
                          publicKeyToken="XXXXXXXXXXXXXX"  
                          culture="neutral" />  
        <codeBase version="2.0.8.0"  
                  href="FILE://C:/Users/username/source/repos/Solution/Project A/bin/Debug/DLL/SampleExample.dll"/>  
      </dependentAssembly>  
    </assemblyBinding>  
  </runtime>  

Here in Project A, when performing the above changes I am able to execute the project successfully without any error.

I want to know, is there any way we can use Approach 1 and the application works correctly? I also want to know updating the same line of code in the Project A application was working correctly without any error so will that affect Project C as I am not sure Approach 2 will have any impact on Project C behavior or functionality?

Here Is there any other approach we can use to resolve the issue, where we can use the same DLL with a different version in Project B and Project C?

Developer technologies | .NET | .NET Runtime
Developer technologies | C#
{count} votes

3 answers

Sort by: Most helpful
  1. Ken Tucker 5,861 Reputation points
    2022-01-03T18:47:54.057+00:00

    You can use a binding redirect in the web or app config to tell both apps to use the same version of the dll. So if Project C uses version 1 of the dll and Project B uses version 2 you can use a binding redirect so both use version 2.

    https://learn.microsoft.com/en-us/dotnet/framework/configure-apps/redirect-assembly-versions

    0 comments No comments

  2. Jiachen Li-MSFT 34,221 Reputation points Microsoft External Staff
    2022-01-04T02:11:34.803+00:00

    Hi, @Priyank Ghorecha ,
    You can try the following two methods to reference different versions of a same dll for different projects of the same solution.

    1. Add the ***.exe.config configuration file corresponding to the running program exe in the client, and configure it according to the picture.
      It’s important to note that you must write all the versions you rely on.
      162017-2022-1-4.png

    2.If you reference different versions of DLLs in the same project, but the new version of the DLL is compatible with the old version of the DLL, you can use Redirecting assembly versions

    <runtime>  
        <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">  
          <dependentAssembly>  
            <assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" />  
            <bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0" />  
          </dependentAssembly>  
        </assemblyBinding>  
      </runtime>  
    

    Hope the above method can help you.
    Best Regards.
    Jiachen Li


    If the answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

  3. radhakrishna Anala 0 Reputation points
    2023-03-03T13:32:27.8266667+00:00

    Hi , i am having 1 dll with different versions and i am unable to use 2 different version dlls in 2 different projects with in same solution. Please let me know how i use 1 version in all the projects. I am getting dll from nuget packages. I am mainly seeing to place nugget package in one place and share the same dll in all the projects with in the same solution. The issue is we can't upload the dlls in bitbucket. Pls help me on this.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.