To get a generic collection from WebService

Source: Internet
Author: User

1. Build the model layer first:

Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Text;

Namespace Air.model
{
public class city
{
private int _cityid;

public int Cityid
{
get {return _cityid;}
set {_cityid = value;}
}
private string _cityname;

public string CityName
{
get {return _cityname;}
set {_cityname = value;}
}
private string _districtcode;

public string Districtcode
{
get {return _districtcode;}
set {_districtcode = value;}
}
}
}

2.. Webservice Reflection method

Using System;
Using System.Data;
Using System.Linq;
Using System.Web;
Using System.Collections;
Using System.Web.Services;

Using System.Xml;
Using System.Xml.Schema;
Using System.Xml.Serialization;

Using System.Collections.Generic;
Using Air.model;


Namespace Air.airservice
{

[WebService (Namespace = "http://tempuri.org/")]
[WebServiceBinding (ConformsTo = wsiprofiles.basicprofile1_1)]
[ToolboxItem (False)]

public class AirService:System.Web.Services.WebService
{
[WebMethod]
Public list<city> Getcitys ()
{
List<city> citys = new list<city> ();
return citys;
}

}

3. Referencing Web Services

Select Web Services in this solution if used for the same project

If the local call is selected, Web services on the machine

If the remote call hangs in the browse UDDI Servers on the local network

Select the Web service you want

Change the Web resource name and click Add Web Reference

4. Calling Web Methods

public void DataBind () {
Airservice.airservice airservice = new Air.AirService.AirService ();
list<city> DataSource = Airservice.getcitys ();
}

As a method of returning a generic collection for WebService, it should be noted that WebService can only return a type and not return an interface, and an object error cannot be serialized if ilist<t> is used.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.