在GAC中添加程序集的好处 [英] Benefit of adding assembly in GAC

查看:80
本文介绍了在GAC中添加程序集的好处的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在寻找在GAC中添加程序集的好处。

我创建了一个程序集,假设'myGac'然后签名并使用GacUtil将其添加到GAC。在这里,我使用.Net 4.0,所以我的程序集现在位于'C:\ Windows \ Microsoft \\ .NET \assembly \ GAC_MSIL'文件夹中。现在我希望myGac可以在visual studio 2010的添加引用对话框中找到,因为我将myGac位置添加到'HKEY_LOCAL_MACHINE \ SOFTWARE \ Microsoft \ .NetFramework \ Version = \\AssemblyFoldersEx \'。现在我创建一个控制台应用程序并使用添加引用对话框添加myGac的引用,它运行良好。

我很困惑为什么我做了所有这些事情(签署程序集,添加到GAC,修改regedit),即使我可以从本地驱动器添加引用意味着本地程序集。好的msdn和所有其他教程说它可以在同一台机器上运行的许多应用程序共享。

1.它是如何分享的?

2.如果我试图制作这个控制台应用程序的一个安装文件myGac出现在应用程序文件夹中,这也发生在本地程序集中。这个myGac与本地组装有何不同。

请不要发布任何链接,我已经搜索了大部分链接。我在这里寻找一些简短的描述或示例,如果有的话。

感谢U

I have been searching for benefit of adding assembly in GAC.
I have created an assembly suppose 'myGac' then sign it and added it to GAC using GacUtil. Here i am using .Net 4.0 so my assembly is now in 'C:\Windows\Microsoft.NET\assembly\GAC_MSIL' folder. Now I wanted myGac to be available at 'add reference' dialog box of visual studio 2010 for that I add myGac location to 'HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\.NetFramework\Version}\AssemblyFoldersEx\'. Now i create one console application and add the reference of myGac using 'add reference' dialog box, it works well.
I am confused why i did all this things like (sign an assembly, add it to GAC, modify regedit), even i can add reference from local drive mean a local assembly. Ok msdn and all other tutorial say it will be shareable for many application run in same machine.
1. How it is shareable??
2. If i try to make one setup file of this console application the myGac appears in 'Application Folder' this thing happen with local assembly also. How this myGac is different from local assembly.
Please do not post any link, i already have searched most of them. I m here looking for some brief description or example, if any one have.
Thank U

推荐答案

经验法则是:如果你有一个可以被多个产品引用的组件,2)使用相同组件的多个产品是可选的,你可以将一些组件放到GAC中受益;一些可能安装或不安装。



此外,安装在GAC中的程序集由其强名称引用。在使用它们的项目中引用它们应该从不涉及使用其可执行模块的任何路径名(PE文件)。这就是GAC程序集与本地程序集不同的方式。由Gacutil.exe添加: http://msdn.microsoft .com / zh-CN / library / aa309379%28v = vs.71%29.aspx [ ^ ]。



参见:

http://en.wikipedia.org/wiki/Global_Assembly_Cache [ ^ ],

http://msdn.microsoft.com/en-us/library/yf1d93sz%28v=vs.110%29.aspx [ ^ ],

http: //msdn.microsoft.com/en-us/library/6axd4fx6%28v=vs.110%29。 aspx [ ^ ]。



您还应该考虑它们的缺点。它们可能污染目标系统。想象一下,你有两个应用程序,productA和productB,都使用程序集myLibrary。您可以按任何顺序独立安装它们,第一个产品安装也应该安装程序集myLibrary。现在,您卸载productA(不一定与安装顺序相同)。由于productB仍在使用,myLibrary应保留在GAC中。现在,您需要安装productB。您应该删除myLibrary,以防止GAC被未使用的组件污染。但如何检测是否还有更多产品仍在使用myLibrary?您必须关心安装中的所有产品。



我,例如,有时使用替代本地方法,使用应用程序配置。我将我的产品使用一些程序集myLibrary放在单独的子目录中,并将myLibrary放在一些子目录中使用应用程序配置文件中规定的相对路径的所有此类产品(例如,某些应用程序myProduct.exe将需要名为myProject.exe.config的配置文件。)此类配置文件可能如下所示:

A rule of thumb would be: you can benefit from putting some assembly to GAC, if you 1) have an assembly which can be referenced by more than one product, 2) multiple products using the same assembly are optional; some may be installed or not.

Also, assemblies installed in GAC are referenced by its strong names. Referencing them in projects using them should never involve using any path names of their executable modules (PE files). This is how GAC assemblies are different from local. The are added by the Gacutil.exe: http://msdn.microsoft.com/en-us/library/aa309379%28v=vs.71%29.aspx[^].

See also:
http://en.wikipedia.org/wiki/Global_Assembly_Cache[^],
http://msdn.microsoft.com/en-us/library/yf1d93sz%28v=vs.110%29.aspx[^],
http://msdn.microsoft.com/en-us/library/6axd4fx6%28v=vs.110%29.aspx[^].

You should also consider downside of them. They may contaminate the target system. Imagine you have two applications, "productA" and "productB", both using the assembly "myLibrary". You can install them independently in any order, and the first of product installation should also install the assembly "myLibrary". Now, you uninstall "productA" (not necessarily in the same order as installation). As "productB" is still used, "myLibrary" should remain in GAC. Now, you need to install "productB". You should remove "myLibrary', to prevent GAC contamination with unused assemblies. But how to detect if there are no more products still using "myLibrary"? You have to care about all that in your installations.

I, for example, sometimes use an alternative local approach, using application configuration. I put my products using some assembly "myLibrary" in separate sub-directories, and "myLibrary" in some sub-directory shared with all such products using relative path prescribed in application config files (say, some application "myProduct.exe" will need a configuration file named "myProject.exe.config"). Such configuration file can look like this:
<configuration>
    <runtime>
        <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
            <probing privatePath=".\mySharedLibraries\myLibrary"/>
        </assemblyBinding>
    </runtime>
</configuration>





-SA


这篇关于在GAC中添加程序集的好处的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆