find a location for property in a new city

Monday 28 February 2011

Multiple generic constraints in C#

I had some trouble using my nice new generic class in C#. I want to ensure one of the types passed in inherited from a particular class. This class can be instantiated using the new key word however the compile time is not aware of this

I found, using this unnatural syntax, it is possible to constrain the type parameters in multiple ways. Like so:

public class MyClass <T, U> : MyBaseClass <T> where T : class where U : AnotherClass, new()
{
    public AnotherClass AnotherClass { get; set; }
    public MyClass()
    {
        AnotherClass = new U();
    }
    //my class
}

This works fine and can be very useful at times

Follow britishdev on Twitter

Wednesday 23 February 2011

Google's DFP adservices have no secure HTTPS ability!

I have been getting security warning on my secure pages because an unsecure resource has crept in since I've started using Google Ad Services.

You will struggle using Google's "DFP" adservices on secure https pages since Google is not hosting their http://partner.googleadservices.com/gampad/google_service.js file on anything capable of serving securely.

Well, they will work, but they will undermine your attempts to reassure users they are on a secure page. In IE8 for example you get the huge AAAAAAAAAGGGGHHHH!!!! IT'S UNSECURE THEREFORE YOU ARE DEFINITELY BEING HACKED. YOU ACTUALLY NEED TO CLICK 'NO' TO PROCEED UNTOUCHED!!! style ridiculous error.

So what the hell are Google ad services playing at?

Usually, you would just reference the secure version of jQuery, skimlinks, OpenX etc so avoid this problem. As a someone trying to push from OpenX to DFP I am disheartened. Will they ever support https? "Not at this time."

Google: sometimes you can be quite disappointing. I first found this problem October 2010. Still there now... (23rd Feb 2011)

Update - It now works!

It now works. Thanks to a commenter I thought I'd recheck the situation and it looks like Google have resolved this issue as of 7th July 2011 (or some point before). You can access https://partner.googleadservices.com/gampad/google_service.js which will now keep your secure pages happy.

Follow britishdev on Twitter

SEO Redirecting URLs to lowercase excluding static files

As you may know Google treats different cased URLs as different URL entirely. If they find both cases they will judge this as duplicate content so it is a good idea to redirect requests for a URL containing uppercase to lowercase.

This is made easy with the URL Rewriting Module 2.0. You can use the "Enforce lowercase URLs" template to do most of the work for you.

This will create a pattern match of [A-Z] (as in if there is even one capital in the URL) it will 301 redirect to {ToLower:{URL}} which is fairly obvious what it does.

What about static resources files?

You don't really want these redirects to happen since you don't really want search engines to index them so it takes up needless resource. Also, for my particular set up it fails to send them (some sort of weird, secure + load balanced + redirecting issue).

  1. Open the Conditions section of your lowercase redirect rule and click Add...
  2. Set Condition input to {URL} (which doesn't look at any querystrings).
  3. Set Check if input string to Does Not Match the Pattern
  4. Change the Pattern field to one that suits you. I have used ^.+\.(axd|gif|jpg|jpeg|png|css|js|svc|ashx)$ since I do not what files with these extension redirected. You may have more. Or less
  5. Ignore case should be checked
  6. Click OK and Apply your new rule

Your site should now send www.mysite.com/Home/Index to www.mysite.com/home/index with a permanent 301 redirect and leave www.mysite.com/WebResource.axd alone.

Follow britishdev on Twitter

Monday 21 February 2011

Multiple duplicate cookies and one blank cookie

I noticed we had a problem where there were duplicate cookies on my site. One was correct but the other was carrying an empty string.

It took a lot of debugging and use of Fiddler but I found out something very odd with the .NET framework that was causing this problem.

The oddity

It would seem that evaluating the response's cookies creates one if it doesn't already exist. Here's an example:
if (Response.Cookies["myCookie"] == null)
{
    var cookie = new HttpCookie("myCookie", "myValue");
    cookie.Domain = ".britishdeveloper.co.uk";
    cookie.Expires = DateTime.Now.AddDays(7);
    Response.Cookies.Add(cookie);
}
//relax because you now have a cookie with the intended domain and expiry. Right?

Line 1 actually creates the myCookie cookie! If you ever evaluate the property indexer it will create that cookie with no value, no expiration, no domain. Nothing, but it will still exist! Fiddler will show you this in your Response headers: Set-Cookie: myCookie. Also, line 1 will always evaluate to false because of this.

How to avoid the blank cookie

If you are checking for its existence you should NOT use the index property as it will make a new cookie if it doesn't exist.

Instead, use this code that star developer Leo discovered to resolve the issue:
if (!Response.Cookies.AllKeys.Contains("myCookie"))
{
    var cookie = new HttpCookie("myCookie", "myValue");
    cookie.Domain = ".britishdeveloper.co.uk";
    cookie.Expires = DateTime.Now.AddDays(7);
    Response.Cookies.Add(cookie);
}
//relax because you now have a cookie with the intended domain and expiry. Right? YES!

Line 1 now evaluates if myCookie exists in the collection of all response cookies rather than accessing (and creating) it directly. So now you know, hopefully it won’t cause you the hours of debugging it cost myself and Leo.

Note: This is not true of Request.Cookies - only Response.Cookies. Lovely bit of inconsistency there

Follow britishdev on Twitter

Thursday 17 February 2011

Proxy server connection failed

I got a mysterious "Unable to connect to the proxy server" when trying to connect to any website. The other useless message was "Error 130 (net::ERR_PROXY_CONNECTION_FAILED): Proxy server connection failed."

It turns out I was using a proxy. I did have fiddler running earlier and I shut it down by ending its process via Task Manager.

Looks like Fiddler needs you to shut it down properly when you are done with it because it obviously needs to stop being a proxy to your internet connection.

Solution

Simply open Fiddler up again and then close it properly and it should close it's current proxy service and you should be fine.

Follow britishdev on Twitter

Wednesday 16 February 2011

OutOfMemoryException when cropping image

When cropping an image using System.Drawing.Bitmap.Clone(Rectangle rect, PixelFormat format) I was receiving an OutOfMemoryException complaining that I was "Out of memory" when trying to crop an Image bitmap in C#.

So I started to assume that I was out of memory as in physical storage on my machine as you might expect. Turns out that that is not in fact the case as you would surely assume...

What's wrong with this clone?!

Well it turns out that the cropping was happening on an image (1 pixel) smaller than the desired cropping size. So I changed my logic about a bit to essentially make sure that the source image is at least the size of the desired crop size. And now the OutOfMemoryException on my Image processing code is working just fine.

Follow britishdev on Twitter

Tuesday 15 February 2011

Regex [x-y] range in reverse order

I started receiving a 'System.ArgumentException' error with a decription of 'parsing "[A-Za-z0-9_- %]+\.(bmp|jpg|jpeg|gif|png)" - [x-y] range in reverse order' after I wrote a C# Regex in my ASP.NET MVC web application.

I was using a RegularExpression Validator in my ASP.NET MVC app but it wasn't having any of it. I was looking at "A-Z" etc thinking IT IS THE RIGHT ORDER!

Turns out it is because I have a hyphen in another part of my regex expression. The one after the underscore.

Solution

Turns out hyphens have to appear at either the front or the end of the regular expression - or be escaped with a \. So now I have updated my regular expression from
[RegularExpression(@"[A-Za-z0-9_- %]+\.(bmp|jpg|jpeg|gif|png)")]
to
[RegularExpression(@"[A-Za-z0-9_ %-]+\.(bmp|jpg|jpeg|gif|png)")]

Follow britishdev on Twitter

Monday 14 February 2011

Working solution breaks on new machine. IIS setup with URL Rewrite Module

A solution that works fine on one machine will not work on a different development machine. I just received a 503 service unavailable error and the IIS rewrite module was giving an error saying "There was an error while performing this operation" and "Unrecognized attribute 'trackAllCaptures'"

When I get a massive IIS style, 'light blue screen of death' error message it is usually something to do with web.config not recognising some attribute. Clicking about the various modules in my site through IIS I soon discovered that it was the URL rewrite module specifically I was having problems with. It was saying that trackAllCaptures was an unrecognised attribute.

It was complaining about not recognising a particular node. This was because I hadn't got IIS URL Rewriter installed on my new machine. Installing it works a treat!

Follow britishdev on Twitter

Thursday 10 February 2011

Unable to load the specified metadata resource. Where are my metadata files?

I made a new model using Entity Framework but I noticed my connection string was blank. I was receiving an "Unable to load the specified metadata resource" error message so I realised that it clearly couldn't find my meta description files. So where the hell are they?

They are not obvious to find and although I was diligently typing in the location of my csdl, ssdl and msl files I have never actually seen them myself.

So where are my csdl, ssdl and msl files?

I opened up Reflector, clicked open and browsed to and opened up my Models.dll:

Here you can see the proper files names to ensure you have got them right in your connection string.

Follow britishdev on Twitter