Deserializing a generic list in WCF throws an exception?
-
Monday, September 15, 2008 1:32 PM
I'm new to WCF and immediately ran into some problems. Most of them I could resolve rather quickly, but there is one that sticks.
I have a WCF services and a WPF client requesting a generic list of it.List<AccountDTO> dtoList = client.GetAccounts(); if (dtoList != null) testView.ItemsSource = dtoList;
When I try this same trick with only one AccountDTO it works fine, however when I try to transfer a collection I get an exception which I don't know how to solve:
The formatter threw an exception while trying to deserialize the message: There was an error while trying to deserialize parameter http://tempuri.org/:GetAccountsResult. The InnerException message was 'The get-only collection of type 'System.Collections.Generic.List`1[[System.String, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]' returned a null value. The input stream contains collection items which cannot be added if the instance is null. Consider initializing the collection either in the constructor of the the object or in the getter.'. Please see InnerException for more details.
Server stack trace:
at System.ServiceModel.Dispatcher.DataContractSerializerOperationFormatter.DeserializeParameterPart(XmlDictionaryReader reader, PartInfo part, Boolean isRequest)
at System.ServiceModel.Dispatcher.DataContractSerializerOperationFormatter.DeserializeParameter(XmlDictionaryReader reader, PartInfo part, Boolean isRequest)
at System.ServiceModel.Dispatcher.DataContractSerializerOperationFormatter.DeserializeBody(XmlDictionaryReader reader, MessageVersion version, String action, MessageDescription messageDescription, Object[] parameters, Boolean isRequest)
at System.ServiceModel.Dispatcher.OperationFormatter.DeserializeBodyContents(Message message, Object[] parameters, Boolean isRequest)
at System.ServiceModel.Dispatcher.OperationFormatter.DeserializeReply(Message message, Object[] parameters)
at System.ServiceModel.Dispatcher.ProxyOperationRuntime.AfterReply(ProxyRpc& rpc)
at System.ServiceModel.Channels.ServiceChannel.HandleReply(ProxyOperationRuntime operation, ProxyRpc& rpc)
at System.ServiceModel.Channels.ServiceChannel.Call(String action, Boolean oneway, ProxyOperationRuntime operation, Object[] ins, Object[] outs, TimeSpan timeout)
at System.ServiceModel.Channels.ServiceChannel.Call(String action, Boolean oneway, ProxyOperationRuntime operation, Object[] ins, Object[] outs)
at System.ServiceModel.Channels.ServiceChannelProxy.InvokeService(IMethodCallMessage methodCall, ProxyOperationRuntime operation)
at System.ServiceModel.Channels.ServiceChannelProxy.Invoke(IMessage message)
I've tried all types of bindings and many different approaches, but I can't solve it.
This app is work related and so I'm really stuck. Could anyone please help me. I think you might need more info, but I don't know what, so if you need it just ask!
Answers
-
Monday, September 15, 2008 1:46 PM
Hi,
how you generate the proxy? Per Default WCF just serialize native data Types (Like int, string etc.). If u want to serialize complex Data Types like List<T> you can use svcutil flags for it (automated proxy generation). Here is an example:
svcutil.exe /out: ServiceProxy.cs http://localhost:8001/BusinessLayer/Service /ct: System.Collections.Generic.List`1
By adding /ct (For Collection Type) for List you can tell WCF to serialize that complex Data Type too.
jayan
-
Thursday, September 18, 2008 2:03 PM
Well it seemed it was a number of issues. The object where not correctly build and apparently there where to many complex objects being send via the connection at once, even though I only send 253 objects
It's quite vague if I say so myself. Thanks for your help!
All Replies
-
Monday, September 15, 2008 1:46 PM
Hi,
how you generate the proxy? Per Default WCF just serialize native data Types (Like int, string etc.). If u want to serialize complex Data Types like List<T> you can use svcutil flags for it (automated proxy generation). Here is an example:
svcutil.exe /out: ServiceProxy.cs http://localhost:8001/BusinessLayer/Service /ct: System.Collections.Generic.List`1
By adding /ct (For Collection Type) for List you can tell WCF to serialize that complex Data Type too.
jayan
-
Monday, September 15, 2008 2:10 PM
Thanks for the quick reply!
I have a host application that hosts all my services and I let VS2008 auto generate the proxy. I've configured the proxy to use at Data Type: Collection type: System.Collections.Generic.List but that doesn't make a difference. I also already gave the service contract the [ServiceKnownType(typeof(AccountDTO))] attribute, which was necessary!
The services will eventually run as windows services though, but I need this to work during development.
yoni
-
Thursday, September 18, 2008 2:03 PM
Well it seemed it was a number of issues. The object where not correctly build and apparently there where to many complex objects being send via the connection at once, even though I only send 253 objects
It's quite vague if I say so myself. Thanks for your help! -
Sunday, November 02, 2008 2:53 AM
Hi yoniathome
" 'The get-only collection of type 'System.Collections.Generic.List`1"
It looks like your property is readonly.. How deserializer would set the deserialized value back to property?
Implement get-set property and it will work.
Thanks,
Jay -
Thursday, January 29, 2009 3:05 AMyoniathome said:
Well it seemed it was a number of issues. The object where not correctly build and apparently there where to many complex objects being send via the connection at once, even though I only send 253 objects
It's quite vague if I say so myself. Thanks for your help!
What was the solution? I'm facing the same problem and don't see why i'm getting this error. The type has a get and set property for that collection as well as it is initialized in the constructor. -
Thursday, January 29, 2009 7:44 AM
Hey George,
Could you please paste your code here?
Thanks,
JK
-
Monday, June 15, 2009 8:41 PMHi ,
I have cteated one WCF service, method is
IEmployee
GetListUserInfo(List<IEmployee > UserInfo)
i am calling this method from Client
ServiceClient
serviceClient1 = new ServiceClient ();
List
<IEmployee > ListUser = new List<IEmployee >();
ListUser = (
List<IEmployee >)serviceClient1 .GetListUserInfo(ListUser);List<IEmployee >)serviceClient1 .GetListUserInfo(ListUser);
// here i am geting following error
cannot convert from 'System.Collections.Generic.List<IEmployee >'
to 'System.Collections.Generic.List<object>'
please give suggestions.
Murali Krishna.Konanki

