“An existing connection was forcibly closed by the remote host” error in WCF service

That was an interesting one. There was only one method in the service that was causing that. On stackoverflow there were several pointers, but all of them were pretty useless. It turned out that its all about iheritance of [DataContract]. I had a simple service that was returning an object:

From there there was...

Required attribute not triggering an exception on SaveChanges()

This was a very interesting (and annoying at the same time). I was writing some integration tests and it turns out that the [Required] attribute doesn't trigger an exception on SaveChanges().  

No exception raised. Instead, we can make the required property nullable.

This time we get: System.Data.Entity.Validation.DbEntityValidationException. I did some digging and it...

Removing Entry from a Database using Entity Framework

Hmm... Most of the tutorials on the internet seem to focus on how to remove these objects from DataContext as oppose to DbContent. In a Code First approach I don't really use DataContext, so I had to remove the object via DbContext and this is how it's done:

Seems to be pretty simple...

Lack of Constructor Inheritence in C#

One of the most annoying things about C# is lack of constructor inheritance. It seems pretty obvious that if the constructor of such type is not defined it could inherit from the base class. That's what happens in PHP.

But no... Microsoft decided not to do this:

I think this is a very...

Get and private set in C#

Yet another interesting feature in C#. Unlike Java, Microsoft tends to put more emphasis on Attributes rather then methods. Hence, they've implemented an interesting encapsulation mechanism: get and set. Although I new about getters and setters I've encountered an interesting phenomenon: you can specify encapsulation mechanism separately for get and for set. For example public...

Returning multiple values in C#

In Python you can return multiple values from a function. This however cannot be done in C#, shame, I think this is a nice feature of the language, but then again, how would you define a type of such function? private int,string methodName()? A bit strange. None-the-less returning multiple values from a function can be...

Using #region in Visual Studio

#region is an interesting instruction that tell visual studio how to wrap the code. In larger files it becomes almost a necessity to wrap certain bits/regions of code. I especially like using it when dealing with several interfaces.

I have to admit... This is pretty damn handy...

Irritating C# Exceptions

Today, when writing my tests I was expecting ValidationException thrown by SaveChanges(). I had the [Required()] attribute over a property. Well, then I realised that all tests failed. The records where not actually added to the database, which was good. It turned out that instead of a ValidationException I was getting DbUpdateException and that got...