To the frontpage...  
   

 

Lambda method caching

/// <summary>
/// I use minutes here.
/// But you could use hours or perhaps even seconds (http://css.dzone.com/articles/caching-fun-and-profit-or-why).
/// </summary>
public static T TimedWebCacheMethod<T>(string cacheKey, Func<T> f, int minutes)
{
    if (HttpContext.Current != null)
    {   //web context...
        if (HttpContext.Current.Cache[cacheKey] == null)
            HttpContext.Current.Cache.Add(cacheKey, f(), null, System.Web.Caching.Cache.NoAbsoluteExpiration, new TimeSpan(0, minutes, 0), System.Web.Caching.CacheItemPriority.Low, null);
        return (T)HttpContext.Current.Cache[cacheKey];
    }
    else return f();
}
 
...
 
public Stuff GetStuff(int stuffId)
{
    return CacheUtil.TimedWebCacheMethod<Stuff>("MyClass.GetStuff" + stuffId, () => StuffRepository.GetStuffById(stuffId), 1);
}

Guide to setting up AnkhSVN with passwordless ssh access to a remote repository.

AnkhSVN.
Assuming Visual Studio is already installed, you also need to install AnkhSVN.
In Visual Studio, go to Tools/Options/Source Control, and chose AnkhSVN in the select box.

You also need to install Cygwin.
In the installation wizard you need to go to the Net-package section, and select openssh (I also select openssl for good measure) for installation.

Setting up ssh.
Here is a guide on how to set up SSH Without Password.

Subversion
Now we need to configure Subversion to use an ssh tunnel.
For some reason, I had to go into Visual Studio, and sort of "activate" Subversion, before the config file was generated.
I just did that by going into View/Repository Explorer.

The Subversion config file is located here:
C:\Users\[UserName]\AppData\Roaming\Subversion\config

...open the file in your favorite editor and somewhere beneath this
### Section for configuring tunnel agents.
[tunnels]

...add this line:
ssh = C:\\cygwin\\bin\\ssh.exe

Back to the Studio...
In Visual Studio, right click your solution and select Add to Subversion:
Repository URL:
svn+ssh://reposi.tory.url/home/[user]/svn
I usually also add a trunk folder for the project with the project name.

Now it is all set up and your project should be under source control :-).

Image Resizer

The JPG-image bulk resizer (C# Console Application).

Select box with confirm

Okay, so I wanted to make a select box, which the user had to confirm his new selection. But! I could not find such a Webcontrol in ASP.NET, hence I made my own.
I added some javascript, to remember the initial setting of the select box to the html-code:
    <script language="javascript">
    var TempSelectId;
    </script>
And then I added some code behind. When the user clicks the select box, the javascript variable is updated and the initial setting stored.
When the setting is changed a confirm message pops up - if not confirmed, the setting is rolled back, and the javascript ends.
    string confirmText = "Are you sure?";
    DropDownList1.Attributes["onclick"] += "TempSelectId = document.getElementById('" + DropDownList1.ID + "').value;";
    DropDownList1.Attributes["onchange"] += "if (!confirm('" + confirmText + "')){ document.getElementById('" + DropDownList1.ID + "').value = TempSelectId;return;}";

Log4Net wrapper

I wasn't able to find any plug and play examples of how to set up a log4net logger so I had to make my own.
public class Logger
{
  private static Logger logger;
  private static readonly log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMeth od().DeclaringType);
 
  private Logger()
  {
    //find log file
    System.IO.FileInfo fileInfo = new System.IO.FileInfo(HttpContext.Current.Application["path"].ToString() + "\\log4net.config");
    log4net.Config.XmlConfigurator.ConfigureAndWatch(fileInfo);
  }
 
  /// <summary>
  /// Returns Logger
  /// </summary>
  /// <returns>Logger</returns>
  public static Logger GetLogger()
  {
    if (logger == null)
    {
      logger = new Logger();
    }
 
    return logger;
  }
 
  /// <summary>
  /// Returns log4net.ILog
  /// </summary>
  public log4net.ILog Log
  {
    get { return log; }
  }
}