find a location for property in a new city

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

Friday, 28 January 2011

Caching. Absolute expiration or Sliding scale expiration?

You can set a cache with a temporal expiration in one of two ways:

Absolute expiration

This mean you set a time in the future in which it will expire e.g. DateTime.Now.AddMinutes(5). So the cache will remove the object from memory at that particular time in the future.

Sliding scale expiration

This means you set a length of time that the cache should keep the object in memory for from the last time it was accessed e.g. new TimeSpan(0, 0, 5, 0). That is 5 mins. So the object will be removed from cache if it hasn’t been accessed for 5mins or more.

Which temporal caching method to use?

I believe Absolute expiration is the one to use for most web page cases, unless there is a good reason not to. Imagine you are using a 5 minute sliding scale cache expiration on a popular page that gets say 50 hits/sec, chances are that this object will never expire from the cache.

How long to cache for?

Also avoiding huge expirations of hours or days on not outrageously expensive data would be an idea too. Although the benefits of large expirations are that you have to retrieve the data less often I think that reducing it from 50*60*5 = 15000 requests/5mins down to 1 request/5mins is efficient enough, without suffering from stagnant data.

Obviously, the length of how long to set your expiration for and your method of expiration is will depend on how expensive your data is and possibly other specific requirements but hopefully you can now make a more informed decision.

Follow britishdev on Twitter

Tuesday, 11 January 2011

Optimising Entity Framework - Don’t query from base entity

Be careful to query the derived type when dealing with an entity data model that involves inheritance. You can massively reduce query complexity by making sure you avoid querying the base type of your model.

Say there is a model with a base type of Person and three derived types of Customer, StaffMember, Client. It doesn't matter if your inheritance model is Table per Inheritance or Table per Type (TPT) but say for example it is Table per Hierarchy (TPH) and the condition for Customer is that an OrdersID column is not null. Also say there is a quality that of all Customers that they will have a guaranteed value of PersonType == 1.

So what is the difference between:

context.People.Where(p => p.MessageType == 1) and context.People.OfType()?

Aside from the better code the difference in generated SQL is massive. For some reason Entity Framework will generate SQL that works out which derived type each of the rows in the Person table are even though I'm asking for just the base type. This means unnecessarily joining on a number of tables if you’re using TPT or evaluating many conditional columns if you’re using TPH.

I tried the different approaches on quite a large Entity Data Model of mine. There is one base type with 5 derived types and a few other entities being included (joined). Using the first query from above the generated query was 28224 characters long. The revised query was 14177. That's half the size!

Using query length is a poor way of evaluating a query because most of EF queries are explicit query lengths which don’t actually add to complexity but believe me – in the case the saving of query length was almost exclusively down to removing pointless complexity.

Follow britishdev on Twitter

Thursday, 6 January 2011

Enable assembly bind failure logging using Fusion

I got a FileNotFoundException with an error message of 'Could not load file or assembly 'LoveMoneyHttpModules' or one of its dependencies. The system cannot find the file specified.' This doesn't help much but does give some advice on enabling assembly bind failure logging.

The advice is to turn on the assembly bind failure logging but gives a tip of setting the registry value [HKLM\Software\Microsoft\Fusion!EnableLog] (DWORD) to 1.

That's not very friendly advice so the question is how to do that exactly. I found a nicer way to do it than messing with the registry. First, you need to open the Fusion Log Viewer tool. You can do this by opening a Visual Studio command prompt and running fuslogvw.exe or alternatively just type Fusion Log Viewer into your Windows search bar thing.
  1. Open Fusion Log Viewer
  2. Click 'Settings...' and select the 'Log in exception text' radio button. Click 'OK'
  3. Do an IISreset.
The IIS isn't meant to be part of it I just found that it wasn't working until I tried that too. Anyway, after that I started getting logs of the attempts at binding in my yellow screen of death.

Follow britishdev on Twitter

Tuesday, 4 January 2011

How to POST to REST WCF web services with Fiddler

After making a set of REST web services with WCF I tested them by making requests with a browser. This is fine for GET requests however you will need some HTTP building tool such as Fiddler to test a POST, PUT or DELETE REST web service.

To begin with I have a WCF REST web service that intends for a Comment object to be POSTed:
[WebInvoke(UriTemplate = "comment", Method = "POST",
  RequestFormat = WebMessageFormat.Xml, ResponseFormat = WebMessageFormat.Xml)]
public Comment Comment(Comment comment)
{
    return comment;
}

The definition of Comment is simple:
[DataContract(Namespace = "")]
public class Comment
{
    [DataMember]
    public string Body { get; set; }
}

Note this namespace is very important as it will need to be specified in the XML sent in the request. Personally I set my DataContract with no namespace to save having to write it out in the XML every time.

Now open up Fiddler and lets get testing:

Create a POST to your REST service in Fiddler

  1. Go to the Request Builder tab in Fiddler.
  2. Set the verb to POST
  3. Set the URL to the one of your service
  4. Type "Content-Type: application/xml" into the Request Headers. (This is the bit that got be stumped for hours)
  5. Add your XML to the Request Body like <Comment><Body>colin</Body></Comment>

If you did give your DataContract a namespace other than blank as I did you will need to specify it in the XML of your Request Body. Such as: <Comment xmlns="MyNameSpace"><Body>colin</Body></Comment>

Happy testing!

Follow britishdev on Twitter

Thursday, 23 December 2010

3 things that changed ASP.NET development in 2010

As 2010 draws to a close I thought about how I was writing code last year. I realised how much development has changed in just one year. I want to look at how my work has evolved during 2010.

Entity Framework 4

I really like using Entity Framework. I wasn't quite sure about it in its first version, however, since the release of Entity Framework 4 in April I fully adopted it. It solves so many problems that I feel exist in modern web development.

I like stored procedures, I know a lot about using and optimising them but there is something seriously wrong with their concept - you have business logic in your database! When you think open mindedly about what a logical architecture should be you realise just how ridiculous that is. Entity Framework takes the last remaining strand of business logic back to the web developer.

Aside from the improvement in architecture, the other benefits I love about it is that speed you can develop with, deferred querying that improves reuse of queries, strong typing of Linq to Entities, Intellisense when writing queries, no more mapping, no more writing tedious POCOs, lack of tedious code in general.

Its hard to believe only a year ago I was writing stored procedures.

ASP.NET MVC 2

Again, I had a look into this when it was in its first version but was carried by the hype around v2. I fully adopted the use of MVC in April too and what a joy it has been to use.
A lot has changed over the last 10 years of web development such as increasing expectations of performance and web standards, popularity of unit tests and agile development, ubiquity of AJAX.

Now, I wouldn't go so far as to say there is anything wrong with ASP.NET Webforms I just feel that ASP.NET MVC has been a superb reaction to these changing trends in what web developers want to produce.

Whilst all of the above concerns can be answered by Webforms, like turning off view state to remove bloat, moving logic out of code behind to unit test, improved web standards in ASP.NET 4 etc you still feel as though you are swimming up stream some what.

I much prefer to get down and dirty with ASP.NET MVC. It's refreshing to get involved in HTML and HTTP again in a way that was abstracted out of your hands with web forms. It makes you feel like a real developer again, omnipotent! Of course the framework and templates do a lot of the tedious work for you but let's keep that quiet ay... Omnipotent! Remember that, yeah.

It's hard to think only a year ago I was using web forms.

Contribution

This has obviously been around for some time but I think only during 2010 has Microsoft's commitment to open source and code contribution has really shone through. It had been displayed, proven and truly adopted by the community. Look at how much there is to gain from MVC contrib.

I would like to think this is why frameworks such as Entity Framework and ASP.NET MVC are evolving so quickly and more importantly in the right direction. Microsoft is really listening to the community now. Forums and polls about what you would like to see in the next version of Entity Framework for example.

It is not just direct listening either, I'd be extremely surprised if Microsoft weren't reading blogs, browsing stackoverflow and stalking twitter to gain insight into what the community love and what they are crying out for.

It's hard to think last year I didn't have a blog or a twitter account or contribute on stackoverflow or publish any code.

Follow britishdev on Twitter

Wednesday, 15 December 2010

WCF Data Services for using Entity Framework over REST web services

I spent some time looking into WCF Data Services 4.0 (AKA ADO.NET Data Services & Astoria) as a way to expose our Entity Data Model (EDM) over an easy to create set of RESTful / OData web services. I decided to document my findings.

WCF Data Services 4.0 is service that provides a way of exposing an EDM over RESTful (OData) web services using WCF. It means that if you have an EDM already built, the work is essentially done. The EDM can be exposed using WCF Data Services so that it can be queried directly by the client.

How to begin

I successfully set up some WCF Data Services to expose a Comment part of my model. This helped me. I did find difficulty due to our use of POCOs instead of raw Entity Framework's generated code but I found a post that helped which told me about a weird hack that overrides the CreateDataSource into the Data Service:
public class CommentsService : DataService<SocialEntities>
{
    protected override CommentingEntities CreateDataSource()
    {
        var context = new CommentingEntities();
        var tracestring = context.CreateQuery<Comment>("CommentingEntities.Comments").ToTraceString();
        return context; 
    }
}

Consuming the web services

A client can now access the data by requesting URLs. E.g. http://api.mysite.com/CommentsService.svc/Comments to get a list of messages or http://api.mysite.com/CommentsService.svc/Comments(413L) to get the message with ID 413 (the L is for long since I use long instead of int)

The querying gets a lot more complicated than that too. See how to use your browser to query the data model

There are many SDKs out there to begin developing against your WCF Data Services from a number of popular platforms since it is simply using the OData protocol (e.g. JavaScript, PHP, Ruby, Java, Objective-C, .NET).

Restricting use

You can restrict the number of results to return when returning a list of entities to, say 5, so the client can't overload the server/bandwidth.

You can also restrict access to select parts of your EDM to calm your security nerves too:
public static void InitializeService(DataServiceConfiguration config)
{
    config.SetEntitySetAccessRule("Comments", EntitySetRights.AllRead);
}

Evaluating when to use WCF Data Services

So it does look like a great way to use the existing EDM to instantly have a set of RESTful web services to consume. However I think I would only use this if the developers of the web service client were have knowledge of the data and are expecting to get stuck into some code when consuming our data services.

If were internally designing our own Silverlight apps then this is great. However, expecting an external user to write sometimes complex queries to get to our data using all of our business rules (e.g. IsDeleted = false && SiteID = 21) is a little much to be expected I feel.

Conclusion

So great when providing RESTful web services to a handful of specific clients who have knowledge of your data and business logic. Not so great for making a friendly API for the masses based on a complex EDM as you cannot expect them to understand your business rules as well as you do.

I am not claiming authority on this topic. I am just discussing my findings so far in the hope that you may find this useful. If you have anything you think I have missed please feel free to comment.

Follow britishdev on Twitter

Thursday, 2 December 2010

What motivates developers?

It concerns itself with the surprising truth of motivating people who's job requires cognitive skill (I'm taking that to include developers). It is very different from what you might you expect and explains why Linux and other open source exists.



Interesting indeed. What do you think?

Follow britishdev on Twitter