Friday 28 May 2010

AddThis validation issue

The AddThis button is a great addition to any website, however if your site needs to conform to W3C standards you may need to tweak the default HTML snippet that is provided, the issue is the use of the # in one of the JavaScript references:


<script type="text/javascript" src="http://s7.addthis.com/js/250/addthis_widget.js#username=blahblah">
</script>


Simply change the # to a ? and you should be validating fine:


<script type="text/javascript" src="http://s7.addthis.com/js/250/addthis_widget.js?username=blahblah"></script>

Monday 17 May 2010

Linq equivalent of for-each

If you've ever wondered about using Linq for a more elegant implementation of a for each loop then this may be useful, use the Select<> method to perform actions against a set of items, as long as you return a value of any sort then you can perform any action during the selection.

In the example below I need to remove all folders that were written to over an hour ago, it also returns info on the folders processed and whether they were successfully removed:


if (string.IsNullOrEmpty(RootWorkingFolder) || !Directory.Exists(RootWorkingFolder)) return;

Directory.GetDirectories(RootWorkingFolder)
.Where(f => Directory.GetLastWriteTime(f) < DateTime.Now.AddHours(-1))
.Select(delegate(string f)
{
try
{
Directory.Delete(f, true);
return new {Folder = f, Removed = true};
}
catch (IOException)
{
return new {Folder = f, Removed = false};
}
}).ToList();


The ToList method is to ensure that the query is executed immediately.

Pretty useful to know :-)

Wednesday 5 May 2010

Visual Studio 2010 running slow

For anyone suffering with the performance of Visual Studio 2010, particularly intellisense, Microsoft has published a support article on the matter, apparently an update to Windows Automation API 3.0 can be applied that will improve matters.

The article is here: http://support.microsoft.com/kb/981741/.

Thursday 29 April 2010

Assigning click event using jQuery in FireFox not working

I've just stumbled upon a strange problem which highlighted some badly formatted Javascript on my part, I had around 30 anchors on my page which needed to have Click event handlers assigned in order for me to override the href invocation, the reason for this was to prevent my Click from propagating through the underlying DIV element that the anchor was sitting in, so here was my original code:


$(document).ready(function() {
$(".addthis_button_facebook, .addthis_button_twitter").click(function() {
return click_addthis(event, this.href)
});
});


Which actually worked fine in every browser except FireFox, this was stumping me for ages until I decided to post a question at the wonderful Stack Overflow (what a god-send that place is!), which resulted in someone immediately spotting the missing argument in my function event.

The code should've been:


$(document).ready(function() {
$(".addthis_button_facebook, .addthis_button_twitter").click(function(e) {
return click_addthis(e, this.href)
});
});


If you haven't noticed it, it is the argument in the function that is required, this can then be passed onto the function being called.

I hope this helps someone who may have a similar issue/oversight, although it's probably just me and my limited JS experience!

So thanks again to the helpful chaps at stack overflow!

Thursday 20 August 2009

Configuration files in GAC assemblies

We had an interesting problem recently where-by we were rolling out integration libraries and installing them into the GAC on our windows servers.

The issue was that the only way to have the dlls respect configuration files was to copy the configuraiton file into the physical GAC path of the assembly, which can prove a bit tricky.

In the end we opted to use a slightly bespoke solution of packaging the configuration file in with the DLL and write a procedure for extracting that, this way we could ensure a single DLL file and have multiple configuration files for each environment.

To achieve this:

1. You need to create your App.config file and set the Build Action to Embedded Resource, this ensures that the config file is embedded into the resulting DLL.

2. Here's a snippet of the code to read the configuration and create a usable Congiruation instance (we ended up wrapping this up into a class, see link at the bottom of the article):


///
/// Ensures configuration exists.
///

public Configuration GetConfiguration()
{
var filename = EnsureConfigFile();
var map = new ExeConfigurationFileMap
{
ExeConfigFilename = filename
};

if (File.Exists(filename))
{
return ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.None);
}

return null;
}
///
/// Ensures that the configuration file is available to be read.
///

/// The configuration filename.
private string EnsureConfigFile()
{
var standardConfigFile = string.Empty;
if (StandardConfigFileExists(out standardConfigFile))
{
return standardConfigFile;
}

var assembly = ConfigurationFileAssembly ?? Assembly.GetCallingAssembly();
var configFileName = string.Concat(assembly.GetName().Name, ".config");
var filename = Path.Combine(Path.GetTempPath(), assembly.GetName().FullName);

if (!Directory.Exists(filename))
Directory.CreateDirectory(filename);

filename = Path.Combine(filename, configFileName);

var info = new FileInfo(filename);
if (!info.Exists || info.LastWriteTime.Date <= DateTime.Now.AddDays(1) || (Debugger.IsAttached))
{
// Be aware that this assembly.GetManifestResourceStream is case sensitive so you need an "App.config" file.
var resource = string.Format("{0}.App.config", assembly.GetName().Name);
var stream = assembly.GetManifestResourceStream(resource);
if (stream != null)
{
var reader = new StreamReader(stream);
File.WriteAllText(filename, reader.ReadToEnd());
}
else
{
throw new Exception("Unable to access App.config as an embedded resource.");
}
}
return filename;
}


Now that you have an App.config file that is an embedded resource you can replace it when you do your builds with your environment-specific config file before you build your project:



Now, there is one handicap with this and that's that you can't update the configuration file once it has been built and compiled, but this isn't such a bad thing as it enforces you to follow a correct build/release process when deploying the assemblies.

ServiceConfiguration.cs