다음을 통해 공유


방법: Azure In-Role Cache의 RoleEntryPoint 메서드에서 DataCache 개체 만들기

중요

모든 새 개발에서는 Azure Redis Cache를 사용하는 것이 좋습니다. Azure Cache 제품을 선택하는 방법에 대한 현재 설명서 및 지침 은 나에게 적합한 Azure Cache 제품을 참조하세요.

web.config 파일의 Microsoft Azure 캐시 설정은 RoleEntryPoint 클래스(일반적으로 WebRole.cs 파일에 정의됨)의 컨텍스트에서 자동으로 사용할 수 없습니다. RoleEntryPoint 클래스 메서드에서 DataCache 개체를 만들려고 하면 다음 오류가 발생합니다.

{"ErrorCode<ERRCA0021>:SubStatus<ES0001>:Server collection cannot be empty."}

이 문제는 web.config 파일의 dataCacheClient 설정이 새 DataCache 클라이언트 개체에 자동으로 적용되지 않기 때문에 발생합니다. 특히 자동 검색 요소의 특성을 사용할 수 없으므로 DataCache 개체가 캐싱을 호스트하는 역할을 대상으로 지정할 수 없습니다.

이 시나리오에서는 web.config 파일의 설정에 직접 액세스하는 DataCacheFactoryConfiguration 개체를 프로그래밍 방식으로 구성해야 합니다.

캐시 클라이언트를 프로그래밍 방식으로 구성하는 방법

  1. DataCacheFactoryConfiguration 개체를 만듭니다.

    DataCacheFactoryConfiguration config = new DataCacheFactoryConfiguration();
    
  2. AutoDiscoveryProperty를 새 DataCacheAutoDiscoverProperty 개체로 설정합니다. 자동 검색을 사용하도록 true를 지정합니다. 또한 캐싱을 호스트하는 역할의 이름을 지정합니다.

    config.AutoDiscoverProperty = new DataCacheAutoDiscoverProperty(true, "WebRole1");
    
  3. 캐시 클라이언트의 동작을 제어하는 DataCacheFactoryConfiguration 개체에 다른 속성을 설정합니다.

  4. 생성자의 DataCacheFactoryConfiguration 개체를 전달하여 DataCacheFactory 개체를 만듭니다.

    DataCacheFactory factory = new DataCacheFactory(config);
    
  5. DataCacheFactory.GetCache 메서드를 호출하여 대상 명명된 캐시에 대한 DataCache 개체를 반환합니다.

    DataCache cache = factory.GetCache("default");
    

예제

// Create a DataCacheFactoryConfiguration object
DataCacheFactoryConfiguration config = new DataCacheFactoryConfiguration();

// Enable the AutoDiscorveryProperty (and any other required configuration settings):
config.AutoDiscoverProperty = new DataCacheAutoDiscoverProperty(true, "WebRole1");

// Create a DataCacheFactory object with the configuration settings:
DataCacheFactory factory = new DataCacheFactory(config);

// Use the factory to create a DataCache client for the "default" cache:
DataCache cache = factory.GetCache("default");

참고 항목

개념

Azure 역할 내 캐시 개발 시작