ASP.NET MVC String Editor Template which handles the StringLength attribute
Interestingly enough I was searching for a way for MVC to automatically handle the stringlength property. Seemed quite straightforward to implement, but somehow there is no available code out there to figure this out. Using the Html.EditorFor just creates a no frills textbox, which is rather irritating.
Logic walkthrough
Basically what the template does is to attempt to find the StringLength attribute from the ModelMetadata, and from there get the validation parameters.
Once it finds the appropriate parameter (in this case max), it will assign this to a string dictionary.
Finally all is wrapped up with a call to Html.Textbox
Usage
to use this ‘custom’ editor template, what you need to do will be
@Html.EditorFor(model => model.strcolumn)
or if you want to specify the width as well
@Html.EditorFor(model => model.strcolumn, new { width = “300px” })
NOTE: This works if you specify Text as the datatype, otherwise you will need to change the top to include the template name
Code Segment (Text.cshtml)
Note that the following code segment is in razor format, but you can easily change that to a webcontrol.
@* Specifies that this is for the string data type *@
@model String
@{
//Create a dictionary to hold all the html attributes to be applied, this follows the requirement for the Html.Textbox last property which requires IDictionary<string, object>
var o = new System.Collections.Generic.Dictionary<string, object>();
//add in the default css class, notice that the @ is not required in this case
o.Add(“class”, “text-box single-line”);
//Attempt to find the validation attribute for maxlength
foreach (var r in ViewData.ModelMetadata.GetValidators(this.ViewContext.Controller.ControllerContext))
{
//What we are looking for is a validator of type StringLengthAttributeAdapter
if (r is StringLengthAttributeAdapter)
{
var attr = (StringLengthAttributeAdapter)r;
var ValidationParameters = attr.GetClientValidationRules().First().ValidationParameters;
//Get the validation parameters, maxlength is a required parameter so it is definately there, but just check in case for some reason it is not there
if (ValidationParameters.ContainsKey(“max”))
{
//add the maxlength property to the dictionary
o.Add(“maxlength”, (int)ValidationParameters["max"]);
}
}
}
//additional check to see if there is a width property specified
if (ViewData["width"] != null)
{
o.Add(“style”, “width: ” + ViewData["width"].ToString());
}
//Finally emit the html for the textbox
@Html.TextBox(“”, ViewData.TemplateInfo.FormattedModelValue, o);
}
Cloud Shared Notepad–Beta
This is the pre beta version of the cloud shared notepad, its free for all to use (unless it becomes so popular that my azure account starts charging me)
Anyway I acknowledge that there are definitely issues with the GUI, so please bear with me for a while.
Just some points to note
- Below the cloudsharenotepad.cloudapp.net/notes is actually a textbox, you have to enter your note url there (in this example it is myurlhere)
- You can enter your notify email, I have yet to implement this function, so for now it is purely cosmetic
Thanks for your support! ![]()
Publishing to Windows Azure from Visual Studio 2010
Here are the steps to publish to Windows Azure from inside Visual Studio 2010
- From inside Visual Studio, right click the Azure project (the icon looks like a blue globe)
- Click on “Publish”
- You will see the screen below

- If you have yet to import your publish settings, click on the hyperlink to generate the publishing file
- Once you get the file, use the import button to import your settings file
- You should now have a subscription available for selection

- If you do not have any hosted services, you will see the screen below. Else skip to step 9

- Fill in your hosted service name and preferred data centre location and click “OK”.
- In the publish settings, make the necessary changes and click “Publish”
- Windows Azure Activity Log will appear and you can see the deployment status there. Do take note that this may take quite a while to deploy even for a simple app.

- Once it is complete, the status will reflect to show it is completed and you will be able to see the url to your app

- Enjoy!
Azure TableServiceContext presents an error with CreateQuery function
If you were using azure table storage and found the following error when writing the CreateQuery function
does not contain a definition for’ ‘CreateQuery’ and no extension method ‘CreateQuery’ accepting a first argument of type..
What is happening is that other than adding Microsoft.WindowsAzure.StorageClient, you aldo need to add System.Data.Services.Client, which Visual Studio actually tells you when you attempt to build the project.
Error 1 The type ‘System.Data.Services.Client.DataServiceContext’ is defined in an assembly that is not referenced. You must add a reference to assembly ‘System.Data.Services.Client, Version=3.5.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089′.
Once you added the reference, the error will automatically go away.
Now if only there was an easy way to automatically add the missing reference in since Visual Studio knows the exact error and also what should be done..
Windows SQL Azure Firewall
For those who are new to SQL Azure, there are firewall settings that you need to take note of when you first create a SQL Database.
By default the firewall is set to block ALL external access
So if you wish to enable ALL access (aka disable the firewall), you can use the following settings
Dealextreme Affliate
if you are shopping at dealextreme, click this link and help me get some commission, thanks.
http://www.dealextreme.com/default.dx/r.43428218
What is this Cloud all about
The cloud term has been used in various forms over the last few Akamai had it with their cached storage, then came google and microsoft with their huge geographically dispersed data centres,and along came facebook.
I’ve never really been a proponent of using the word cloud as i never felt that technology has advanced to such a stage. In the last 2 years, what cloud meant was that you pay for a “cloud” instance and then use it. This was what hosting companies have been doing for the last 5 years via virtual private server (VPS). The difference between VPS and cloud instances is that you are charged for everything you use in the cloud, VPS usually impose a monthly charge.
This to me is just server virtualization, and not a true cloud, there is still server administration that i need to take care of, which is not really what i should be doing.
Recently after getting my hands dirty with Microsoft Azure and new System Centre 2012 and Windows Server 8 offerings, i’m beginning to feel that Microsoft really does know the direction they are going with cloud. No longer is it server virtualization, it lets you host your app and scale your app. It is no longer really about having to do windows update, restarts, worrying about correct installation of the components, securing the server, it is just about my app, and how to make it run smoothly.
This i feel is cloud, me as an app owner, managing my app, making it perform as best as it can, and leave the rest to a system that knows very well what it is doing.
Eject and Close CD Tray command line utilities
Been searching around the internet for a decent command line utility to eject or close the CD tray but can’t seem to find any. So i decided to write my own command line versions using visual studio 2010 and .net 4.
Note that the actual code does not utilize any of .net 4 features so you can easily take the same code and compile it against .net 1 or .net 2
The project is currently hosted in codeplex and can be found at http://ejectclosecdtray.codeplex.com/
Building resilient IT systems – IIS
History
Another main component of windows systems is the Internet Information Services aka IIS. This is what runs the web applications which we are so familiar with today.
Previously with IIS 6 and below, there is no easy way to synchronize the IIS metabase between 2 or more servers. The only way to do that is to install the IIS metabase editor and do a copy and paste between the source and destination servers.
With the introduction of IIS 7 and shared configuration, it is now possible to share the configuration between web servers so that the IIS running in all the servers are in sync and have the exact IIS configuration (assuming that the folder structure and user permissions are the same in all the web servers)
You can use the file share method to point the other web servers to the primary web server, however this may mean that in the event that the primary server is down, the other web servers IIS configuration may not reflect correctly as well. One quick way to work around this is to use DFSR to replicate the IIS configuration folder across the servers and have IIS point to a local path instead.
How to build a resilient IIS (7.0 and above)
In order to set up shared configuration, you can refer to this article or this video guide. In fact there are a whole lot of guide which can easily be found, but in general setting up shared configuration is extremely simple.
An alternative to using DFSR can be found here, where they use offline files to ensure the files are always available. Do note that in the comments, there are mentions that using DFSR might be a better option rather than offline files.
Once you have shared configuration up and running, you will have multiple servers hosting the same IIS content. However this is purely hosting, you still need a load balancing appliance/application to perform the load balancing.
To solve this, you can either use Windows network load balancing or a hardware load balancer box such as the likes from F5, NetApp, Radware (in no particular order).
This link explains how to use Microsoft Application Request Routing and in addition, the article provides 2 links somewhere at the end which teaches you how to integrate IIS with a hardware or software load balancer.
The benefits of a load balancer appliance is that generally they offer something known as global load balancing, where you have your servers all over the world and the load balancer has the capability to load balance your requests across these servers
Building resilient IT systems – File Replication
Some history about the File Replication found in Windows Servers
Since the days of Windows Server 2000, Microsoft has been providing distributed file system as an add on to the server systems.
This service known as File Replication System (FRS) basically detects changed files and copies the entire file to all the servers associated with the FRS.
In Windows Server 2003 R2, Microsoft shipped an updated version of FRS known as Distributed File System Replication (DFSR) which detects the changes in the files and only copies these changes to the other servers, using a technology called Remote Differential Compression (RDC), along with giving you the ability to schedule network usage.
One major caveat to using this technology is the fact that it only works with closed files It is not a suitable candidate for any files which are constantly opened e.g database files.
What is the purpose of DFS and DFSR
Distributed File System aka DFS is the technology where you access a share via a domain name. So instead of the traditional \\server\fileshare, you will instead type \\domain\fileshare.
There are several purposes of DFS
- Load balance usage: There is a limit to how many shares can be created and maintained by a single server, with DFS you can effectively load balance multiple servers to increase the number of available connections.
- Reduce network bandwidth (2003 R2 and above): Since only the changes are compressed and sent, it drastically reduces the amount of data that is being sent. Do note though that this comes at the expense of transmission speed and CPU clock cycles (Windows first needs to find the changes and then compress them before sending it over the pipe)
- Added redundancies: Althought DFS works with at least one server, adding an additional server effectively provides a basic form of redundancy to guard against single server failure. The cost to this then is that the storage requirement of any file is doubled or tripled depending on the number of servers participating in this share.
- Virtualize shares: Since you can effectively remap any share to a main domain share, you are able to then create your own share directory structure and DFS will automatically map it to the appropriate file share for you
- Ease of adding new hosts: Utilizing both DFS and DFSR, adding and synchronizing the new host is done with just a few clicks. DFSR will automatically synchronize the file contents for you in the backgroud
- Ease of recovery: Should you have a host that went down unexpectedly, once it is up, DFSR will automatically synchronize the changes to the host
The main role of DFSR is purely to replicate the data for the various DFS shares. One of the main things to note is that although remote differential is good in a WAN setup, if your servers are all connected in a LAN (at least 1gbps), then it becomes a hindrance and you will do better to turn it off. The steps can be found here.
Guides to setup DFS
There are already detailed steps for setting up DFS and DFSR for the various Windows Server editions at technet, just click on the server edition which you are using to get the guide.
Important links you should take a look at before using DFS