XML Web Service开发实例——通过Windows Forms调用Web Service
开发工具:Visual Web Developer 2005 Express Edition Beta Visual C# 2005 Express Edition Beta
Service.asmx
<%@ WebService Language="C#" CodeBehind="~/Code/Service.cs" Class="MyServiceCS" %>
Service.cs
using System;
using System.Web;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Web.Services;
using System.Web.Services.Protocols;
[WebServiceBinding(ConformanceClaims=WsiClaims.BP10,EmitConformanceClaims = true)]
public class MyServiceCS : System.Web.Services.WebService
{
public MyServiceCS()
{
}
[WebMethod]
public int AddNumbers(int num1, int num2)
{
return (num1+num2);
}
[WebMethod]
public int SubNumbers(int num1, int num2)
{
return (num1-num2);
}
[WebMethod]
public int MulNumbers(int num1, int num2)
{
return (num1*num2);
}
[WebMethod]
public int DivNumbers(int num1, int num2)
{
return (num1/num2);
}
}
Form1.cs
#region Using directives
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Windows.Forms;
#endregion
namespace ArithCalculatorWebServiceClient
{
partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private MyServiceCS CSService = new MyServiceCS();
private void LblResult_Click(object sender, EventArgs e)
{
int result = CSService.AddNumbers(Int32.Parse(TxtNum1.Text),Int32.Parse(TxtNum2.Text));
LblResult.Text = result.ToString();
}
private void BtnSub_Click(object sender, EventArgs e)
{
int result = CSService.SubNumbers(Int32.Parse(TxtNum1.Text), Int32.Parse(TxtNum2.Text));
LblResult.Text = result.ToString();
}
private void BtnMul_Click(object sender, EventArgs e)
{
int result = CSService.MulNumbers(Int32.Parse(TxtNum1.Text), Int32.Parse(TxtNum2.Text));
LblResult.Text = result.ToString();
}
private void BtnDiv_Click(object sender, EventArgs e)
{
int result = CSService.DivNumbers(Int32.Parse(TxtNum1.Text), Int32.Parse(TxtNum2.Text));
LblResult.Text = result.ToString();
}
private void Form1_Load(object sender, EventArgs e)
{
}
}
}