Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Assembly has a strong name, but I"m getting the error that says a strong name is needed

I am trying to load a third party COM dll into my application. Everything builds fine but when I run the application I keep getting this message from my application:

Could not load file or assembly '"assembly", Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. A strongly-named assembly is required. (Exception from HRESULT: 0x80131044).

When I do a sn -vf "assembly" it says the assembly is valid. Has anyone seen this type of behavior before?

like image 984
Mykal73 Avatar asked Aug 21 '15 20:08

Mykal73


People also ask

How do you give a strong name to assembly?

In Solution Explorer, open the shortcut menu for the project, and then choose Properties. Under the Build tab you'll find a Strong naming node. Select the Sign the assembly checkbox, which expands the options. Select the Browse button to choose a Strong name key file path.

How do you fix referenced assembly does not have a strong name error?

The solution was to install StrongNamer from NuGet, which automatically adds a strong name to all referenced assemblies. Just simply having it referenced in the project fixed my issue.

How do you tell if an assembly is strongly named?

To determine if an assembly is strong-typed, use the Strong Name Tool from Microsoft (http://msdn.microsoft.com/en-us/library/k5b5tt23(v=vs.71).aspx) by running the 'sn.exe -v <assembly>' command. You may need to download one of the Windows SDK packages to get access to this tool.

How do I disable strong name validation?

You can turn off strong name validation for an assembly by using the sn.exe utility that ships with the framework. This is helpful if you want to add an assembly to the GAC that is delay signed.


2 Answers

The assembly you are trying to load does not have a strong name. This can be seen by your message, it says PublicKeyToken=null. If it had a strong name, it would have a public key token.

If you have given it a strong name after you compiled or referenced it, try to reference it again in your project. Maybe your project has still the old reference and is trying to load an unsigned version.

like image 66
nvoigt Avatar answered Oct 06 '22 01:10

nvoigt


I have seen the behavior before when working with the Mongo CSharp Driver. From Version 1.10.0 and up they stopped providing strongly named assemblies, so you had to sign them yourself.

When I signed the 3 dll's provided; MongoDB.Bson, MongoDB.Driver, and MongoDB.Driver.Core, I neglected the built in dependency structure of those assemblies. MongoDB.Driver was dependent on MongoDB.Driver.Core which was dependent on MongoDB.Bson. This meant although my code was referencing the signed assemblies the pre-compiled assemblies were referencing the signed assemblies they were dependent on.

In general you'll observe this behaviour when you have a dependency tree such as this

Assembly1 -------> Assembly2
   |                   |
   |---> Assembly3 <---|

Where both assemblies 1 & 2 are dependent on assembly 3 but assembly 1 is also dependent on assembly 2. Its one step short of a circular dependency which makes it very rare.

I discuss the process and SDK tools available for signing a 3rd party DLL and resolving this issue in part 4 of the 5 part series. .NetFU also had a good article on the process but their page has since been removed.

like image 44
TheDude Avatar answered Oct 06 '22 00:10

TheDude