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!!!!
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
Service.CS
Employee.CS
Client.CS
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:
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….
I tried this, but didn’t work
also tried this
http://silverlight.net/forums/p/21513/77019.aspx#77019
no luck
finally i am taking top 100 records.
hi, for me this code don’t work
ERROR:
System.Runtime.Serialization.SerializationException: Type ‘System.Collections.Generic.List`1[[ClientWS.wcfServerService.Employee, ClientWS, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]’ with data contract name ‘ArrayOfEmployee:http://schemas.datacontract.org/2004/07/wcfServer‘ 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.
at System.Runtime.Serialization.XmlObjectSerializerWriteContext.SerializeAndVerifyType(DataContract dataContract, XmlWriterDelegator xmlWriter, Object obj, Boolean verifyKnownType, RuntimeTypeHandle declaredTypeHandle)
at System.Runtime.Serialization.XmlObjectSerializerWriteContext.SerializeWithXsiTypeAtTopLevel(DataContract dataContract, XmlWriterDelegator xmlWriter, Object obj, RuntimeTypeHandle originalDeclaredTypeHandle)
at System.Runtime.Serialization.DataContractSerializer.InternalWriteObjectContent(XmlWriterDelegator writer, Object graph)
at System.Runtime.Serialization.DataContractSerializer.InternalWriteObject(XmlWriterDelegator writer, Object graph)
at System.Runtime.Serialization.XmlObjectSerializer.WriteObjectHandleExceptions(XmlWriterDelegator writer, Object graph)
at System.Runtime.Serialization.XmlObjectSerializer.WriteObject(XmlDictionaryWriter writer, Object graph)
at System.ServiceModel.Dispatcher.DataContractSerializerOperationFormatter.SerializeParameterPart(XmlDictionaryWriter writer, PartInfo part, Object graph)
My code:
Server side – WCF
IService1.cs
namespace wcfServer
{
// NOTE: If you change the interface name "IService1" here, you must also update the reference to "IService1" in Web.config.
[ServiceContract]
[ServiceKnownType(typeof(List))]
public interface IService1
{
[OperationContract]
string ParseObject(object obj);
}
}
Service1.svc.cs
namespace wcfServer
{
// NOTE: If you change the class name "Service1" here, you must also update the reference to "Service1" in Web.config and in the associated .svc file.
public class Service1 : IService1
{
#region Implementation of IService1
public string ParseObject(object obj)
{
List emp = (List) obj;
string empDetalils = string.Format("EmployeeID {0} And Employee Name {1} And GetType is {2}",
emp[0].EmployeeID, emp[0].EmployeeName, obj.GetType());
return empDetalils;
}
#endregion
}
}
Employee.cs
namespace wcfServer
{
[DataContract]
public class Employee
{
[DataMember]
public int EmployeeID { get; set; }
[DataMember]
public string EmployeeName { get; set; }
}
}
Client side:
toWcfServer.cs
namespace ClientWS
{
public class toWcfServer : IService1
{
#region Implementation of IService1
public string ParseObject(object obj)
{
try
{
wcfServerService.Service1Client client = new Service1Client();
string strEmployee = client.ParseObject(obj);
client.Close();
return strEmployee;
}
catch (Exception ex)
{
return ex.InnerException.ToString();
}
}
#endregion
}
}
Form1.cs
private void button1_Click(object sender, EventArgs e)
{
toWcfServer toServer = new toWcfServer();
List listEmployees = new List();
listEmployees.Add(new Employee(){EmployeeID = 10, EmployeeName = "PEPEK"});
textBox1.Text = toServer.ParseObject(listEmployees);
}
what is wrong in the code?
Hi,
Thanks for going through my article.
In your code you just need to change following lines:
IService1.cs
namespace wcfServer
{
// NOTE: If you change the interface name “IService1” here, you must also update the reference to “IService1” in Web.config.
[ServiceContract]
[ServiceKnownType(typeof(Employee))] //Earlier it was List
public interface IService1
{
[OperationContract]
string ParseObject(object obj);
}
}
Because your ServiceMessage contains Employee object which .NET f/w don’t knows….so you just need to introduce your Employee object to .NET f/w and [ServiceKnownType(typeof(Employee))] does the same.
Feel free to get back to me if still it doesn’t work.
Happy Coding,
Kiran
Thanks kiran , Great article it worked for me.
This topic helped me to solve my issue… Thanks
Hey I am so thrilled I found your website, I really found you by accident,
while I was searching on Aol for something else,
Anyways I am here now and would just like to say thanks a lot for a fantastic post and a
all round thrilling blog (I also love the theme/design), I don’t have time to read
it all at the minute but I have saved it and also included your RSS feeds, so when I have time I
will be back to read much more, Please do keep
up the awesome job.