There was an error while trying to serialize parameter http://tempuri.org/:obj. The InnerException message was ‘Type ‘System.Collections.Generic.List`1[Employee, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]’ with data contract name ‘ArrayOfEmployee:http://schemas.datacontract.org/2004/07/Interfaces’ is not expected. Add any types not known statically to the list of known types – for example, by using the KnownTypeAttribute attribute or by adding them to the list of known types passed to DataContractSerializer.

Hi if you are getting your hands dirty with WCF and got your mind blown up with the error as shown above or shown in Screen i have medicine of it..so go ahead!!!!

image

I am creating one simple serviceoperation which should take argument as an object but i will pass instance of my generic list which can be like List<Employee> or List<Person> and all this….because in service implementation i will do further operation based on type….I have created one application like this:

IService.cs

image

Service.CS

image

Employee.CS

image

Client.CS

image

Now when i run this application it gives me error of KnownTypeAttribute and serialization

Cause:

Actually root cause of problem is that we have defined object as our datacontract but when we pass List<Employee> or List<Person> it dosen’t knows what to serialize and send from wire…..hoohh…….simple if i have told you to say HI and it i will sat Bye…you can’t understand what i mean to say…so i need to help you in understanding Bye…Simple!!!!!so let us tell CLR that hey what to do if i pass List<Employee> or any type which i want to pass..

Solution:

Open your service contract IService.cs

and after [ServiceContract] Add [ServiceKnownType(typeof(List<Employee>))] or [ServiceKnownType(typeof(List<Person>))]….and Run the application…modified IService.cs looks like this:

image

Hope it helped you…Wants to say..Thanks say it to

Sowmy Srinivasan’s for nice and in-depth article:http://blogs.msdn.com/sowmy/

Question:

I am just wondering suppose if i have 50 Types which i want to pass then i need to declare all this on top of IService….just searching some alternative for it..if i found will let you know and hoping for the same….