Webservice简单案例

东西不用,时间长了就会被忘掉。重新拾起来

做一个简单的Demo,便于以后的查询

服务器端--新建Calculator.asmx

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;

namespace WebServiceDemo
{
    /// <summary>
    /// Calculator 的摘要说明
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。
    // [System.Web.Script.Services.ScriptService]
    public class Calculator : System.Web.Services.WebService
    {

        [WebMethod]
        public int Add(int a,int b)
        {
            return a + b;
        }
        [WebMethod]
        public int Sub(int a, int b)
        {
            return a - b;

        }
        [WebMethod]
        public int Mul(int a, int b)
        {
            return a * b;
        }

        [WebMethod]
        public int Div(int a, int b)
        {
            try
            {
                return a / b;
            }
            catch 
            {
                return 0;
            }
        }

    }
}

 

客户端调用

增加Web引用 http://localhost:21901/Calculator.asmx

            localhost.Calculator client = new localhost.Calculator();     //localhost为增加的web时起的名称
            client.Add(1, 2);

 

posted @ 2014-03-11 20:43  尼姑哪里跑  阅读(294)  评论(0编辑  收藏  举报