2012-12-21 25 views
0

我有一臺計算機(X1),其中在VS 2010中構建的Web應用程序運行並且Appfabric在此應用程序中安裝和使用。我有另一臺計算機(X2),其中也安裝了Appfabric。兩臺電腦都連接在一個工作組中。從另一臺機器訪問Appfabric緩存

例如,目前我在X1中創建了一個「NamedCache1」緩存,並使用它來存儲值並在X1中從我的應用中引用它。我在X2中創建了一個緩存「NamedCache2」,並且我想在我的應用中使用這個緩存「NamedCache2」。我怎樣才能獲得這個?

回答

0

這個問題有點含糊不清,你在配置AppFabric的位置。我假設您已經完成了Google結果中的一個步驟,以配置AppFabric緩存,例如the one by Scott Hanselman。如果您在該鏈接查看他的CacheUtil類,則可以修改它以訪問兩個緩存。

using Microsoft.ApplicationServer.Caching; 
using System.Collections.Generic; 

public class CacheUtil 
{ 
    private static DataCacheFactory _factory = null; 
    private static DataCache _NamedCache1 = null; 
    private static DataCache _NamedCache2 = null; 

    public static DataCache GetNamedCache1() 
    { 
     if (_NamedCache1 != null) 
      return _NamedCache1; 

     //Define Array for 2 Cache Hosts 
     List<DataCacheServerEndpoint> servers = new List<DataCacheServerEndpoint>(2); 

     //Specify Cache Host Details 
     // Parameter 1 = host name 
     // Parameter 2 = cache port number 
     servers.Add(new DataCacheServerEndpoint("X1", 22233)); 
     servers.Add(new DataCacheServerEndpoint("X2", 22233)); 

     //Create cache configuration 
     DataCacheFactoryConfiguration configuration = new DataCacheFactoryConfiguration(); 

     //Set the cache host(s) 
     configuration.Servers = servers; 

     //Set default properties for local cache (local cache disabled) 
     configuration.LocalCacheProperties = new DataCacheLocalCacheProperties(); 

     //Disable tracing to avoid informational/verbose messages on the web page 
     DataCacheClientLogManager.ChangeLogLevel(System.Diagnostics.TraceLevel.Off); 

     //Pass configuration settings to cacheFactory constructor 
     _factory = new DataCacheFactory(configuration); 

     //Get reference to named cache called "default" 
     _NamedCache1 = _factory.GetCache("NamedCache1"); 

     return _NamedCache1; 
    } 

    public static DataCache GetNamedCache2() 
    { 
     if (_NamedCache2 != null) 
      return _NamedCache2; 

     //Define Array for 2 Cache Hosts 
     List<DataCacheServerEndpoint> servers = new List<DataCacheServerEndpoint>(2); 

     //Specify Cache Host Details 
     // Parameter 1 = host name 
     // Parameter 2 = cache port number 
     servers.Add(new DataCacheServerEndpoint("X1", 22233)); 
     servers.Add(new DataCacheServerEndpoint("X2", 22233)); 

     //Create cache configuration 
     DataCacheFactoryConfiguration configuration = new DataCacheFactoryConfiguration(); 

     //Set the cache host(s) 
     configuration.Servers = servers; 

     //Set default properties for local cache (local cache disabled) 
     configuration.LocalCacheProperties = new DataCacheLocalCacheProperties(); 

     //Disable tracing to avoid informational/verbose messages on the web page 
     DataCacheClientLogManager.ChangeLogLevel(System.Diagnostics.TraceLevel.Off); 

     //Pass configuration settings to cacheFactory constructor 
     _factory = new DataCacheFactory(configuration); 

     //Get reference to named cache called "default" 
     _NamedCache2 = _factory.GetCache("NamedCache2"); 

     return _NamedCache2; 
    } 
} 
+0

感謝您的回覆。是的,我在安裝過程中關注了Scott Hanselman的文章。我在X1的應用程序中應用了您的代碼。 GetNamedCache1()函數工作正常,但GetNamedCache2()在此行生成錯誤_NamedCache2 = _factory.GetCache(「NamedCache2」); – user1921583

+0

由於某些原因,NamedCache2無法從X1訪問。 – user1921583

+0

X1和X2是否配置爲同一緩存集羣的一部分?如果是這樣,請嘗試在兩臺機器上的AppFabric Powershell中運行Get-Cache,以確保NamedCache1和NamedCache2都在兩者上。如果不是,並且它們是單獨的羣集,則將_factory設置爲2個變量,每個獲取緩存方法一個。然後刪除或註釋在GetNamedCache1的GetNamedCache2和X2中添加服務器X1的行。或者,你可以讓兩臺機器成爲同一個緩存集羣的一部分。 – smlync

相關問題