Thursday, May 31, 2012

.NET Exceptions Vs Faults

Exception is a .NET mechanism used to communicate problems encountered during the execution of a program. Various .NET languages allows us to throw, catch/handle or possibly ignore exceptions so that they can be further propogated up to the call stack.


While Fault refers to SOAP fault mechanism that transfers erros or fault condition from service to consumer according to SOAP Specifications.

In Windows Communication Foundation, its always very helpful to diagnose problems during development phase.

WCF Service - What is a KnownType?

In Windows Communication Foundation, a KnownType is the acceptable derived class for a given Data Contract.

Consider the following classes which has inheritance relationship as follows:
----------------------------------------------------
[DataContract]
public class UserAccount {}

[DataContract]
public class Admin : UserAccount {}

[DataContract]
public class Guest : UserAccount {}
---------------------------------------------------

Now, look into following code:
---------------------------------------------------
[DataContract]
[KnownType(typeof(Admin))]
[KnownType(typeof(Guest))]
public class SecurityInfo
{
[DataMember]
private UserAccount user;
}
----------------------------------------------------
Above code will work fine, either we set SecurityInfo data member to Admin or Guest.
But if KnownTypeAttribute for Admin and Guest are not provided, deserialization engine will not recognize Admin and Guest types and will cry.

Other WCF Tutorial with Examples: