I have a dll
in C:\Windows\Microsoft.NET\assembly\GAC_32\MyFolder\
that I want to use in my C#
project.
I tried to add reference(References->Add Reference->Browse) to it and it seems fine but during runtime the program crushed with an AccessViolationException
.
I wasn't sure if the reference was the problem so I decided to look into it and I found an answer in this post saying that you can't reference an assembly from the GAC
this way. What I didn't find is how should I do it.
When I remove the reference to the dll
the program isn't compiling of course.
-
please add your code, it's almost impossible to help suck a question just out of nowhere – No Idea For Name Nov 23 '14 at 08:33
-
Why do you need the code? I'm askig a technical question. I have a `dll` in the `GAC`, how do I use it? I don't expect people to figure out why I'm getting an exception, just tell me if I reference the `dll` like I should. – GM6 Nov 23 '14 at 08:40
-
1@GM6 - how did you add it to the GAC ? Did you use GacUtil ? Exactly what command parameters did you use ? When you say "at runtime" do you mean in the VS.net IDE or when executing on a different machine ? How do you know the DLL is properly registered in the GAC ? Have you used GacUtil to list the GAC contents on the target machine ? – PhillipH Nov 23 '14 at 08:47
-
there can be many reasons for the exception. we don't know why it happens just because u said so. the only thing i can do from what you told me is saying go search your wanted solution in google. but we want to help, not necessarily the exact answer you want to hear, but the answer your program needs. that's why we insist on code, that's why you have so low view count, and that's why i'm asking you for code – No Idea For Name Nov 23 '14 at 08:47
-
@PhillipH all good questions but I'm afraid I have very few answers. I installed an `msi` file which among other things place that dll there so I can't tell you how it was done becuase I don't know. – GM6 Nov 23 '14 at 09:02
-
@NoIdeaForName, it looks like we are not understanding each other. Forget the code, pretend I have a clean project(which is not far from the truth) and I want to use an assembly from the GAC. Do I just reference it like a regualr `dll`(Browse tab) or is there a different way(like they said in the link I gave). If you insist I can tell you that the program crushes when I create a new instance of one of the classes defined in the dll, but again that's not the issue. It's completley possible that the problem is elsewhere, I just want to rule out the reference option. – GM6 Nov 23 '14 at 09:02
-
@PhillipH: I'm not the right person to be adressed to. I just changed a tag. – rkhb Nov 23 '14 at 16:26
-
it should be presented in your Add Reference dialog box - you should not need to file browse for it. But before you do that, run the GacUtil tool I mentioned to make sure that it is definitely in the Gac and properly registered. – PhillipH Nov 23 '14 at 16:28
-
@GM6 Have you not found any of the provided answers helpful? – Ondrej Janacek Dec 24 '14 at 10:02
2 Answers
There are some things that you need to understand before working with GAC assemblies.
1) Assembly in GAC has to be strong-named.
2) You can't just copy the file there, here's how to deploy an assembly to GAC properly.
3) A strong-named assembly can only use types from other strong-named assemblies.
4) Here's how to reference a strong name assembly during compile-time (using a compiler option) and run-time and here's how to reference a GAC assembly from Visual Studio. Read also the answer from Hans Passant in this thread.

- 1
- 1

- 12,486
- 14
- 59
- 93
Possible duplicate of 'Referencing DLL from GAC in Visual Studio'. Check out especially this answer that explains why adding a reference from GAC is bad practice. However you can still do that using the full path on your machine. Consider the following scenario:
- your reference is a third party dll
- You rely on the fact that machines you are deploying to already have that dll registered
Resolution:
- Copy 3rd party assemblies you depend on to some folder of choice.
- Use those references just for compilation purposes. You are not shipping those items.
- Upon installation those references will be resolved from the GAC on that machine. If not, you need to install those assemblies to GAC.
This scenario is used by those who build custom apps for SharePoint (for instance).

- 1
- 1

- 2,895
- 1
- 19
- 29