locked
Azure AppFabric Caching using 3.5 .NET RRS feed

  • Question

  • Hi

     

    I'm trying to get the Azure AppFabric caching to work using .Net 3.5. Is this possible? For various reasons, I can't yet more to 4.0 of the framework. I';ve seen various posts that state that this should be possible. However, there are no builds in the SDK for 3.5 (of Caching.Client.dll for example).

    BTW, it works fine in 4.0 - but that breaks other parts of my application. 

     

    cc

     


    Tuesday, August 23, 2011 1:27 PM

Answers

  • Hi Christopher,

    From the call stack, it seems that you are using the provided output cache provider or session state provider. Please note that these providers are built in Microsoft.Web.DistributedCache assembly which requires .NET 4.0. So if your project is .NET 3.5, you cannot use these providers.

    You can write code to consume the cache service or build your own providers. Here is a simple sample code:

    // Declare array for cache host.
    DataCacheServerEndpoint[] servers = new DataCacheServerEndpoint[1];
    servers[0] = new DataCacheServerEndpoint("[your namespace].cache.windows.net", 22233);
    
    // Setup DataCacheSecurity configuration.
    string strACSKey = "your secure key";
    var secureACSKey = new SecureString();
    foreach (char a in strACSKey)
    {
      secureACSKey.AppendChar(a);
    }
    secureACSKey.MakeReadOnly();
    DataCacheSecurity factorySecurity = new DataCacheSecurity(secureACSKey);
    
    // Setup the DataCacheFactory configuration.
    DataCacheFactoryConfiguration factoryConfig = new DataCacheFactoryConfiguration();
    factoryConfig.Servers = servers;
    factoryConfig.SecurityProperties = factorySecurity;
    
    // Create a configured DataCacheFactory object.
    DataCacheFactory cacheFactory = new DataCacheFactory(factoryConfig);
    
    // Get a cache client for the default cache.
    DataCache defaultCache = cacheFactory.GetDefaultCache();
    
    // Add and retrieve a test object from the default cache.
    defaultCache.Put("testkey", "Hello");
    string strObject = (string)defaultCache.Get("testkey");
    

    Thanks.


    Wenchao Zeng
    Please mark the replies as answers if they help or unmark if not.
    If you have any feedback about my replies, please contact msdnmg@microsoft.com.
    Microsoft One Code Framework
    Thursday, August 25, 2011 5:45 AM

All replies

  • Hi Christopher,

    Windows Azure AppFabric Caching supports .NET Framework 3.5 or higher (non-client profile). However, the packaged providers (output cache provider and session state provider) support .NET framework 4.0 only.

    You can find these DLL's in [Windows Azure AppFabric SDK]\V1.0\Assemblies\NET4.0\Cache directory. Only Microsoft.Web.DistributedCache.dll is built against .NET 4.0 runtime. Other DLL's are built against .NET 2.0 runtime. .NET Framework 3.5 makes use of .NET 2.0 runtime.

    Thanks.


    Wenchao Zeng
    Please mark the replies as answers if they help or unmark if not.
    If you have any feedback about my replies, please contact msdnmg@microsoft.com.
    Microsoft One Code Framework
    Wednesday, August 24, 2011 8:30 AM
  • Wenchao,

     

    If I don't reference (in my 3.5 projects) the distributed cache dll, I get the error below. 

    Do the others not depend on this dll?

    WRN: Assembly binding logging is turned OFF.
    To enable assembly bind failure logging, set the registry value [HKLM\Software\Microsoft\Fusion!EnableLog] (DWORD) to 1.
    Note: There is some performance penalty associated with assembly bind failure logging.
    To turn this feature off, remove the registry value [HKLM\Software\Microsoft\Fusion!EnableLog].
    


    Stack Trace: 

    [BadImageFormatException: Could not load file or assembly 'Microsoft.Web.DistributedCache' or one of its dependencies. This assembly is built by a runtime newer than the currently loaded runtime and cannot be loaded.]
      System.Reflection.Assembly._nLoad(AssemblyName fileName, String codeBase, Evidence assemblySecurity, Assembly locationHint, StackCrawlMark& stackMark, Boolean throwOnFileNotFound, Boolean forIntrospection) +0
      System.Reflection.Assembly.InternalLoad(AssemblyName assemblyRef, Evidence assemblySecurity, StackCrawlMark& stackMark, Boolean forIntrospection) +416
      System.Reflection.Assembly.InternalLoad(String assemblyString, Evidence assemblySecurity, StackCrawlMark& stackMark, Boolean forIntrospection) +166
      System.Reflection.Assembly.Load(String assemblyString) +35
      System.Web.Configuration.CompilationSection.LoadAssemblyHelper(String assemblyName, Boolean starDirective) +190
    
    [ConfigurationErrorsException: Could not load file or assembly 'Microsoft.Web.DistributedCache' or one of its dependencies. This assembly is built by a runtime newer than the currently loaded runtime and cannot be loaded.]
      System.Web.Configuration.CompilationSection.LoadAssemblyHelper(String assemblyName, Boolean starDirective) +11211672
      System.Web.Configuration.CompilationSection.LoadAllAssembliesFromAppDomainBinDirectory() +388
      System.Web.Configuration.CompilationSection.LoadAssembly(AssemblyInfo ai) +232
      System.Web.Configuration.AssemblyInfo.get_AssemblyInternal() +48
      System.Web.Compilation.BuildManager.GetReferencedAssemblies(CompilationSection compConfig) +210
      System.Web.Compilation.BuildProvidersCompiler..ctor(VirtualPath configPath, Boolean supportLocalization, String outputAssemblyName) +76
      System.Web.Compilation.CodeDirectoryCompiler.GetCodeDirectoryAssembly(VirtualPath virtualDir, CodeDirectoryType dirType, String assemblyName, StringSet excludedSubdirectories, Boolean isDirectoryAllowed) +11200850
      System.Web.Compilation.BuildManager.CompileCodeDirectory(VirtualPath virtualDir, CodeDirectoryType dirType, String assemblyName, StringSet excludedSubdirectories) +185
      System.Web.Compilation.BuildManager.EnsureTopLevelFilesCompiled() +551
    
    [HttpException (0x80004005): Could not load file or assembly 'Microsoft.Web.DistributedCache' or one of its dependencies. This assembly is built by a runtime newer than the currently loaded runtime and cannot be loaded.]
      System.Web.Compilation.BuildManager.ReportTopLevelCompilationException() +76
      System.Web.Compilation.BuildManager.EnsureTopLevelFilesCompiled() +1012
      System.Web.Hosting.HostingEnvironment.Initialize(ApplicationManager appManager, IApplicationHost appHost, IConfigMapPathFactory configMapPathFactory, HostingEnvironmentParameters hostingParameters) +1025
    
    [HttpException (0x80004005): Could not load file or assembly 'Microsoft.Web.DistributedCache' or one of its dependencies. This assembly is built by a runtime newer than the currently loaded runtime and cannot be loaded.]
      System.Web.HttpRuntime.FirstRequestInit(HttpContext context) +11305670
      System.Web.HttpRuntime.EnsureFirstRequestInit(HttpContext context) +88
      System.Web.HttpRuntime.ProcessRequestNotificationPrivate(IIS7WorkerRequest wr, HttpContext context) +4342340
    Wednesday, August 24, 2011 7:46 PM
  • Hi Christopher,

    From the call stack, it seems that you are using the provided output cache provider or session state provider. Please note that these providers are built in Microsoft.Web.DistributedCache assembly which requires .NET 4.0. So if your project is .NET 3.5, you cannot use these providers.

    You can write code to consume the cache service or build your own providers. Here is a simple sample code:

    // Declare array for cache host.
    DataCacheServerEndpoint[] servers = new DataCacheServerEndpoint[1];
    servers[0] = new DataCacheServerEndpoint("[your namespace].cache.windows.net", 22233);
    
    // Setup DataCacheSecurity configuration.
    string strACSKey = "your secure key";
    var secureACSKey = new SecureString();
    foreach (char a in strACSKey)
    {
      secureACSKey.AppendChar(a);
    }
    secureACSKey.MakeReadOnly();
    DataCacheSecurity factorySecurity = new DataCacheSecurity(secureACSKey);
    
    // Setup the DataCacheFactory configuration.
    DataCacheFactoryConfiguration factoryConfig = new DataCacheFactoryConfiguration();
    factoryConfig.Servers = servers;
    factoryConfig.SecurityProperties = factorySecurity;
    
    // Create a configured DataCacheFactory object.
    DataCacheFactory cacheFactory = new DataCacheFactory(factoryConfig);
    
    // Get a cache client for the default cache.
    DataCache defaultCache = cacheFactory.GetDefaultCache();
    
    // Add and retrieve a test object from the default cache.
    defaultCache.Put("testkey", "Hello");
    string strObject = (string)defaultCache.Get("testkey");
    

    Thanks.


    Wenchao Zeng
    Please mark the replies as answers if they help or unmark if not.
    If you have any feedback about my replies, please contact msdnmg@microsoft.com.
    Microsoft One Code Framework
    Thursday, August 25, 2011 5:45 AM