Monday, March 7, 2011

Return Object From Webservice

How can I retuen a Object from a web service:

[WebMethod] public DataSet GetVendors(string Database) { SqlConnection sqlConn = new SqlConnection();

        sqlConn.ConnectionString = GetConnString(Database);

        // build query
        string strSQL = @"  SELECT      [No_] AS [VendorNo],
                                        LTRIM([Name]) AS [VendorName]

                            FROM        [********_$Vendor]

                            WHERE       LEN(RTRIM([Name])) > 0 /* avoid blank names */

                            AND         [Vendor Posting Group] = 'VEND'

                            ORDER BY    LTRIM([Name]) ASC; /* LTRIM fixes spaces before name */ ";

        SqlDataAdapter da = new SqlDataAdapter(strSQL, sqlConn);

        DataSet ds = new DataSet();

        da.Fill(ds, "Vendors");

        return (ds);
    }
From stackoverflow
  • If I'm interpreting your question properly, populate an object on your end with your information in the DataSet and set your return type to object. Or just return the object you populate as that object.

  • An alternative would be to return the dataset xml as a string and create a dataset from it on the client side.

    Although I'm sure encrypting the object would be fairly straightforward, this approach helped me out when my web services needed encryption (serialize everything, encrypt that string, return string, decrypt, deserialize).

  • If you're asking how to return any kind of object, not an Object, then you're going to need to serialize that object. If you're trying to serialize a DataSet, I would suggest making it into a List or other data structure first.

  • Seems brittle to me. It presumes that the client will know how to deserialize that object, which locks the client into a particular platform (e.g. Java EE or .NET).

    XML serialization is less brittle because it's platform-agnostic and leaves unmarshalling quirks to be resolved by the client. I'd recommend that over returning an object.

  • Another reason not to return DataSet is "leaky abstraction": why expose the client to anything having to do with the persistence tier? Rethink it.

  • IMO returning datasets from your web services is not such a great idea. It assumes that the clients understand the Dataset, DataTable etc data structures and classes. Instead I recommend you use plain CLR object i.e. Data Transfer Objects or if you want you may use XmlNode as your datatype.You can secure your web service using WSE.

0 comments:

Post a Comment