Passing Dictionary in WebService
Passing Dictionary in WebService
I tried to pass Dictionary as a return value of a webservice
Here's a snippet of the code:
//I marked BL.Customer class as serializable.
[Webmethod]
public Dictionary<string, BL.Customer> GetAllCustomer()
{
Dictionary<string, BL.Customer> customers = new Dictionary<string, BL.Customer>();
customers.Add("1", new BL.Customer("John"));
customers.Add("2", new BL.Customer("Jane"));
return customers;
}
When I view the asmx file on the browser, I got this error:
The type System.Collections.Generic.Dictionary`2[[System.String, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[BL.Customer, BL, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]] is not supported because it implements IDictionary.
I need to pass a collection of Customer class (in this example) that support search, get object by key. Anybody can help me solve this issue or have any suggestion of other alternatives
Passing Dictionary in WebService
KasiSunkara
So a possible schema would be
<XML>
<element key="foo" value="bar"/>
</XML>
With an element for every key/value pair. Then you can rebuild it on the other end.
ChindhiChor
I guess that's my solution for now unless somebody else know how to pass dictionary through webservice.
Thanks for your reply.
Sanjy
Loosely related, what type can I use in an ASMX web service method to *pass* a collection of name-value pairs to the ASMX web service
I have tried using a parameter datatype based on CollectionBase but in the client WSDL reference.cs stub created by Visual Studio, the CollectionBase parameter is reduced to an
FooType[] colMyCollection
Michael.
(Note the previous postings to this thread with returning a collection/array ...I want to go in the opposite direction and pass a collection to my ASMX web service.)
DonovanH
Vijay0638
can you elaborate more about this alternative
Damir Tomicic
To be honest, I have no idea why you can return an arraylist and not a dictionary, I'd have expected that you could, I've just been trying to think of an alternative, based on your saying that you couldn't :-)
Converting an arraylist into a dictionary is probably less work than parsing it through XML, I would have thought.
Thomas Pagel
I am passing a collection of objects of Customer that have members like first name, last name, id, etc, so that solution won't work in my case.
I can return an ArrayList of Customers from the webservice but then I have to convert it to Dictionary on the client side to have a better search performance. It seems to me like an extra work. So I was wondering if I can just return Dictionary<> datatype from webservice.
Elvia
I found a really good article about this.
http://www.aspzone.com/blogs/john/articles/167.aspx
I think you can use it in your case. Basically what you have to do is to implement your own serialization methods.
Hope this helps.
Devi
JTDSpoons
ASMX web services do not support types that implement IDictionary since V1. This is by design and was initially done since key-value constraints could not be appropriately expressed in schema.
As discussed earlier in the thread as discussed earlier in the thread, in order to send a dictionary type, you can implement logic to convert the dictionary type to an array or List or ArrayList. Consider using a public property where the getter returns the dictionary items in a list and the setter takes a list and stores it as a dictionary internally.
Thanks,
Natasha Jethanandani
Developer, Microsoft