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

Two ways to return complex data from .NET web methods

, 7 Mar 2004
Rate this:
Please Sign up or sign in to vote.
Two ways to return complex data from .net web methods

Why publish an n API to the Internet?

Much like a web page, when you make an API public to the Internet, you allow anyone on the planet to use your web method. Once you post an API you should attempt to keep it up and stable. This does not necessarily mean you grantee how long your API will be available. However, if you wish more application programmers to rely on your API then keep it up and stable. Search engines use this sort of technology to encourage people to automate use of their searching services, some AI Chatbot communities use public API for allowing people to create, train and deploy AI chatbots from anywhere to anywhere in the world, Comic book venders use public APIs to allow anonymous business partners to market their comic books. In short, public APIs make anonymous B2B (Business to Business) relationships more feasible. Not all .NET web methods, however, are used for public APIs, for example, some .NET web methods require passwords or use WSSecurity to limit who can use them. Although non-public API's often use .NET web methods, they are outside the scope of this article.

When to use .NET web methods to publish an API to the Internet

Data can be transferred between remote servants using many architectures. Web methods are faster and easier to implement then writing a socket or .NET remoting server. Web methods more self-documenting then ASPX Request/Response. With web methods, you literally mark your method as a web method and let .NET generate the connectivity mechanism and documentation for accessing the method over the Internet. This way you can quickly get your web method up and running and also benefit from the throttles and security of IIS. The web method approach is not ideal if you cannot afford Microsoft server software because IIS in Windows XP Pro, 2000 pro, etc. has been deliberately disabled to prevent it from being used as a web server on the Internet. If windows 2003 Server, 2000 Server, etc. are in your price range then you will benefit from using .NET web methods. You can, however, practice web method development on any of the .NET enabled Microsoft server and workstation operating systems. If you cannot afford a Microsoft server product then focus on exposing your web methods with .NET socket server architectures, which are just as elegant and expandable but not as ideal for rapid development.

Writing web methods

For this article we focus on two different ways to return complex data types from web methods.

Returning complex data types with .NET serialization/de-serialization

This method just initializes a complex data type with some sample data.

   Private Function getDocument() As SComplex
        Dim l_sComplex As SComplex
        l_sComplex = New SComplex()
        l_sComplex.dblVal = 4.14159
        l_sComplex.iVal = 4
        l_sComplex.m_SChildNode.dblChildVal = 4.14159
        l_sComplex.m_SChildNode.iChildVal = 4
        getDocument = l_sComplex
    End Function

This method returns the complex datatype using .NET serialization/de-serialization. You return the object, structure or array is returned and .NET constructs an XML (eXtensible Markup Language) representation.

    <WebMethod()> Public Function complex() As Scomplex
Get sample complex data
        Dim l_sComplex As SComplex
        l_sComplex = getDocument()

Return complex type to be converted into XML in the Response object by .NET

       complex = l_sComplex
    End Function

Returning complex data types with manual document creation

This method manually constructs an xml document and returns it. .NET simply appends the xml in that you manually construct to ""<?xml version…" and returns it in the HTTP (HyperText Transfer Protocol) response.

    <WebMethod()> Public Function complexManual() As System.Xml.XmlDocument

Get sample complex data

        Dim l_sComplex As SComplex
        l_sComplex = getDocument()

Manually construct an XML representation of the sample complex data

        Dim strData As String
        strData = ""
        strData = strData & _
             "<SComplex xmlns:xsd=""http://www.w3.org/2001/XMLSchema"" " & _
            "xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" " & _ 
             "xmlns=""http://tempuri.org/"">"
        strData = strData & "   <dblVal>" & l_sComplex.dblVal & "</dblVal> "
        strData = strData & " <iVal>" & l_sComplex.iVal & "</iVal> "
        strData = strData & " <m_SChildNode>"
        strData = strData & "  <dblChildVal>" & _
            l_sComplex.m_SChildNode.dblChildVal & "</dblChildVal> "
        strData = strData & "  <iChildVal>" & _
            l_sComplex.m_SChildNode.iChildVal & "</iChildVal> "
        strData = strData & " </m_SChildNode>"
        strData = strData & "</SComplex>"
        Dim pDoc As System.Xml.XmlDocument
        pDoc = New System.Xml.XmlDocument()
Return the manually created XML representation of the sample complex data.
        pDoc.LoadXml(strData)
        complexManual = pDoc
    End Function

What is returned

Although written differently, both complex() and complexManual() return the same XML representation of the the sample complex data:

<?xml version="1.0" encoding="utf-8" ?> 
<SComplex xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  xmlns="http://tempuri.org/">
 <dblVal>4.14159</dblVal> 
 <iVal>4</iVal> 
 <m_SChildNode>
  <dblChildVal>4.14159</dblChildVal> 
  <iChildVal>4</iChildVal> 
 </m_SChildNode>
</SComplex>

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

dzzxyz
Web Developer
United States United States
Keep on coding!

Comments and Discussions

 
Generalplanetapi.com does not exist PinmemberParthasarathy Mandayam23-Oct-07 10:31 
Generalreturning complex data types Pinmembereggertj17-Mar-05 5:18 
GeneralThe worst way of dealing with XML PinsussDaniel Cazzulino [MVP XML]31-May-04 15:30 
GeneralTwo ways to return complex data from .NET web methods PinmemberDaniel Stephen Rule31-May-04 19:29 
GeneralRe: Two ways to return complex data from .NET web methods PinsussDaniel Cazzulino [MVP XML]1-Jun-04 5:18 
GeneralRe: Two ways to return complex data from .NET web methods PinsussAnonymous1-Jun-04 7:41 
GeneralRe: Two ways to return complex data from .NET web methods PinmemberDaniel Stephen Rule1-Jun-04 7:46 
GeneralRe: Two ways to return complex data from .NET web methods PinmemberDaniel Cazzulino [MVP XML]1-Jun-04 8:52 
GeneralRe: Two ways to return complex data from .NET web methods PinmemberDaniel Stephen Rule1-Jun-04 9:48 
GeneralRe: Two ways to return complex data from .NET web methods PinsussAnonymous1-Jun-04 7:43 
GeneralProblem with passing complex types PinmemberMeera Rajaram26-May-04 5:12 
I am building a Web Service which should accept sqlcommand parameters, and the database name. This web service will connect to the database, Run the query and return the results. The first problem I faced was that WebServices cannot accept SQLCommand object as an input parameter(since it is not XML Serializable). I tried to overcome this problem by defining my class which would accept the properties that we set for a parameter(like Name, size, direction, type,etc)
 
For example I have a class
Public class prmParameter
Public strParameterName as string
Public strParameterType as SQlDbType
etc...
Public Sub New()
some initializations...
end sub
Public Sub New(ParameterName as string, ParameterType as SQLDBType)
strParameterName=ParameterName
strParameterType =ParameterType
end sub
End Class

I have created the class in a separate class library and included its reference in my Web Service.
 
IN my webservice I then expose a method which takes an array of prmParameter(since we can have multiple parameters passed to a stored procedure), StoredProcedure or Query name, CommandType(StoredProcedure, Text, TableDirect),DBName.
 
The signature of my method would look like this:

Public Function ExecuteProc(aryParams() as prmParameter, strQuery as string, cmdType as CommandType,strDBName as string) as DataSet
 

The problem that I am facing is that the overloaded constructor does not get exposed at the client end. So for each parameter to be passed, I have to create an instance of prmParameter and individually set its properties. Though this runs fine, it is a very cumbersome process.
 
Is there any way to avoid this? I had tried creating another class which has an array of parameters. I had put in a method in that class for accepting a SQLcommand object and building the PrmParameter array using the SQLCommand Object. But once again, the method did not get exposed at the client end Cry | :((
 
Please help!
 

Generalthanks PinsussAnonymous7-May-04 8:26 
GeneralThanks for sharing! PinsussAnonymous20-Apr-04 10:33 
GeneralI needed this thanks! PinsussAnonymous20-Apr-04 10:33 
GeneralHelp with site Pinmemberdog_spawn10-Apr-04 5:25 
GeneralNice work. PinsussJason L. Tormlage8-Mar-04 15:43 
GeneralWhy publish API's PinmemberMark Focas8-Mar-04 12:52 
GeneralRe: Why publish API's PinsussAnonymous8-Mar-04 15:34 
GeneralRe: Why publish API's Pinmemberandy brummer8-Mar-04 18:50 
GeneralRe: Why publish API's PinsussAnonymous9-Mar-04 6:41 

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 | Mobile
Web03 | 2.8.141015.1 | Last Updated 8 Mar 2004
Article Copyright 2004 by dzzxyz
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid