Posts

using(TricksToFormatYourCodeNicely)

I've been writing a data importer which takes a specific data input format and outputs XML, this XML is then imported within my application. What annoyed me was the way in which the source code was formatted.... writer.WriteStartElement("data"); writer.WriteAttributeString("1", "1"); writer.WriteAttributeString("2", "2"); writer.WriteAttributeString("3", "3"); writer.WriteStartElement("systemData"); writer.WriteAttributeString("a", "a"); writer.WriteAttributeString("b", "b"); writer.WriteEndElement();//systemData writer.WriteEndElement();//data It just didn't look nice. I thought about splitting it into separate methods, but most of the time this would have been overkill as the methods would have been very short. Instead I wrote an extension method on XmlWriter: public static class XmlWriterHelper {   public static IDisposable StartElement(this XmlWriter wr

Parameterised queries in ECO

Whenever I generate OCL queries in code I find myself having to escape user input in order to avoid making the query invalid, or allowing malicious input. I've decided instead to use the ECO equivalent of parameterised queries (variables in ECO) and here is the result. public static string CreateParameterisedQuery(   this IEcoServiceProvider serviceProvider,   string query,   out IModifiableVariableList vars,   params object[] args) {   vars = serviceProvider.GetEcoService<IVariableFactoryService>().CreateVariableList();   for (int varIndex = 0; varIndex < args.Length; varIndex++)   {     string variableName = "autoVar_" + varIndex.ToString();     query = query.Replace("{" + varIndex.ToString() + "}", variableName);     vars.AddConstant(variableName, args[varIndex]);   }   return query; } To use this code you would do something like this //1: Create the OCL with string.format style parameters string query = "Person.allInstances" +   

User authentication in SilverLight

I wanted to know how to authenticate users in a SilverLight app using their Windows login info. 01: Set the authentication mode to Windows and <deny users="?"/> in <system.web> within web.config 02: Move the silverlight control to Default.aspx and set that as your start page 03: Add the following Page_Load code protected void Page_Load(object sender, EventArgs e) { IPrincipal p = HttpContext.Current.User; if (p == null) throw new SecurityException("No current user"); if (!(p is WindowsPrincipal)) throw new SecurityException("Not a windows user"); if (!p.Identity.IsAuthenticated) throw new SecurityException("Not authenticated"); Xaml1.InitParameters = string.Format("user={0},session={1}", p.Identity.Name, Session.SessionID); } 04: In app.xaml.cs you can now read the InitParameters using e.InitParameters in the Application_Startup method.

No more free disk space - Vista

My computer was recently running out of disk space. I couldn't believe it, I have 175GB on my partition, how could I fill it so quickly? I was considering buying a 500GB external drive to store my home videos etc on (which is where I assumed all the space was being taken) and earlier today I nearly went out and bought one too! I just decided to check where all that space was being taken up. Here are the folders on C and their sizes in GB apps 2.76 data 40.7 msocache 0.7 otherdata 0.8 program files 9.16 program data 0.9 users 1.64 windows 13.8 That's a total of 70.46GB, but Windows was reporting 173GB in use! I ran scan disk etc, no luck! Where was all the space being used? The answer was System Restore! After reading this article I realised my restore data was using 90GB of data! What a waste! I have now limited it to 10GB maximum. Can you believe that? It's ridiculous! Luckily I have just saved myself £70!

The contract name could not be found in the list of contracts implemented by the service

When trying to add a service reference to my SilverLight project I kept getting this error message The contract name xxxxxxx could not be found in the list of contracts implemented by the service I looked around the web and couldn't find anything of any use (yes, I had added ServiceContract to my interface). The solution was really simple! In my Web.Config I had the wrong interface defined. IApplicationService was defined in another assembly, one I had added a namespace to, so I additionally needed to add the namespace before the interface name. All I then had to do was to add the following attribute to the service implementing the interface and it seemed to import fine. [AspNetCompatibilityRequirements(RequirementsMode=AspNetCompatibilityRequirementsMode.Allowed)]

Validating NumericUpDown on compact framework

A customer requested that instead of my NumericUpDown controls silently capping the input value within the Minimum..Maximum range it instead showed an error message telling the user their input is incorrect and that they need to alter it. I was a bit annoyed to see that NumericUpDown.Validating is never called on the compact framework, in addition there was no way to get the input value and either accept or reject it before it is applied to its data bindings. There's an article here which shows how to implement auto-select text when the NumericUpDown receives focus and I have been using it since Feb 2006. I decided to extend upon the techniques within it to implement the Validating event. My goal was to fire the Validating event before the value is applied to all data-bindings, but also to allow the programmer to read NumericUpDown.Value in order to determine the new value. To do this I had to replace the WndProc of the control so that I could handle the WM_UPDOWN_NOTIFYVALUECH

Single instance application

An app I am working on needs to be a single instance. It is associated with certain file extensions so that when I select a character or license file it will be imported automatically. When the user buys a character or license (etc) from the website it will be downloaded and opened, and then imported. Obviously it is a pretty poor user experience if they have to close the app, download, close the app, download... So what I really needed was a way to have the 2nd instance of the application to invoke the first instance and pass the command line parameters. Here is a simple solution I implemented using remoting. 01: An interface public interface ISingleInstance { void Execute(string[] args); } 02: A class that implements the interface public class SingleInstance : MarshalByRefObject, ISingleInstance {   private static object SyncRoot = new object();   private static Form1 MainForm;   static SingleInstance()   {     Application.EnableVisualStyles();     Application.SetCompatibleText