How can I reference a dll in the GAC from Visual Studio?
Asked Answered
A

10

55

This assembly is in the GAC: Microsoft.SqlServer.Management.RegisteredServers.dll

How can I add a reference to this assembly in Visual Studio?

I can view the file in c:\windows\assembly\

Anteversion answered 14/1, 2009 at 21:1 Comment(0)
P
64

As the others said, most of the time you won't want to do that because it doesn't copy the assembly to your project and it won't deploy with your project. However, if you're like me, and trying to add a reference that all target machines have in their GAC but it's not a .NET Framework assembly:

  1. Open the windows Run dialog (Windows Key + r)
  2. Type C:\Windows\assembly\gac_msil. This is some sort of weird hack that lets you browse your GAC. You can only get to it through the run dialog. Hopefully my spreading this info doesn't eventually cause Microsoft to patch it and block it. (Too paranoid? :P)
  3. Find your assembly and copy its path from the address bar.
  4. Open the Add Reference dialog in Visual Studio and choose the Browse tab.
  5. Paste in the path to your GAC assembly.

I don't know if there's an easier way, but I haven't found it. I also frequently use step 1-3 to place .pdb files with their GAC assemblies to make sure they're not lost when I later need to use Remote Debugger.

Psychosurgery answered 16/9, 2010 at 15:57 Comment(9)
I was recently watching a Channel 9 video or something similar and briefly saw that the instructor's add reference window included a GAC option (don't remember exactly). I don't know if that was a homemade add-in or if there's one out there available. Has anyone seen this?Psychosurgery
"GAC_MSIL is not recognised as internal or external command" in Win7Graniteware
@Rakesh, it works in Win7. Type the full path as shown in the answer, not only gac_msilPalladic
May also need to search here: C:\Windows\Microsoft.NET\assemblyUpchurch
Yeah, no, it doesn't work, full path or otherwise, depending on what version of Windows you have so it's a useless general solution.Latticework
You may also wish to edit the project file and remove the HintPath.Perseus
On my Windows 10 I can simply paste the path into File Explorer hit Enter and the assemblies are displayed. E.g. pasted : C:\Windows\Microsoft.NET\assembly\gac_64 and all seems to work. One useful link is this.Hep
Thanks, @DanielDušek. This answer was from the Windows XP/.NET 2.0 days. XP had something called the Assembly Cache Viewer shell extension from shfusion.dll that provided a special (but restrictive) view of the directory. For whatever reason (which I'm sure makes perfect sense to someone more familiar with the shell), hitting the path from the run dialog bypassed it. I believe it was also possible to disable it in the registry. It became obsolete with .NET 4. This is a good walk down memory lane on the topic: codeproject.com/Articles/4352/…Psychosurgery
I see thanks, great article!Hep
F
20

Registering assmblies into the GAC does not then place a reference to the assembly in the add references dialog. You still need to reference the assembly by path for your project, the main difference being you do not need to use the copy local option, your app will find it at runtime.

In this particular case, you just need to reference your assembly by path (browse) or if you really want to have it in the add reference dialog there is a registry setting where you can add additional paths.

Note, if you ship your app to someone who does not have this assembly installed you will need to ship it, and in this case you really need to use the SharedManagementObjects.msi redistributable.

Forestay answered 14/1, 2009 at 21:54 Comment(2)
Your link is broken. It looks like ther eis now a NuGet package. But there's also still downloads of the MSI, like here.Perseus
This answer was posted 9 years ago ! - edited to remove the link, but honestly this question and answer are basically historical.Forestay
N
15

I've created a tool which is completely free, that will help you to achieve your goal. Muse VSReferences will allow you to add a Global Assembly Cache reference to the project from Add GAC Reference menu item.

Hope this helps Muse VSExtensions

Noach answered 29/4, 2010 at 8:7 Comment(2)
Is there any way to make it work with Visual Studio Express Edition?Grizel
@Fetchezlavache: I have unofficially modified the extension to support VS2012 and VS2013. It is available for download hereLemniscate
C
8

In VS2010, from the Add Rerences window you can click 'Browse' and navigate to C:\Windows\Assembly and add references to the assemblies that you want. Please note that the files may be grouped under different folders like GAC, GAC_32, GAC_64, GAC_MSIL etc.

Cutis answered 29/4, 2011 at 8:38 Comment(0)
Q
6

In VS, right click your project, select "Add Reference...", and you will see all the namespaces that exist in your GAC. Choose Microsoft.SqlServer.Management.RegisteredServers and click OK, and you should be good to go

EDIT:

That is the way you want to do this most of the time. However, after a bit of poking around I found this issue on MS Connect. MS says it is a known deployment issue, and they don't have a work around. The guy says if he copies the dll from the GAC folder and drops it in his bin, it works.

Quintile answered 14/1, 2009 at 21:11 Comment(2)
It is not there for some reason!Anteversion
On the page you linked they now (since 17-Nov-2010) mention that the issue has been fixed.Pink
S
4

The only way that worked for me, is by copying the dll into your desktop or something, add reference to it, then delete the dll from your desktop. Visual Studio will refresh itself, and will finally reference the dll from the GAC on itself.

Stoned answered 18/1, 2012 at 8:49 Comment(0)
B
3

Assuming you alredy tried to "Add Reference..." as explained above and did not succeed, you can have a look here. They say you have to meet some prerequisites: - .NET 3.5 SP1 - Windows Installer 4.5

EDIT: According to this post it is a known issue.

And this could be the solution you're looking for :)

Barnyard answered 14/1, 2009 at 21:14 Comment(0)
C
1

May be it's too late to answer, but i found a very simple way to do this(without a hack).

  1. Put your dll in GAC (for 3.5 Drag Drop inside "C:\Windows\assembly\")
  2. GoTo Projects --> Properties
  3. Click Reference Path (for 3.5 it's "C:\Windows\assembly\")
  4. and Build

Hope it helps

Curagh answered 19/6, 2015 at 14:59 Comment(0)
W
0

The relevant files and references can be found here:

http://msdn.microsoft.com/en-us/library/cc283981.aspx

Note the links off it about implementation/etc.

Wilks answered 13/1, 2011 at 14:17 Comment(0)
B
0

I found this extension for VS 2013 Vitevic GAC Reference.

Buckskin answered 14/8, 2015 at 9:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.