Tuesday, October 08, 2013

Serializing list of enums in .NET

Recently I've been coding some WCF methods and I've got a strange exception during WCF message serialization. I needed to send list of enums to the WCF service. There are couple of ways to do that.
First solution is using [Flag] attribute to combine several enum values into one variable - but then you need to use powers of 2 for enum values. I couldn't do that because of the requirements and actual big number of that enums stored in current database. It would require writing and applying a lot of scripts just to correct old enum values in db.
Another way of doing that is by passing a list of enums. So I've decided to use that approach. But strangely I've encountered a little problem around that. Below is a sample code from WCF data contract:

 public class SomeClass  
   public List<SomeEnum> SomeEnums { get; set; }  

Looks pretty simple but if we will try to pass empty list of that values we'll encounter an below exception:
Enum value '0' is invalid for type 'SomeEnum' and cannot be serialized. Ensure that the necessary enum values are present and are marked with EnumMemberAttribute attribute if the type has DataContractAttribute attribute.
Code is valid. The problem is that during serialization .NET framework is trying to serialize empty list which has capacity set to something bigger than 0. So the solution is simple - we just need to set the capacity to 0 by using Count property.

 someObject.SomeEnums.Capacity = someObject.SomeEnums.Count;  

It's a shame that it isn't working like that out of the box. In my opinion .NET Framework should check that before serialization and do not throw any exception. Code is not doing something invalid so it shouldn't throw any exception.

Sunday, October 06, 2013

Gaining MCSD - about Microsoft exams

Recently I've passed last exam required to obtain Microsoft Certified Solutions Developer. Now I'm fully certified in web development in .NET framework. That wasn't my first meeting with exams from Microsoft and because of that I want to leave some comments about them.

First of all I must say that preparation for some of them took me more time than I thought. That's because exams are testing knowledge in a very detailed way. In my opinion sometimes in too detailed way. Of course they should be difficult enough to ensure somebody with this title is representing something but hey - in some areas we are being questioned which attributes in WCF configuration are valid(!). That's not fair in my opinion - I don't think that somebody might be editing those files without referencing MSDN documentation or some kind of editor.
Below are exams that I've passed in chronological order.

70-513 TS: Windows Communication Foundation Development with Microsoft .NET Framework 4
It was mostly about configuring WCF services. I have couple of caveats - as I've said earlier I don't think that knowing all configuration options by heart is a good thing, we need to be realistic about it.

70-480 Programming in HTML5 with JavaScript and CSS3
This exam was offered for free by Microsoft, so I've decided to give it a try. Before preparing for it I thought that it might contain some questions about Metro style specific styles and extensions, but it was purely about HTML5 and CSS3 without mentioning anything about Metro UI.

70-516 TS: Accessing Data with Microsoft .NET Framework 4
This exam was checking our general knowledge about Entity Framework, LINQ To SQL and similar topics. I think it is a very important one, because in almost every application that is being developed we can encounter tampering with data of some kind.

70-515 TS: Web Applications Development with Microsoft .NET Framework 4
Because I've been working a lot with web technologies, mostly with ASP.NET MVC I thought it will the be easiest one to pass for me... and I was right - from this particular one I've managed to gain maximum number of 1000 points. What I didn't like about this exam? The fact that they are testing knowledge about ASP.NET MVC 2 when the current edition is MVC 4. Framework has changed a lot during that time. Hopefully there wasn't too much questions about MVC at all.

70-519 PRO: Designing and Developing Web Applications Using Microsoft .NET Framework 4
This exam was the last one and in some way it's trying to sum up knowledge tested in three earlier exams. Because of that fact it was also rather easy one. After passing that one I've obtained MCPD title.
70-486 Developing ASP.NET MVC4 Web Applications
That one was the next version of 70-515 - I was complaining that it was about MVC2 so they've prepared new version with MVC4 on board (so MVC3 is not included in any exam at all) - and there were even questions about mobile stuff. This exam is definitely the most appropriate in web developer path. It is really testing knowledge that is crucial in real life scenarios. Very important one.

70-487 Developing Windows Azure and Web Services
That one is in my opinion new version of 70-513 because it is mostly examining WCF knowledge. It's a pity that there were only couple of (maybe 3 or 4) questions about Windows Azure. It should have removed Windows Azure from title - because it was only testing general knowledge around that. Most questions were about WCF and LINQ.

To sum up exams are really different - some of them are testing really important knowledge which is good and some of them are checking if you can remember xml configuration schema properly which is bad. Good thing is that preparation to exams in standardizing knowledge and making it more confident. Even though I had had some knowledge around particular topics after preparation my self-confidence had risen.

Another interesting thing is that some kind of people (especially me) need some kind of goals to achieve. Some kind of path to follow. It is really satisfying when you finish something that you have been doing for a long time.

Sunday, February 24, 2013


As I mentioned some time ago Microsoft is offering free 3 month trial account in the BrowserStack service. Because I'm currently working on application that should support variety of browsers including old IE versions (fortunately starting from IE7 only - not IE6) I've decided to give it a try. I must say that I'm really impressed about it. But first let me explain some more details.

What is it

It is a service that gives us a possibility to use almost every browser running on operating system of our choice (but they are missing Linux - place to make some improvements in the future). We can have access to the remote browser running somewhere in the cloud to check how our application is working in it. No more using virtual machines, no more installing anything - BrowserStack is running in browser which simplifies a lot of things.

How it works - live sessions

First of all we can choose on which browser and on which operating system we want to test our application. Then in a very quick manner an instance of this browser is being prepared. Average preparation times were about 1 minute. It is acceptable because preparing your own virtual machine and starting it would definetely take much more time. After mentioned 1 minute we can control remote machine through some Adobe Flash application. It feels like remote desktop but limited to browser window. Of course we can use developer tools in almost every one of it - it is really good idea because without them service would be useless.


Most of applications that need to be tested are rather running locally on developer machine. For BrowserStack that isn't a problem - there is a tunneling feature to make our local sites available for the remote browsers. Tunnel is being prepared by simple Java application.

Automated testing

The most interesting thing is that with this service comes an API for making browser instances programatically (from any language where it is possible to send HTTP request). We can specify URL which should be opened when the browser will start. It is very useful because we can leverage automated JavaScript tests of our application. For example we can write and run Jasmine unit tests in the cloud. To collect test results from the remote browser we need something that can send data from the JavaScript code in the remote browser to our computer - and that is the place when node.js come in handy. There are several test runners on the internet (Testem, Yeti, etc.) that are built on the top of node.js and socket.io to send test results back to our computer through sockets. Of course they are travelling using tunnel that was prepared earlier.

Thursday, February 21, 2013

Disable attach security warning

Visual Studio by default is showing a message when trying to attach to process. It is saying that:
Attaching to this process can potentially harm your computer.  If the information below looks suspicious or you are unsure, do not attach to this process.
Because I'm attaching many times a day it becomes really annoying. Couple minutes of googling and here it is the solution for disabling it:
  1. First you need to make sure that Visual Studio isn't running.
  2. Next you need to modify registry key depending on which version of Visual Studio you have.

    For Visual Studio 2008:

    For Visual Studio 2010:

    For Visual Studio 2012:

    In all cases simply set DisableAttachSecurityWarning to 1.
That's all.

Wednesday, February 20, 2013


Recently Microsoft did something really interesting. Everybody knows that Internet Explorer (especially older versions) shouldn't be even called a browser. But still Microsoft Windows is the most popular operating system used commercially and so their browser is. Every web developer  knows that making something to work on the IE is really challenging task. Microsoft learned a lesson in couple of recent years and wanted to repair their image in that field. Internet Explorer 9 and 10 aren't the best browsers but in my opinion they are now acceptable. But they can't repair the old versions and years will past before they will disappear completely

So they have prepared a website: http://www.modern.ie which is trying to make developing web applications supporting Internet Explorer easier.

First of all they've prepared a tool that can scan our website and get a report that can help us improve general look and feel experience of our site. In some situations it can be helpful but it's always better to see how our site is looking in the real browser.

Second great feature is set of virtual machines with all Internet Explorer versions starting from IE7. Apart from that there is a nice bonus - 3 month free trial account in the BrowserStack - a service which we can use to test how our application is looking on a variety of browsers and operating systems, including mobile ones. This service is really amazing.

Microsoft prepared also a developers guide for applications that must work on older versions of their browsers.

All of that is really looking good and I think Microsoft realized how difficult it is to develop web sites for the IE. When developing on a computer that has a Internet Explorer 9 installed I've been using IE7 and IE8 modes to test if everything is ok with application, but to be honest I can remember several situations in which this mode doesn't really reflect true behavior of those old browsers. So it's a really step forward from Microsoft in the developers direction. Hopefully the next versions of Internet Explorer won't be so popular because of problems around them.