Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

How to return complex types like Generics(List<KeyValuePair<string,string>> or Dictionary<string,string>) from WebMethod

Posted on 2007-11-28
8
Medium Priority
?
3,236 Views
Last Modified: 2013-11-19
I am currently working on ASP.NET Web Services and I want to return complex types like System.Collection.Generic class. I tried to return Dictionary<string,string> but this gave me exception. then I tried to return List<KeyValuePair<string,string> objKeyValuePair> but it didnt solve my problem. Can you tell me how can I return complex types. I dont want to serialize my data as this is not my requirement. Thanks in advance for any help or suggestion.
0
Comment
Question by:anwar ul haque
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
8 Comments
 
LVL 21

Expert Comment

by:MogalManic
ID: 20364708
Serialization IS a requirement for Web Services.  Every Web method is converted to XML to send across the wire.  So all of the classes have to be able to be represented as XML strings.  The only solution is to create serializable adapters that take the generic class and convert it into something that can be turned into XML strings.  For example This would be an adapter for a KeyValuePair<string, string>:
[Serializable]
public class KeyValuePairSerializable
{
 
     public KeyValuePairSerializable()
}

Open in new window

0
 
LVL 7

Expert Comment

by:Infinite_Recursion
ID: 20364715
As far as I can tell, response of a web service is always in XML, that is why they are called XML Web Services, that means types that are to be returned have to be serializable.
0
 
LVL 21

Expert Comment

by:MogalManic
ID: 20364730
Sorry submitted by mistake.  Here is it again:

Note:You could try to make it a Generic class so that it would work with all types of KeyValuePair<t1, t2>.  I'm not sure how it would work though.
[Serializable]
public class KeyValuePairSerializable
{
     private string _key,_value
     public KeyValuePairSerializable()  //Required for serialization
     {
     }
     public KeyValuePairSerializable(KeyValuePair<string, string> value)
     {
        this._key=value.Key;
        this._value=value.Value;
     }
 
     public string Key {
       get {return _key;}
       set {_key=value;}
     }
     public string Value {
       get {return _value;}
       set {_value=value;}
     }
     
}

Open in new window

0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 21

Expert Comment

by:MogalManic
ID: 20364867
I just did a test and List<T> is serializable, but I could not get List<KeyValuePair<string,string>> to work.  Dictionary<string,string> is NOT XML serializable because it implements IDictionary.  But I got List<KeyValuePairSerializable> to work.
    [WebMethod]
    public List<KeyValuePairSerializable> HelloWorld()
    {
        Dictionary<string, string> hello = new Dictionary<string, string>();
        hello.Add("hello", "world");
        hello.Add("world", "hello");
 
        List<KeyValuePairSerializable> list=new List<KeyValuePairSerializable>();
        foreach (KeyValuePair<string, string> item in hello)
        {
            list.Add(new KeyValuePairSerializable(item));
        }
        return list;
    }

Open in new window

0
 
LVL 2

Author Comment

by:anwar ul haque
ID: 20365516
How can I use this class within my web service. My method's return value is List<KeyValuePair<string,string>>.
0
 
LVL 21

Accepted Solution

by:
MogalManic earned 2000 total points
ID: 20365651
You will have to either create a new method or change the signature so that it returns List<KeyValuePairSerializable>.  Just like I did in my HelloWorld webmethod
0
 
LVL 2

Author Comment

by:anwar ul haque
ID: 20366253
Thank you mates, finally I got desired results.
0
 
LVL 2

Author Comment

by:anwar ul haque
ID: 20366332
while I was searching for the solution I could not find the answer so that's why I asked my question here.For me points are not issue I got solution and I'm very happy and thakful to these gentlemen.

Thanks.
0

Featured Post

Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Preface This is the third article about the EE Collaborative Login Project. A Better Website Login System (http://www.experts-exchange.com/A_2902.html) introduces the Login System and shows how to implement a login page. The EE Collaborative Logi…
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Suggested Courses

618 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question