加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

Webservice的简单应用(wsdl的获取)

发布时间:2020-12-16 22:07:51 所属栏目:安全 来源:网络整理
导读:1.打开VS,新建网站,点击“ASP.NETWeb服务” 2.找到Service.asmx文件,查看代码,编写你想要的方法: using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.Services;[WebService(Namespace = "http://temp

1.打开VS,新建网站,点击“ASP.NETWeb服务”


2.找到Service.asmx文件,查看代码,编写你想要的方法:

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

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。 
// [System.Web.Script.Services.ScriptService]

public class Service : System.Web.Services.WebService
{
    public Service()
    {

        //如果使用设计的组件,请取消注释以下行 
        //InitializeComponent(); 
    }

    [WebMethod]
    public string HelloWebservice()
    {
        return "Welcome to come Webservice";
    }

    [WebMethod(Description = "加法")]
    public int GetAddResult(int a,int b)
    {
        return a + b;
    }

    [WebMethod(Description = "减法")]
    public int GetSubResult(int a,int b)
    {
        return a - b;
    }

    [WebMethod(Description = "乘法")]
    public int GetMultiResult(int a,int b)
    {
        return a * b;
    }

    [WebMethod(Description = "除法")]
    public int GetDevResult(int a,int b)
    {
        return a / b;
    }
}

3.新建一个项目,添加Webservice的服务引用:


4.在这个项目中,调用Webservice的接口,进行操作:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WebServiceApp
{
    public partial class Form1 : Form
    {
        WebService.ServiceSoapClient webClient;
        public Form1()
        {
            InitializeComponent();
            Loading();
        }

        void Loading()
        {
            webClient = new WebService.ServiceSoapClient();//Webservice调用的类
            btnClick.Click -= new EventHandler(btnClick_Click);
            btnClick.Click += new EventHandler(btnClick_Click);
            cbbControl.DataSource = new object[] { "加法","减法","乘法","除法" };
            cbbControl.SelectedIndex = 0;
            lbTitle.Text = webClient.HelloWebservice();
        }

        void btnClick_Click(object sender,EventArgs e)
        {
            try
            {
                int result = 0;
                int paraA = 0;
                int paraB = 0;
                string control = cbbControl.Text;

                if (!int.TryParse(tbParaA.Text,out paraA) || !int.TryParse(tbParaB.Text,out paraB))
                {
                    MessageBox.Show("请输入整数!");
                    return;
                }

                switch (control)
                {
                    case "加法":
                        result = paraA + paraB;
                        break;
                    case "减法":
                        result = paraA - paraB;
                        break;
                    case "乘法":
                        result = paraA * paraB;
                        break;
                    case "除法":
                        result = paraA / paraB;
                        break;
                    default:
                        break;
                }

                tbResult.Text = result.ToString();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }
}

5.运行程序:


6.获取WSDL文件,在Webservice服务地址后面加上"?wsdl"即可获取:





源代码的获取地址:

Webservice的简单应用

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读