強式名稱組件Strong-named assemblies

強式命名組件會為組件建立唯一識別,並可防止發生組件衝突。Strong-naming an assembly creates a unique identity for the assembly, and can prevent assembly conflicts.

構成強式名稱的組件之項目為何?What makes a strong-named assembly?

強式名稱的組件是透過私密金鑰所產生,該私密金鑰對應至隨組件或當做組件本身散發的公開金鑰。A strong named assembly is generated by using the private key that corresponds to the public key distributed with the assembly, and the assembly itself. 該組件包含組件資訊清單,其中包含構成組件之所有檔案的名稱和雜湊。The assembly includes the assembly manifest, which contains the names and hashes of all the files that make up the assembly. 具有相同強式名稱的組件應該是相同的。Assemblies that have the same strong name should be identical.

您可以使用 Visual Studio 或命令列工具強式命名組件。You can strong-name assemblies by using Visual Studio or a command-line tool. 如需詳細資訊,請參閱如何:使用強式名稱簽署元件Sn.exe (強式名稱工具)For more information, see How to: Sign an assembly with a strong name or Sn.exe (Strong Name tool).

建立強式名稱的組件時,會包含組件的簡單文字名稱、版本號碼、文化特性資訊 (選擇性)、數位簽章,以及對應至用於簽署之私密金鑰的公開金鑰。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.

為什麼需要強式命名您的組件?Why strong-name your assemblies?

針對 .NET Framework,強式名稱的元件在下列案例中很有用:For .NET Framework, strong-named assemblies are useful in the following scenarios:

  • 您想要啟用組件以供強式名稱的組件參考,或者您想要授與 friend 存取權,以從其他強式名稱的組件存取您的組件。You want to enable your assemblies to be referenced by strong-named assemblies, or you want to give friend access to your assemblies from other strong-named assemblies.

  • 應用程式需要存取相同組件的不同版本。An app needs access to different versions of the same assembly. 這表示您需要在不起衝突的情況下,在同一個應用程式網域中並存載入組件的不同版本。This means you need different versions of an assembly to load side by side in the same app domain without conflict. 例如,如果組件中存在具有相同簡單名稱的不同應用程式開發介面擴充模組,強式命名會為每個組件版本提供唯一識別。For example, if different extensions of an API exist in assemblies that have the same simple name, strong-naming provides a unique identity for each version of the assembly.

  • 您不希望在使用組件時,對應用程式效能造成負面影響,因此想要使用領域中性的組件。You do not want to negatively affect performance of apps using your assembly, so you want the assembly to be domain neutral. 由於領域中性的組件必須安裝在全域組件快取中,因此需要對其強式命名。This requires strong-naming because a domain-neutral assembly must be installed in the global assembly cache.

  • 您想要藉由套用發行者原則來為您的應用程式集中化服務,這表示元件必須安裝在全域組件快取中。You want to centralize servicing for your app by applying publisher policy, which means the assembly must be installed in the global assembly cache.

針對 .NET Core,強式名稱的元件不提供材質優勢。For .NET Core, strong-named assemblies do not provide material benefits.

如果您是開放原始碼開發人員,而且想要強名稱元件的身分識別優點,以更符合 .NET Framework 的相容性,請考慮將與元件相關聯的私密金鑰簽入您的原始檔控制系統。If you are an open-source developer and you want the identity benefits of a strong-named assembly for better compatibility with .NET Framework, consider checking in the private key associated with an assembly to your source control system.

另請參閱See also