C # use multiple versions of the same library (socket)

I have a situation like this:

1 Solution containing 3 projects:

  • 1 console app that links to these two other elasticity related projects.
  • 1 elix search v1 project containing the Nest and ElasticSearch.Net v1 libraries for searching our old elastic cluster 1.4.
  • 1 Elastic Search v5 project which contains the Nest and ElasticSearch.Net v5 libraries to perform searches on our new elastic cluster 5.4

The Nest library is 1 Nuget package and you must be using the correct version for your elastic cluster. It uses the internal ElasticSearch.Net library. Also Newtonsoft.Json conflicts between these library versions.

Most of the answers to questions like this are related to assembly redirection, but in this case it is not possible because the versions are not compatible.

I tried to use Fody / Costura ( https://github.com/Fody/Costura ) to embed DLL related libraries in my v1 and v5 libraries to avoid conflicts, Embedding myself is fine so that these libraries are not copied into bin folder.

I have not tried ILMerge yet, but I understand that it is similar to Costura.

public void Test1()
{
    var es1Helper = new MyElasticSearchV1RelatedProject.SearchHelper();
        es1Helper.TestSearch();

    var es5Helper = new MyElasticSearchV5RelatedProject.SearchHelper();
        es5Helper.TestSearch();
}

public void Test2()
{
    var es5Helper = new MyElasticSearchV5RelatedProject.SearchHelper();
    es5Helper.TestSearch();

    var es1Helper = new MyElasticSearchV1RelatedProject.SearchHelper();
    es1Helper.TestSearch();
}

      

But Test1 doesn't work

System.TypeLoadException: 'Could not load type' Nest.Indices 'from assembly' Nest, Version = 1.0.0.0, Culture = neutral, PublicKeyToken = 96c599bbe3e70f5d '.'

And Test2 doesn't work

System.TypeLoadException: 'Failed to load type' Elasticsearch.Net.ConnectionPool.IConnectionPool 'from assembly' Elasticsearch.Net, Version = 5.0.0.0, Culture = neutral, PublicKeyToken = 96c599bbe3e70f5d '.'

It looks like depending on the order other dll versions are being loaded and another library project is trying to use them as well.

Can both versions be used in the same project?

+3


source to share


1 answer


You might be in luck with using a little known C # feature called extern alias

https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/extern-alias

This will allow you to reference two DLLs with the same full type name, creating additional root-level namespaces so you can refer to types such as: nestv1::ConnectionSettings

and nestv2::ConnectionSettings

. Similar to how you can refer to regular types through a namespace alias global::

.



https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/namespaces/how-to-use-the-global-namespace-alias

NuGet doesn't expose this, so you'll need to create the link manually.

+2


source







All Articles