2012-07-26 65 views
0

我一直在嘗試編寫一個簡單的小Cmdlet來允許我設置/獲取/刪除緩存項目。我遇到的問題是我無法弄清楚如何連接到本地緩存集羣。AppFabric Cmdlet - 無法連接到本地集羣

我嘗試添加在平時的app.config的東西,但似乎並沒有得到回升...

<?xml version="1.0" encoding="utf-8" ?> 
<configuration> 
    <configSections> 
    <section name="dataCacheClient" type="Microsoft.ApplicationServer.Caching.DataCacheClientSection, Microsoft.ApplicationServer.Caching.Core, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" allowLocation="true" allowDefinition="Everywhere" /> 
    </configSections> 
    <dataCacheClient> 
    <hosts> 
     <host name="localhost" cachePort="22233" /> 
    </hosts> 
    </dataCacheClient> 
</configuration> 

我寧願沒有這方面的配置都沒有。那麼,我真的問的是等效的C#代碼是什麼以下PowerShell的...

Use-CacheCluster 

從我可以收集Use-CacheCluster連接到本地集羣如果沒有提供參數

回答

1

我已經只是使用Reflector對AppFabric Powershell代碼進行了一些探討,以瞭解它如何在封面下工作。如果您撥打Use-CacheCluster而不帶任何參數,例如對於本地羣集,代碼將從註冊表項HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\AppFabric\V1.0\Configuration中讀取連接字符串和提供程序名稱。不幸的是,它然後使用這些值來構建一系列類(ClusterConfigElement,CacheAdminClusterHandler),這些類都被標記爲內部類,因此您不能使用它們來獲取當前集羣上下文(因爲需要更好的單詞) Powershell正在與之合作。

爲了使您的Cmdlet能夠正常工作,我認爲您需要傳遞一個主機名(這將是羣集中的服務器之一,也許您可​​以將其默認爲本地計算機名稱)和一個端口號(您可以將其默認爲22233),然後使用這些值構建一個DataCacheServerEndpoint以傳遞給您的DataCacheFactory

[Cmdlet(VerbsCommon.Set,"Value")] 
public class SetValueCommand : Cmdlet 
{ 
    [Parameter] 
    public string Hostname { get; set; } 
    [Parameter] 
    public int PortNumber { get; set; } 
    [Parameter(Mandatory = true)] 
    public string CacheName { get; set; } 

    protected override void ProcessRecord() 
    { 
     base.ProcessRecord(); 

     // Read the incoming parameters and default to the local machine and port 22233 
     string host = string.IsNullOrWhiteSpace(Hostname) ? Environment.MachineName : Hostname; 
     int port = PortNumber == 0 ? 22233 : PortNumber; 

     // Create an endpoint based on the parameters 
     DataCacheServerEndpoint endpoint = new DataCacheServerEndpoint(host, port); 

     // Create a config using the endpoint 
     DataCacheFactoryConfiguration config = new DataCacheFactoryConfiguration(); 
     config.Servers = new List<DataCacheServerEndpoint> { endpoint }; 

     // Create a factory using the config 
     DataCacheFactory factory = new DataCacheFactory(config); 

     // Get a reference to the cache so we can now start doing useful work... 
     DataCache cache = factory.GetCache(CacheName); 
     ... 
    } 
} 
+0

我在反射器中找到了相同的代碼。我希望避免必須傳遞配置。我遇到的問題是,我希望它能夠在我碰巧運行它的任何集羣中「工作」。我有一個本地緩存用於開發,但我的生產緩存運行在3個虛擬機上。我想這是我們可以期待的最好的,直到微軟發佈一個「.Server」nuget包供我們使用。 – 2012-07-26 12:32:25

+0

@AntonyScott FWIW我認爲這是MS的一個糟糕的設計決定,意味着我們需要在我們自己的Cmdlet中複製所有這些工作,而不是以PowerShell-y的方式工作 – PhilPursglove 2012-07-26 12:53:52

+0

同意。一個.NET程序集會更好,那麼我們可以用它來編寫cmdlet。嘗試使用cmdlet進行反向工程並不好:S – 2012-07-26 12:57:37

0

的問題是,該呼叫: DataCacheFactoryConfiguration配置=新DataCacheFactoryConfiguration();

在Cmdlet mothods中產生一個聽起來像「無法初始化DataCacheFactoryConfiguration」的錯誤。