So this issue has been driving us nuts for almost 2 weeks, Azure updated its NuGet package for Caching, and anyone who recently updated it but still had projects running SDK 2.0 will most likely miss this highlighted section:

This is the error message we kept seeing repeatedly in the cloud as well as the emulator:
<ES0006>:There is a temporary failure. Please retry later. (One or more specified cache servers are unavailable, which could be caused by busy network or servers. For on-premises cache clusters, also verify the following conditions. Ensure that security permission has been granted for this client account, and check that the AppFabric Caching Service is allowed through the firewall on all cache hosts. Also the MaxBufferSize on the server must be greater than or equal to the serialized object size sent from the client.)
This fix? Simply update your azure SDK to >2.0
I hope this helps anyone having this same issue!
For JSONP calls we should always restrict and validate the callback parameter to prevent code injections and other hacker attacks.
To do this we usually set a max size and only allow alphanumeric characters and underscores.
Most developers would turn to regex but I prefer to do things regex-less, in C# we can validate the string callback as seen in this code snippet below using some LINQ magic:
if (!callback.ToCharArray().All(c => Char.IsLetter(c) || Char.IsNumber(c) || c == '_')))
return "illegal callback, can only contain alphanumeric characters and underscores";
In order to implement server-side google analytics tracking into our API for request monitoring we decided to give this library a try:
https://github.com/maartenba/GoogleAnalyticsTracker
In order to get this example below to work inside the Global.asax file
Tracker tracker = new Tracker("UA-XXXXXX-XX", "www.example.org");
tracker.TrackPageView(HttpContext, "My API - Create");
I needed to somehow cast HttpContext.Current into the abstract class HttpContextBase.
Turns out it is as simple as:
new HttpContextWrapper(HttpContext.Current)
so the example becomes:
Tracker tracker = new Tracker("UA-XXXXXX-XX", "www.example.org");
tracker.TrackPageView(new HttpContextWrapper(HttpContext.Current), "My API - Create");
So recently I need to unit test some js code but this code used the window.location.host variable, and in order to fully test the logic I had to mock the window.location global var.
In case anyone ever has to do something similar here is a snippet that works with QUnit:
test( "test initial mocks", function()
{
custom_window =
{
location:
{
host: "test.asu.edu"
}
};
(function(window)
{
equal( window.location.host, "test.asu.edu", "we expect test.asu.edu to be in the window.host mock" );
})(custom_window);
});
I recently needed to do some QUnit testing on some code that interacted with google analytics in javascript.
Specifically, firing some standard ga logic that read a custom var (slot 4) using this:
_gat._getTrackerByName()._getVisitorCustomVar(4);
In order to not load ga.js in the test I decided to mock these methods so that the code could be tested in QUnit, the following code will set the custom var to the string ‘asu’ so it can be read later in the code using the normal custom var method:
var customvar4 = undefined;
function _tracker()
{
this._getVisitorCustomVar=_getVisitorCustomVar;
function _getVisitorCustomVar(i)
{
return customvar4;
}
}
// test the initial mocks
test( "test initial mocks", function()
{
// setup ga mocks
custom_gat =
{
_getTrackerByName: function()
{
return new _tracker();
}
}
customvar4 = 'asu';
function _tracker()
{
this._getVisitorCustomVar=_getVisitorCustomVar;
function _getVisitorCustomVar(i)
{
return customvar4;
}
}
(function(_gat)
{
equal( _gat._getTrackerByName()._getVisitorCustomVar(4), "asu", "we expect asu to be in the ga mock" );
})(custom_gat);
});
Obviously this can be expanding on by adding more of the ga methods but this should help you get started. Also you will notice I ignore the slot number, add a switch to the method if you need specific slot data.
It is way easier that in previous versions of TFS, simply create a .tfignore file.
For example:
######################################
# Ignore .cpp files in the ProjA sub-folder and all its subfolders
ProjA\*.cpp
#
# Ignore .txt files in this folder
\*.txt
#
# Ignore .xml files in this folder and all its sub-folders
*.xml
#
# Ignore all files in the Temp sub-folder
\Temp
#
# Do not ignore .dll files in this folder nor in any of its sub-folders
!*.dll
We needed to determine if a class was running in Azure and if so whether it was actually in the cloud or being emulated locally.
Turns out this is very simple to do, just add a reference to:
Microsoft.WindowsAzure.ServiceRuntime
And then use these two static booleans, notice the try/catch blocks, 32 bit processes throw an error here, so reporting false in the catch remedies that.
public static bool InAzureEnvironment
{
get
{
try
{
return RoleEnvironment.IsAvailable;
}
catch { return false; }
}
}
public static bool InCloud
{
get
{
try
{
return InAzureEnvironment && !RoleEnvironment.IsEmulated;
}
catch { return false; }
}
}
Using the .net ProtectedData.Protect method is great way to secure in memory data.
However if it isn’t obvious and you try to compile but receive this message
The name ‘ProtectedData’ does not exist in the current context, and The name ‘DataProtectionScope’ does not exist in the current context
The fix is you need to manually add the System.Security as a reference.
I have used secure string before but now I needed to protect other types of objects in memory, what is the best way to do this?
Start with the following example, taken from here:
using System;
using System.Security.Cryptography;
public class DataProtectionSample
{
// Create byte array for additional entropy when using Protect method.
static byte [] s_aditionalEntropy = { 9, 8, 7, 6, 5 };
public static void Main()
{
// Create a simple byte array containing data to be encrypted.
byte [] secret = { 0, 1, 2, 3, 4, 1, 2, 3, 4 };
//Encrypt the data.
byte [] encryptedSecret = Protect( secret );
Console.WriteLine("The encrypted byte array is:");
PrintValues(encryptedSecret);
// Decrypt the data and store in a byte array.
byte [] originalData = Unprotect( encryptedSecret );
Console.WriteLine("{0}The original data is:", Environment.NewLine);
PrintValues(originalData);
}
public static byte [] Protect( byte [] data )
{
try
{
// Encrypt the data using DataProtectionScope.CurrentUser. The result can be decrypted
// only by the same current user.
return ProtectedData.Protect( data, s_aditionalEntropy, DataProtectionScope.CurrentUser );
}
catch (CryptographicException e)
{
Console.WriteLine("Data was not encrypted. An error occurred.");
Console.WriteLine(e.ToString());
return null;
}
}
public static byte [] Unprotect( byte [] data )
{
try
{
//Decrypt the data using DataProtectionScope.CurrentUser.
return ProtectedData.Unprotect( data, s_aditionalEntropy, DataProtectionScope.CurrentUser );
}
catch (CryptographicException e)
{
Console.WriteLine("Data was not decrypted. An error occurred.");
Console.WriteLine(e.ToString());
return null;
}
}
public static void PrintValues( Byte[] myArr )
{
foreach ( Byte i in myArr )
{
Console.Write( "\t{0}", i );
}
Console.WriteLine();
}
}
Use these methods in this example to protect objects held in memory, all .net objects can be represented with a byte array some with native methods to serialize and deserialize them. You could then use public get and set methods to retrieve and store the data.
The following shows how we can store a guid in a class using protected data:
public Guid secureGUID
{
get
{
return new Guid(Unprotect(_secureGUID));
}
set
{
_secureGUID = Protect(value.ToByteArray());
}
}
private byte[] _secureGUID;
The other thing you will want to make sure you do is run the Array.Clear on dispose of the class, to ensure the array data has been overwritten with zeros in memory.