21 February, 2012

WCF IEnumerable DataContracts bug

Kept getting an error while using SoapUI to create valid requests for a WCF web service hosted in IIS.
While debugging the operation executed without any problems, but the client always got a connection error (java.net.SocketException: Connection reset).
Turns out there is a bug in WCF when serializing a DataContract with an IEnumerable<T> member. Changed to List<T> and the problem was gone.

19 February, 2012

Solution file (.sln) comments (#)

When to adding comments to Solution files start the comment line with the # char.

Example:

# Comment

17 February, 2012

WCF service contract changes supported by old proxy

Needed to know which changes to a WCF Service Contract could an old proxy support. The tests were made with BasicHttpBinding and WsHttpBinding and a client with a proxy generated by visual studio. All DataContract attributes were strings.

My results were:

Add an Operation - OK
Remove an Operation - OK unless the client calls it.


Added Attribute to response object - OK
Added Attribute to request object - OK

Removed Attribute from response object - OK (the client interprets it as null)
Removed Attribute from request object - OK (the client interprets it as null)

Change the type of an Attribute of the response object -depends whether the new type can be parsed to the old type from the message string.
Change the type of an Attribute of the request object -depends whether the new type can be parsed to the old type from the message string.

Change the Attribute name in the response object - OK (the client gets null since the old attribute was removed)
Change the Attribute name in the request object - OK (the client gets null since the old attribute was removed)

All OK results indicate that no Exception is thrown.
The client will not be aware of new properties or new operations but will also not crash (at least for nullable properties, haven't tested types other than strings).

The results were the same for both bindings.

Other bindings, especially ones that have binary serialization will probably present different behaviors.

10 July, 2010

Comparing collections with LINQ

Once in a while i have to call the Distinct LINQ method and, more often than not, i don't want to compare the objects reference. To this effect Microsoft provides the interface IEqualityComparer<T>.
The IEqualityComparer<T> interface implements two methods: bool Equals(T x, T y) and int GetHashCode(T obj). These methods are both used by LINQ to compare objects in collections.
Since i need to compare most of the business objects in my solutions, and don't want to go through the hassle of implementing tens of new classes (one per object) I wanted a generic solution.
Simple enough, I built myself the GenericComparer<T>.

public class GenericComparer<T> : IEqualityComparer<T>
{
public GenericComparer(Func<T, T, bool> equals, Func<T, int> getHashCode)
{
this.equals = equals;
this.getHashCode = getHashCode;
}

readonly Func<T, T, bool> equals;
public bool Equals(T x, T y)
{
return equals(x, y);
}

readonly Func<T, int> getHashCode;
public int GetHashCode(T obj)
{
return getHashCode(obj);
}
}

This worked fine. I could finally just set my comparer directly in my LINQ command. All i had to give was the expression to be used in the Equals and GetHashCode methods.



IEnumerable<mytype> result = collection.Distinct(new GenericComparer<mytype>(((mt1, mt2) => mt1.id == mt2.id), (mt => mt.id.GetHashCode())));

Still, it was more code than I would like to see. Since all I want is to receive all unique elements based on a single expression then that's all i should have to write. A simple tweak to the GenericComparer class and I got a simpler comparer SimpleGenericComparer<T>.

public class SimpleGenericComparer<T> : IEqualityComparer<T>
{
public SimpleGenericComparer(Func<T, int> getHashCode)
{
this.getHashCode = getHashCode;
}

public bool Equals(T x, T y)
{
return getHashCode(x) == getHashCode(y);
}

readonly Func<T, int> getHashCode;
public int GetHashCode(T obj)
{
return getHashCode(obj);
}
}
Having the Equals method simply compare both objects with the GetHashCode expression i could finaly just give the expression with which the comparison will be made.

IEnumerable<mytype> result = collection.Distinct(new SimpleGenericComparer<mytype>(mt => mt.id.GetHashCode());

25 April, 2010

First post

Hello Blog World.

This is my first blog and consequently my first blog post.

Never been much of a writer and never had a reason to start a blog but lately I've found that I couldn't easily find answers to some of the questions i had. After some time I ended up with what i needed but if I had just found it earlier i could have met the deadline. So this blog is just a repository for those answers so that you may be on time.

This will be a software development blog so if you're not a developer this may not be your cup of tea.

I will focus on .NET, C# , Silverlight, WCF (my specialties so far) and hopefully in the near future Mobile platforms (iPhone, Android and Windows Phone 7).

This first blog post was just an overview of the subjects I will be writing about.
From now on just technical stuff.

See ya later.