by Njål

Quick row count from Microsoft SQL Database

 

Recently, I’ve been involved in a cloud provider project where I had to do a row count on a Microsoft SQL Database table with more than 50.000.000 rows in it.

Doing

SELECT COUNT(*) FROM myTable

took about 10 seconds – since it performs a full table scan.

 

Here’s an alternative way (still accurate) of doing it that takes under 1 sec:

SELECT SUM (row_count)
FROM sys.dm_db_partition_stats
WHERE object_id=OBJECT_ID('myTable')   
AND (index_id=0 or index_id=1);

 

You can run this from Microsoft Entity Framework as well:

long total = dbContext.Database.SqlQuery<long>("SELECT SUM (row_count) FROM sys.dm_db_partition_stats WHERE object_id=OBJECT_ID('myTable') AND (index_id=0 or index_id=1);").First();

by Njål

The directory specified for caching compressed content is invalid. Static compression is being disabled.

image

 

After adding this in my web.config (Azure Project)…

<staticContent>
    <mimeMap fileExtension=".svg" mimeType="image/svg+xml" />
</staticContent>

…IIS refused to serve any static content – such as js and png files. Log entries in Event Viewer said:

The directory specified for caching compressed content
C:\Users\xxx\AppData\Local\dftmp\Resources\b503ce54-3fba-4772-beaa-415d94ffc214\temp\temp\RoleTemp\iisexpress\IIS Temporary Compressed Files\9ef2ab2d-28fc-4554-a09c-5ba867eb1d19 is invalid.
Static compression is being disabled.

The problem was that .svg files were already added to the staticContent list in my applicationHost.config – but still – the error message shown in the Event Viewer is pretty useless. There is nothing wrong with the directory specified to store compressed content.

So if you suddenly get this error – undo any changes in Web.config (remove all config under staticContent) and restart IIS (Express).

by Andreas

Email validation – finally a .NET regular expression that works!

imageNote: this is only relevant for .NET 3.5 and earlier. In .NET 4 the System.Net.Mail.MailAddress class includes validation and will throw an exception if instantiated with an invalid address. See comments below. 

Validating an email address sounds simple, and it really is! Until your fancy validator is released into the real world and people’s actual email addresses start pouring in, instead of the three email addresses you’ve been testing while developing: your work address, your Gmail address and an old Yahoo address you created in 2001.

chris-@somedomain.com
!habla-hobla%wow@somedomain.com
!def!xyz%abc@example.com
joe_blow_@somedomain.com
love/hate=relationship@example.com
$ilovemoney1234@example.com

They are, believe it or not, all valid according to the RFC spec. That doesn’t mean that you’ll be allowed to actually register them everywhere. Some (most?) email providers have their own rules that are much stricter than the specifications, simply because it makes no sense to allow all sorts of rubbish.

But if you’re writing a system allowing people to register using their email address, and you confirm their ownership of the given address by sending a confirmation email you really can’t be any stricter than the RFC and this is where the problem lies. I was also under the impression that “blah, how often would THAT become an issue?!”, but recently I got feedback from three different users within just a few weeks that they were unable to register. So I thought I’d better find a regular expression that actually works.

I’ve tried them all.. examples from Microsoft, Stack Overflow, random forums, cooked up my own and also converted a couple from other languages. It wasn’t until yesterday I stumbled across an old blog post by Phil Haack called I Knew How To Validate An Email Address Until I Read The RFC. He did all the hard work (i.e. reading the #¤%&”¤ RFC specs), and came up with an expression that so far seems to be working. So I thought I’d better save it here, because I know I will need this one again:

string pattern = @"^(?!\.)(""([^""\r\\]|\\[""\r\\])*""|"
                + @"([-a-z0-9!#$%&'*+/=?^_`{|}~]|(?<!\.)\.)*)(?<!\.)"
                + @"@[a-z0-9][\w\.-]*[a-z0-9]\.[a-z][a-z\.]*[a-z]$";
            
Regex regex = new Regex(pattern, RegexOptions.IgnoreCase);
return regex.IsMatch(emailAddress);

 

So thanks to Phil for his efforts! Let us know if anyone finds a flaw in this one as well…

by Andreas

.NET HTTP Handlers (.ashx) – How to get access to Session

.NET HTTP Handlers (.ashx) are really useful for jQuery / JSON communication, but if you are using these as part of a bigger web application you will find that you don’t have immediate access to the current Session data. In a recent project I needed to validate some of the current user’s session information before allowing the handler to be invoked. In order to get access to HttpContext.Current.Session from the System.Web namespace you’ll have to add the System.Web.SessionState.IReadOnlySessionState interface in the class definition:

image

This will allow you to access the current session data:

// get session data
var sessionVariable = HttpContext.Current.Session["sessionVariableName"];
by thorhalvor

NCrunch = time saved

NCrunch is an automated continuous testing tool for Visual Studio .NET. It takes responsibility for running automated tests so that you don’t have to.

Instead of going through these steps when doing Test Driven Development:

  1. Write tests
  2. Run tests
  3. Write code
  4. Run tests
  5. Refactor code
  6. Run tests

NCrunch will take care of all the “Run tests” and you can therefore concentrate on writing and refactoring your tests and code.

How does it work?

I will give some simple examples below but you can also go to NCrunch’s About-page where you can see a video.

First of all, will you get new column with red/green/black dots on every line of code.

Green = Covered by successfull test
Red =  Covered by test but failing
Black  = indicating that there are no tests for this line of code.

It will test while you write and you get feedback while coding. No need to compile or save the document.

A specific example of the most basic steps is shown below:

A: You see a green dot for every line of code. That means at least one test is testing these lines and it is also successful.
B: If I hold the mouse over the dot at line 45 I can see that there are 4 passings tests for this part of code.
C: If I comment line 43-46, the color of the dots will immediately change because some tests are now failing. And some code is untested.
D: Holding the mouse over the red dot at line 42 gives a summary of why it is red. This part of code is covered by 3 passing tests and 1 failing.

This was only the basics of NCrunch. Other things it can do is for example: Code Coveage, Performance metrics, Inline Exception Details, Paralell Execution ++

My advice is therefore: Download, Install and Enable it. Try to not focus on it in the beginning, just “let it do its job”, get familiar with the new “dots” and explore its features step by step.

by thorhalvor

Are you still writing your own TemplateEngine?

Years ago I was on a project that needed a dynamic engine for writing documents based on a template. It had to be possible to add keys like “$name$” in a text and the engines job was to replace with a Name from a given context. I ended up writing a module that used reflection on my context-object and it worked the way I wanted. – But it took some hours developing it…

NVelocity / TemplateEngine

Later I discovered NVelocity. NVelocity is an open source project, and as many others starting with a ‘N’, is based on the Java project Velocity. Castle Projects has abstracted the templating process and made it even easier to use in their TemplateEngine Component. This component gave me all I wanted and more. If I just had knew earlier..

An example of a template for Castle’s TemplateEngine is given below:

Hi $customer.FirstName,
your account has been lost..
We are sorry for that!

Regards,
The bank

The code below is reading the template, mapping the key-properties and writing the output the the variable ‘text’. As you can see, it is putting the Customer object into an Hashtable with keyword “customer” and then forwarding it to the Processor as context. More or less the same I did in “my own engine”.

string text;
var customer = GetCustomer();
using (var writer = new StringWriter())
{
    var context = new Hashtable();
    context.Add("customer", customer);
    TemplateEngine.Process(context, "yourtemplatefilename.txt", writer);
    text = writer.ToString();
}

You can also do if/else and foreach logic (see here for more examples):

#foreach($person in $people)
<table>
       <tr>
           <th>Name</th>
           <th>Age</th>
    </tr>
</table>
#end

And:

#if($order.Status == "Undefined")
  Sorry, but we don't know this order.
#elseif($order.Status == "Created")
  Your order is being processed. Hold on!
#end
RazorEngine

Most new web projects created in the “.NET World” today, at least the one I know of, is based on ASP.NET MVC 3 and the parsing technology Razor. Developers have learned to use and to like the Razor syntax. And what could be better for a new templateengine than using the same technology? Matthew Abbot has done something about this and made in late 2010 the first version of the Razor Template Engine, in v2.0 called RazorEngine. It is opensource and can be found on GitHub and as a nugetpackage, ‘Install-Package RazorEngine’.

Simple example:

var template = "Hi @Model.FirstName! Knock knock who's there?";
var result = Razor.Parse(template, new { FirstName = "KenSentMe" });

or you could offcourse read the template from a file:

var template = File.ReadAllText("yourfilename.txt");

 

A nice feature is that the template-engine support anonymous types, – as you can see in the second parameter of Razor.Parse(). (and also DynamicObject and ExpandoObject). With this loose coupling to the context/model it makes it easier for a developer to extend it. He can now just add a new property directly instead of creating a specific class just to works as a “placeholder”.

I guess there are many more template engines “out there” but these are the two I have used on my last projects. I use RazorEngine on my current project, with html-email-templates, and it works very well. The main reason I choose RazorEngine is mostly because we use MVC 3 and Razor on the websites and the email template is based on the same style and context(Viewmodels). But also the fact that it is so easily downloaded and upgraded through the NuGet Package.

So If you still write your own templateengines I hope you will take a look on one of these engines next time.

by Njål

WebClient // HttpWebResponse – Problems with Chunked Transfer encoding

 

Doing a HTTP POST to a webserver using C# is pretty easy using the System.Net.HttpWebRequest. However – if you need to retrieve the answer/reply from the webserver – you’ll often get an exception:

Exception: 'Unable to read data from the transport connection: The connection was closed.'

When looking at the HttpWebResponse in the debugger – the length of the response is -1, so something strange is going on here.

When investigating further in the VS 2010 debugger/ Fiddler, I found out that the webserver (IIS 7 in this case) is sending the response using Transfer-Encoding: Chunked – which is relatively common in HTTP 1.1. It means that the length of the message isn’t specified in the HTTP Header Content-Length, but in the message itself.

Nothing wrong with this – but apparently HTTPWebResponse does not support Chunked Transfer Encoding (and thereby not HTTP 1.1).

The easiest way to fix this is to make sure the HttpWebRequest is sent using HTTP 1.0 – this way the server will reply with a HTTP Header specifying Content-Length.

HttpWebRequest wr = (HttpWebRequest)WebRequest.Create("http://www.degree.no");
wr.ProtocolVersion = Version.Parse("1.0");

An easier/alternative way of doing HTTP Communication in the .NET framework is the System.Net.WebClient class. But since it uses HttpWebRequest under its hood,  the same problem occurs here.

There is no way (afaik) to specify WebClient to use HTTP 1.0 – so use HttpWebRequest with HTTP 1.0 if you need to POST something to a server and read the response.

by Stian

The type or namespace name ‘log4net’ could not be found

If you have added a reference to log4net correctly and added your usings, you may get a compile error “The type or namespace name ‘log4net’ could not be found (are you missing a using directive or an assembly reference?)”.

This is most likely due to the Target framework being set to .NET Framework X Client Profile. Change this to .NET Framwork 4 (full version) and your application will compile again!

2011-01-27_1158

If you want to read more about how to set up Log4Net, read my colleagues blog post Configure log4net from code

Tags: ,
by Njål

ImageGen resize functionality for free!

ImageGen is a great little .NET library that basically resizes images on the fly. The typical use of it is on websides, blogs etc – where a image is resized just before sending it to the browser. It is often used in Umbraco CMS sites, but it can be used just as easily in any Asp.net site.

Here’s an example:

http://server.degree.no/media/39263/untitled-19.png (original image)

http://server.degree.no/umbraco/ImageGen.aspx?image=/media/39263/untitled-19.png&width=150 (resized by ImageGen)

It is often very useful to resize a image to a height of e.g. 60 pixels while also ensuring that the width should not be greater than e.g. 200px.
ImageGen offers this in its PRO version:

This feature is the only true useful feature of the PRO version ($199 per domain!) I have ever had use for.  Here’s a trick to achieve maxHeight / maxWidth functionality with the free version:

http://server.degree.no/umbraco/ImageGen.aspx?image=/media/39263/untitled-19.png&width=150&height=50&constrain=true

This resizes the image to a width of max 150px, height of max 50px, while preserving the aspect ratio.

Happy resizing!

by Njål

Configuring Log4net from code!

LOGGING!
Photo: A more traditional way of logging.

The first thing I do when creating a new .NET website/app is to add Log4net. I usually find some old code, and

  1. copy the log4net.dll,
  2. copy the log4net.config
  3. copy the c# code that loads the config file

For a lot of the smaller projects/solutions I am working on – there is no need at all to change log levels / appenders etc. during run time – so I configure log4net directly from my c# code. This means that I can drop the log4net.config file.

Here is a piece of code that creates a simple FileAppender. It can easily be modified to handle RollingFileAppenders etc..


Hierarchy hierarchy = (Hierarchy)LogManager.GetRepository();
hierarchy.Root.RemoveAllAppenders(); /*Remove any other appenders*/

FileAppender fileAppender = new FileAppender();
fileAppender.AppendToFile = true;
fileAppender.LockingModel = new FileAppender.MinimalLock();
fileAppender.File = Server.MapPath(“/”) + “log.txt”;
PatternLayout pl = new PatternLayout();
pl.ConversionPattern = “%d [%2%t] %-5p [%-10c] %m%n%n”;
pl.ActivateOptions();
fileAppender.Layout = pl;
fileAppender.ActivateOptions();

log4net.Config.BasicConfigurator.Configure(fileAppender);

//Test logger
ILog log =LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
log.Debug(“Testing!”);

The result of the log test is:
2010-11-13 14:52:46,611 [20] DEBUG [_Default ] Testing!
A good place to put this code is in the entrypoint of your App (the Main(string[] args) method), or in the Global.asax if you are creating a web app (remember to grant the NETWORK SERVICE user write access to the folder you are logging to).

Happy logging!

-N