Step by Step 建立一個 Web Service

來源:互聯網
上載者:User

標籤:led   cprofile   called   pac   gif   net   add   images   service   

(一)建立Web Service

建立第一個項目。類型選擇ASP.NET Empty Web Application

 

 

加入一個新項目 Web Service

 

 

然後再建立一個類Contact

 

代碼分別例如以下。

Contact.cs:

 

    [Serializable]    public class Contact    {        private string name;        public string Name        {            get { return name; }            set { name = value; }        }        private int age;        public int Age        {            get { return age; }            set { age = value; }        }    }

 

HelloWebService.asmx.cs:

 

    /// <summary>    /// Summary description for HelloWebService    /// </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 HelloWebService : System.Web.Services.WebService    {        [WebMethod]        public string GetMessage(string name)        {            return "Hello " + name;        }        [WebMethod]        public Guid CreateContact(Contact c)        {            return Guid.NewGuid();        }    }

 

 

(二)建立client

以下建立一個client調用Web Service,檢驗一下是否正確。建立一個ASP.NET Empty Web Application

 

 

加入服務引用

 

 

 

WebForm1.cs代碼為

 

        protected void Button1_Click(object sender, EventArgs e)        {            HelloWebService.HelloWebServiceSoapClient client = new HelloWebService.HelloWebServiceSoapClient();            Label1.Text = client.GetMessage(TextBox1.Text);        }        protected void Button2_Click(object sender, EventArgs e)        {            HelloWebService.HelloWebServiceSoapClient client = new HelloWebService.HelloWebServiceSoapClient();            Label2.Text = client.CreateContact(new HelloWebService.Contact()).ToString();        }

 

最後執行client,分別點擊button,得到示範效果

 

Step by Step 建立一個 Web Service

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.