WCF over TCPIP Serialization Exception while sending dictionary in a dictionary
-
Hi, I have created two programs to test WCF using TCPIP binding. One program is acting as a client and other is acting as a server. Server has one function which is something like /////////////////////////////////////////////////////////// public bool Func(out Dictionary > response,Dictionary > request) { return true; } /////////////////////////////////////////////////////////// The client on the other hand has following code for using this function /////////////////////////////////////////////////////////// InstanceContext ct = new InstanceContext(new Program()); //generated proxy KineticServiceClient client = new KineticServiceClient(ct); Dictionary> response = null; Dictionary> request = new Dictionary>(); List lst= new List(); lst.Add("hello"); lst.Add(23); /* Dictionary dict = new Dictionary(); dict.Add("1", "10"); dict.Add("2", "10"); dict.Add("3", "10"); lst.Add(dict); */ request.Add("test1",lst); client.Func(out response, request); //////////////////////////////////////////////////////////////////// Everything works fine until I uncomment above commented code in client program. Then I start getting following exception "There was an error while trying to serialize parameter Avanza.Kinetic.Terminal.Communication:request. The InnerException message was 'Type 'System.Collections.Generic.Dictionary`2[[System.String, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[System.String, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]' with data contract name 'ArrayOfKeyValueOfstringstring:http://schemas.microsoft.com/2003/10/Serialization/Arrays' 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.'. Please see InnerException for more details." I've got no idea why it is happening. Can any body help??
-
Hi, I have created two programs to test WCF using TCPIP binding. One program is acting as a client and other is acting as a server. Server has one function which is something like /////////////////////////////////////////////////////////// public bool Func(out Dictionary > response,Dictionary > request) { return true; } /////////////////////////////////////////////////////////// The client on the other hand has following code for using this function /////////////////////////////////////////////////////////// InstanceContext ct = new InstanceContext(new Program()); //generated proxy KineticServiceClient client = new KineticServiceClient(ct); Dictionary> response = null; Dictionary> request = new Dictionary>(); List lst= new List(); lst.Add("hello"); lst.Add(23); /* Dictionary dict = new Dictionary(); dict.Add("1", "10"); dict.Add("2", "10"); dict.Add("3", "10"); lst.Add(dict); */ request.Add("test1",lst); client.Func(out response, request); //////////////////////////////////////////////////////////////////// Everything works fine until I uncomment above commented code in client program. Then I start getting following exception "There was an error while trying to serialize parameter Avanza.Kinetic.Terminal.Communication:request. The InnerException message was 'Type 'System.Collections.Generic.Dictionary`2[[System.String, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[System.String, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]' with data contract name 'ArrayOfKeyValueOfstringstring:http://schemas.microsoft.com/2003/10/Serialization/Arrays' 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.'. Please see InnerException for more details." I've got no idea why it is happening. Can any body help??
Are you using the Dictionary<(Of <(TKey, TValue>)>) Class? If so, I'm not seeing that in your code posted - can you fix that?
Mark Salsbery Microsoft MVP - Visual C++ :java:
-
Are you using the Dictionary<(Of <(TKey, TValue>)>) Class? If so, I'm not seeing that in your code posted - can you fix that?
Mark Salsbery Microsoft MVP - Visual C++ :java:
Sorry please find the correct client code which gives exception /////////////////////////////////////////////////////////// InstanceContext ct = new InstanceContext(new Program()); //generated proxy KineticServiceClient client = new KineticServiceClient(ct); Dictionary response = null; Dictionary> request = new Dictionary>(); List lst= new List(); lst.Add("hello"); lst.Add(23); /* Dictionary dict = new Dictionary(string,string>); dict.Add("1", "10"); dict.Add("2", "10"); dict.Add("3", "10"); lst.Add(dict); */ request.Add("test1",lst); client.Func(out response, request);
-
Hi, I have created two programs to test WCF using TCPIP binding. One program is acting as a client and other is acting as a server. Server has one function which is something like /////////////////////////////////////////////////////////// public bool Func(out Dictionary > response,Dictionary > request) { return true; } /////////////////////////////////////////////////////////// The client on the other hand has following code for using this function /////////////////////////////////////////////////////////// InstanceContext ct = new InstanceContext(new Program()); //generated proxy KineticServiceClient client = new KineticServiceClient(ct); Dictionary> response = null; Dictionary> request = new Dictionary>(); List lst= new List(); lst.Add("hello"); lst.Add(23); /* Dictionary dict = new Dictionary(); dict.Add("1", "10"); dict.Add("2", "10"); dict.Add("3", "10"); lst.Add(dict); */ request.Add("test1",lst); client.Func(out response, request); //////////////////////////////////////////////////////////////////// Everything works fine until I uncomment above commented code in client program. Then I start getting following exception "There was an error while trying to serialize parameter Avanza.Kinetic.Terminal.Communication:request. The InnerException message was 'Type 'System.Collections.Generic.Dictionary`2[[System.String, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[System.String, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]' with data contract name 'ArrayOfKeyValueOfstringstring:http://schemas.microsoft.com/2003/10/Serialization/Arrays' 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.'. Please see InnerException for more details." I've got no idea why it is happening. Can any body help??
Hi, It looks like you've run across exactly the same problem (although the objects are different) that I encountered earlier this week. Hopefully I can help you out, or point you in the right direction at least. As the error shows, the problem happens when DataContractSerializer tries to serialize the Dictionary object. It does also point you in the right direction as to how to fix this. There are two ways (that I know of to fix this issue). The one is to apply the KnownTypeAttribute to the DataContract with the type you want the DataContractSerializer to know about, in normally it would look something like this: [KnownType(typeof(MyType))]. Sadly, in your case as you are working with generic types, you will probably have a bit more work than that by the looks of the documentation (See the MSDN page for the KnownTypeAttribute). The other way around it (the method I used), is to put the known type in your application config file. Basically what you will do is something like this (haven't tried it with your generic types, but this should point you in the right direction):
<system.runtime.serialization>
<dataContractSerializer>
<declaredTypes>
<add type = "Reporting.Contracts.Data.ReportData, Reporting.Contracts, Version=1.0.0.0, Culture=neutral, PublicKeyToken=312e149fdc68a08f">
<knownType type = "Reporting.Contracts.Data.NewTest,Reporting.Contracts, Version=1.0.0.0, Culture=neutral, PublicKeyToken=312e149fdc68a08f"/>
</add>
</declaredTypes>
</dataContractSerializer>
</system.runtime.serialization>Your dictionary would go in the line
<knownType type = "Reporting.Contracts.Data.NewTest,Reporting.Contracts, Version=1.0.0.0, Culture=neutral, PublicKeyToken=312e149fdc68a08f"/>
I think (not 100% sure on this though), that your List'1 would go on the line that looks like:<add type = "Reporting.Contracts.Data.ReportData, Reporting.Contracts, Version=1.0.0.0, Culture=neutral, PublicKeyToken=312e149fdc68a08f">