Click here to Skip to main content
Click here to Skip to main content

Pass Dynamic List of Parameters to Web Service

, 21 May 2010 CPOL
Rate this:
Please Sign up or sign in to vote.
Make interfacing with your web service simpler and more maintainable
I'm working on a Silverlight 3 project, and I have to connect to a database through a web service. One of the aspects of this web service is that there is a single common method that accepts the name of the stored proc to execute, and one or more parameters of various types.
 
Some things I learned along the way to figuring out my not-so-elegant solution:
 
0) Silverlight doesn't support the Serialize attribute.
 
1) A web service method can't accept generic lists of objects (nor apparently an array of object objects).
 
After playing with this for a couple of days, I came up with the following solution. The short version is that I created a xml string in the client, and parse it in the web service. To convert to a xml string, I did this:
 
//--------------------------------------------------------------------------------
public static XElement MakeXmlParameter(string name, object value)
{
    XElement element = new XElement("Parameter",
                                     new XElement("Name", name),
                                     new XElement("Type", value.GetType()),
                                     new XElement("Value", value));
    return element;
}
 
And in the service, I have this method:
 
public class Parameters :List<SqlParameter>
{
}
 

//-------------------------------------------------------------------------------------
private Parameters MakeSqlParameters(string xmlData)
{
    Parameters parameters = null;
    if (string.IsNullOrEmpty(xmlData))
    {
	XDocument  xDoc       = XDocument.Parse(xmlData);
	XElement   root       = xDoc.Element("Parameters");
	if (root.HasElements)
	{
            parameters = new Parameters();
	    IEnumerable<xelement> children = (from item in root.Elements() select item);
	    foreach (XElement value in children)
	    {
		string paramName       = value.Element("Name").Value;
		string typeName        = value.Element("Type").Value;
		object obj             = Convert.ChangeType(value.Element("Value").Value, 
                                                            Type.GetType(typeName));
		SqlParameter parameter = new SqlParameter(paramName, obj);
		parameters.Add(parameter);
            }
        }
    }
    return parameters;
}</xelement>
 
There's probably a better way to do this given the project architecture, but I needed to get something working fast.
 
Side benefit - notice the call to the Convert.ChangeType method, I searched google for several hours before finding out that you could convert to a named type this way. Look at how much time I saved you from having to do the same google search! Smile | :)
 
EDIT===========================
 
You can make the MakeSqlParameters() method more generic by simply retrieving the first element before looping through all of the child elements. That way, it doesn't matter what they called the root node.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

John Simmons / outlaw programmer
Software Developer (Senior)
United States United States
I've been paid as a programmer since 1982 with experience in Pascal, and C++ (both self-taught), and began writing Windows programs in 1991 using Visual C++ and MFC. In the 2nd half of 2007, I started writing C# Windows Forms and ASP.Net applications, and have since done WPF, Silverlight, WCF, web services, and Windows services.
 
My weakest point is that my moments of clarity are too brief to hold a meaningful conversation that requires more than 30 seconds to complete. Thankfully, grunts of agreement are all that is required to conduct most discussions without committing to any particular belief system.

Comments and Discussions

 
Questionweb service PinmemberMember 1105298115-Sep-14 2:45 
GeneralSome cool observations. Thanks. PinmvpAbhinav S9-May-10 1:33 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.150327.1 | Last Updated 21 May 2010
Article Copyright 2010 by John Simmons / outlaw programmer
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid