Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to replace a reference to a strongly-named assembly with a "weak" reference?

I'm writing a .NET tool that requires the SQL Server SMO library. I don't care if it's the version from Server 2005 (9.0), 2008 (10.0) or 2008 R2 (probably 10.5, didn't check). The SMO library is installed together SQL Server, so I can safely assume that on any system with SQL Server installed, some version of the SMO library is available as well.

Unfortunately, the SMO libraries are strongly-named: If I add a reference to SMO 9.0 in my project, it will fail (FileNotFoundException) if only SMO 10.0 is present on the customer's system, and vice versa.

Is there some way to tell the compiler that any version of the library is fine for me? Or do I really have to distribute 3 identical versions of my tool, each compiled to a different version of the SMO?


Disclaimer: I do know that the SMO libraries (and the libraries required by the SMO libraries) can be redistributed. But there's a big difference between (a) one slim 100KB standalone EXE and (b) a full-blown setup package that installs a whole bunch of prerequisites.

Disclaimer 2: I am aware of the following duplicates:

  • c# - can you make a “weak” assembly reference to a strong named assembly
  • Need a C# Assembly to reference a strongly named assembly loosely

The solutions provided do not fit, however. In question 1, the developer has control over the referenced DLL (which I do not); in question 2, the developer has control over the target systems (which I do not either).

like image 894
Heinzi Avatar asked Jul 29 '11 14:07

Heinzi


People also ask

What are assemblies explain briefly strong named and weak named assemblies?

If an assembly is not signed with the private/public key pair then the assembly is said to be a weak named assembly and it is not guaranteed to be unique and may cause the DLL hell problem. The Strong named assemblies are guaranteed to be unique and solve the DLL hell problem.

How is a strongly named assembly different from one that isn't strongly named?

When a strong-named assembly is created, it contains the simple text name of the assembly, the version number, optional culture information, a digital signature, and the public key that corresponds to the private key used for signing. Do not rely on strong names for security. They provide a unique identity only.

What makes a strong named assembly?

A strong name consists of the assembly's identity—its simple text name, version number, and culture information (if provided)—plus a public key and a digital signature. It is generated from an assembly file using the corresponding private key.

What is an assembly its type its use and what is a strong name is net?

It is basically compiled code that can be executed by the CLR. An Assembly is a basic building block of . Net Framework applications. It is basically a compiled code that can be executed by the CLR.

What is the difference between a strong and weak reference?

A reference to an instance is strong by default. But that is not always what you want. If a strong reference is not appropriate, you have a few other options, weak and unowned references. As the name suggests, a weak reference keeps a weak reference to the instance it references.

How do you get an object from a weak reference?

The object can be reached only by traversing a weak reference. First off, the Garbage Collector clears a weak reference, so the referent is no longer accessible. Then the reference is placed in a reference queue (if any associated exists) where we can obtain it from.

What does it mean when a weakly referenced object is cleared?

A weakly referenced object is cleared by the Garbage Collector when it's weakly reachable. Weak reachability means that an object has neither strong nor soft references pointing to it.

How to fix a weak reference in DLL?

This can be done by assigning Strong Name to the .DLL or assembly which is called as Strong reference. With use of Strong Name client will be able to identify original publisher and issue of weak reference will be resolved.


1 Answers

As I know it is not possible to remove the dependency on exact version. That is one of reasons why strong names exist - to avoid version mismatch. Internals or even public interfaces of the assembly can change among version and you can find that new version is not backward compatible with the old one. Because of that .NET looks for version used during compilation to make sure that application works correctly.

If third party decides that their new version is backward compatible and if they deploy assembly to GAC they can add publisher policy which will do redirect automatically.

If you decide that you want to force loading another assembly you can use the approach mentioned by @chibacity or implement handler for AppDomain.CurrentDomain.AssemblyResolve. This event fires when .NET is not able to find referenced assembly and you can implement your own logic to find it and load it by calling Assembly.LoadFrom. In such case it is completely up to you which version you load.

like image 190
Ladislav Mrnka Avatar answered Nov 08 '22 16:11

Ladislav Mrnka