Locked Returning List<> of my Data type in Webserivce

  • Friday, June 15, 2012 10:05 PM
     
     

    I'm using web service on Asp.net 3.5. 

    I want to return list of my data type when the method call from client application 

    what shall I do? 


    binyam

All Replies

  • Tuesday, June 19, 2012 4:43 AM
     
      Has Code

    If you are using WCF service you can just try something like this:

    /// <summary>
            /// Get Entity list.
            /// </summary>
            /// <param name="EntityName">Name of the entity.</param>
            /// <returns></returns>
            [OperationContract]
            [WebGet(UriTemplate = "/Entity/{EntityName}")]
            IList<Entity> GetMyEntity(string EntityName);


    Best Regards Sanjay Pant [Metadesign Solutions]


    • Edited by Sanjay Pant Tuesday, June 19, 2012 4:43 AM
    •  
  • Tuesday, June 19, 2012 5:53 AM
     
     
    I am not using WCF, just asp.net 3.5 web service!

    binyam

  • Wednesday, June 20, 2012 1:51 PM
     
     Proposed Answer Has Code

    That's somehow easy and somehow difficult to answer. If you want to return a list of a certain datatype by the webService the method, that encapsulates your logic simply needs this list as the return type:

    /// <summary>
        /// Summary description for WebService1
        /// </summary>
        [WebService(Namespace = "http://tempuri.org/")]
        [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
        [System.ComponentModel.ToolboxItem(false)]
        // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
        // [System.Web.Script.Services.ScriptService]
        public class WebService1 : System.Web.Services.WebService
        {
    
            [WebMethod]
            public List<YourDataType> HelloWorld()
            {
                List<YourDataType> resultList = new List<YourDataType>();
                
                // logic to be implemented, for example the creation of elements of type "YourDataType" or their retreival from a Database or whatever
                
                return resultList;
            }
        }

    The probably more difficult part is, what needs to be implemented inside your client application, to make is effectively use this list. Since, your question is not more precise, I guess, that's what you wanted to know. Basically this is not different from writing simple methods within your client application. You should just consider, that the standard test page might not be generated by the server,hosting the service, because it does not give back a simple data type. It is definetively this case, if you have non-primitive Input-variables. Maybe the test page will be available, but the result page will look strange or at least unformatted.

    Best regards!

    Peter