Cliente C# para Velocity
Hoy he estado liado con este tema con unas pruebas básicas para probar el Cache del AppFabric
El Windows Server AppFabric Beta 2 lo he instalado en un Windows Server 2008 con el Framework 4.0 RC, ojo que tiene que ser el RC no puede ser el final, además tiene otros requisitos con el Windows Power Shell 2.0 RTM y algunas cosas mas, pero el instalador te avisa y te dice de donde bajarlos.
El cliente en C# con VS2010 y Framework 4.0, recién salido del horno
Creamos el proyecto cliente y le añadimos esta referencias del AppFabric (Encontraremos las dll en la carpeta C:\Windows\System32\AppFabric del servidor donde lo hayamos instalado
- Microsoft.ApplicationServer.Caching.Client.dll
- Microsoft.ApplicationServer.Caching.Core.dll
- Microsoft.WindowsFabric.Common.dll
- Microsoft.WindowsFabric.Data.Common.dll
Podemos configurar el cliente por código o en el AppConfig.
Por código:
private DataCache Client { get { if (dataCache != null) return dataCache; // Aqui los servidores de cache en este caso 1. DataCacheServerEndpoint[] servers = new DataCacheServerEndpoint[1]; servers[0] = new DataCacheServerEndpoint("CACHESERVER", 22233); DataCacheFactoryConfiguration factoryConfig = new DataCacheFactoryConfiguration(); factoryConfig.Servers = servers; DataCacheFactory mycacheFactory = new DataCacheFactory(factoryConfig); //El Cache, en este caso el que pone por defecto al instalarlo DataCache myDefaultCache = mycacheFactory.GetCache("default"); dataCache = myDefaultCache; return dataCache; } }
Por configuración:
App.config:
<?xml version="1.0" encoding="utf-8" ?> <configuration> <!--configSections must be the FIRST element --> <configSections> <!-- required to read the <dataCacheClient> element --> <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="CACHESERVER" cachePort="22233"/> </hosts> </dataCacheClient> </configuration>
Codigo:
private DataCache Client { get { if (dataCache != null) return dataCache; //Configurado en el app.config DataCacheFactory mycacheFactory = new DataCacheFactory(); // Get a cache client for the cache "default". DataCache myDefaultCache = mycacheFactory.GetCache("default"); dataCache = myDefaultCache; return dataCache; } }
Y listo para probarlo:
private void Test() { DataCache cliente = Client; cliente.Add("key", "data"); object data = cliente.Get("key"); cliente.Remove("key"); }


