Visual C#을 사용하여 전역 어셈블리 캐시에 어셈블리 설치

이 문서에서는 Visual C#의 전역 어셈블리 캐시에 어셈블리를 설치하는 방법에 대한 정보를 제공합니다.

원래 제품 버전: Visual C#
원래 KB 번호: 815808

요약

이 문서에서는 어셈블리의 강력한 이름을 생성하는 방법과 GAC(전역 어셈블리 캐시)에 .dll 파일을 설치하는 방법을 설명합니다. GAC를 사용하면 여러 애플리케이션에서 어셈블리를 공유할 수 있습니다. GAC는 .NET 런타임과 함께 자동으로 설치됩니다. 구성 요소는 일반적으로 에 C:\WINNT\Assembly저장됩니다.

GAC에 어셈블리를 설치하려면 어셈블리에 강력한 이름을 지정해야 합니다. 이름은 암호화 해시 키 또는 서명입니다. 이 강력한 이름은 올바른 구성 요소 버전 관리를 보장합니다. 이렇게 하면 이름이 같은 구성 요소가 서로 충돌하거나 소비하는 애플리케이션에서 잘못 사용되지 않도록 방지할 수 있습니다.

요구 사항

  • 공유 어셈블리가 설치되는 컴퓨터에 대한 관리자 권한
  • .NET의 어셈블리에 대한 일반적인 친숙함.
  • 명령 프롬프트에서 도구 사용에 대해 일반적으로 잘 알고 있습니다.

전역 어셈블리 캐시

Visual Studio를 사용하여 작은 클래스 라이브러리 프로젝트를 만들고 강력한 이름을 생성하고 GAC에 프로젝트의 .dll 파일을 설치하려면 다음 단계를 수행합니다.

  1. Visual Studio에서 새 Visual C# 클래스 라이브러리 프로젝트를 만들고 프로젝트의 이름을 GACDemo로 지정합니다.

  2. 강력한 이름을 사용해야 합니다. 이 암호화 키 쌍을 생성하려면 강력한 이름 도구(Sn.exe)를 사용합니다. 이 도구는 \bin .NET Framework SDK(솔루션 개발자 키트)가 설치된 하위 디렉터리에 있습니다. Sn.exe 도구를 사용하기 쉽습니다. 명령줄 문은 다음을 사용합니다.

    sn -k "[DriveLetter]:\[DirectoryToPlaceKey]\[KeyName].snk"
    

    참고

    Visual Studio에서 IDE 프로젝트 속성을 사용하여 키 쌍을 생성하고 어셈블리에 서명할 수 있습니다. 그런 다음 3단계와 4단계를 건너뛰고 AssemblyInfo.cs 파일에 대한 코드 변경도 건너뛸 수 있습니다.

    IDE 프로젝트 속성을 사용하여 키 쌍을 생성하고 어셈블리에 서명하려면 다음 단계를 수행합니다.

    1. 솔루션 탐색기 GACDemo를 마우스 오른쪽 단추로 클릭한 다음 속성을 클릭합니다.

    2. 서명 탭을 클릭한 다음 을 클릭하여 어셈블리 검사 서명 상자를 선택합니다.

    3. 강력한 이름 키 선택 목록에서 새로 만들기...>를 클릭합니다<.

    4. 키 파일 이름으로 GACkey.snk를 입력하고 암호 검사 사용하여 내 키 파일 보호 상자를 선택 취소한 다음 확인을 클릭합니다.

    5. Ctrl+Shift+B 바로 가기 키를 눌러 프로젝트를 컴파일합니다.

    이러한 단계를 수행한 후에도 5단계를 따라 GAC에 어셈블리를 설치해야 합니다.

  3. 키를 쉽게 찾고 명령 프롬프트에서 키에 액세스할 수 있도록 에서 C:\GACKey라는 디렉터리를 만듭니다.

    대부분의 사용자의 경우 .NET 도구는 에 있습니다 C:\Program Files\Microsoft.NET\FrameworkSDK\Bin. 다음 명령을 입력하기 전에 컴퓨터에서 이와 유사한 경로를 .NET bin 디렉터리에 복사할 수 있습니다. 명령 프롬프트에 를 입력 cd 하고 마우스 오른쪽 단추를 클릭하여 경로를 붙여넣은 다음 Enter 키를 눌러 SN 도구가 있는 디렉터리로 빠르게 변경합니다.

    다음 명령을 입력합니다.

    sn -k "C:\GACKey\GACkey.snk"
    
  4. 키가 생성되었지만 아직 프로젝트의 어셈블리와 연결되지 않았습니다. 이 연결을 만들려면 Visual Studio .NET 솔루션 탐색기 AssemblyInfo.cs 파일을 두 번 클릭합니다. 이 파일에는 Visual Studio .NET에서 프로젝트를 만들 때 기본적으로 포함되는 어셈블리 특성 목록이 있습니다. AssemblyKeyFile 다음과 같이 코드에서 어셈블리 특성을 수정합니다.

    [assembly: AssemblyKeyFile('C:\\GACKey\\GACKey.snk') ]
    

    Ctrl+Shift+B를 눌러 프로젝트를 컴파일합니다. GAC에 .dll 파일을 설치하기 위한 추가 코드가 있을 필요는 없습니다.

  5. Gacutil 도구를 사용하거나 .dll 파일을 적절한 폴더로 끌어 .dll 파일을 설치할 수 있습니다. Gacutil 도구를 사용하는 경우 다음과 유사한 명령을 사용할 수 있습니다.

    gacutil -I "[DriveLetter]:\[PathToBinDirectoryInVSProject]\gac.dll"
    

    파일을 끌어다 놓려면 Windows Explorer 두 인스턴스를 엽니다. 한 instance 콘솔 프로젝트에 대한 .dll 파일 출력의 위치를 찾습니다. 다른 instance 을 찾습니다c:\<SystemRoot>\Assembly. 그런 다음 .dll 파일을 Assembly 폴더로 끌어옵니다.

전체 코드 목록(AssemblyInfo.cs)

using System.Reflection;
using System.Runtime.CompilerServices;

// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// that is associated with an assembly.
[assembly: AssemblyTitle("")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("")]
[assembly: AssemblyCopyright("")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

// Version information for an assembly is made up of the following four values:
// Major Version
// Minor Version
// Build Number
// Revision
// You can specify all the values, or you can default the revision and build numbers
// by using the '*' as shown below:
[assembly: AssemblyVersion("1.0.*")]
// To sign your assembly you must specify a key to use. See the
// Microsoft .NET Framework documentation for more information about assembly signing.
// Use the following attributes to control that key is used for signing.
// Notes:
//     (*) If no key is specified, the assembly is not signed.
//     (*) KeyName refers to a key that has been installed in the Crypto Service
//         Provider (CSP) on your computer. KeyFile refers to a file that contains
//         a key.
//     (*) If the KeyFile and the KeyName values are both specified, the
//         following processing occurs:
//         (1) If the KeyName can be found in the CSP, that key is used.
//         (2) If the KeyName does not exist and the KeyFile does exist, the key
//             in the KeyFile is installed to the CSP and used.
//     (*) To create a KeyFile, you can use the sn.exe (Strong Name) utility.
//         When specifying the KeyFile, the location of the KeyFile must be
//         relative to the project output directory which is
//         %Project Directory%\obj\<configuration>. For example, if your KeyFile is
//         located in the project directory, you would specify the AssemblyKeyFile
//         attribute as [assembly: AssemblyKeyFile("..\\..\\mykey.snk")]
//     (*) Delay Signing is an advanced option - see the Microsoft .NET Framework
//         documentation for more information about this.
[assembly: AssemblyDelaySign(false)]
[assembly: AssemblyKeyFile("C:\\GACKey\\GACKey.snk")]
[assembly: AssemblyKeyName("")]

확인

  1. Windows 탐색기를시작합니다.
  2. 를 찾습니다 C:\SystemRoot\assembly.
  3. 설치된 .dll 파일 목록에 GACDemo 가 표시됩니다.

참조