当前位置 : 主页 > 网页制作 > Nodejs >

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

来源:互联网 收集:自由互联 发布时间:2021-06-24
1.打开VS,新建网站,点击“ASP.NETWeb服务” 2.找到Service.asmx文件,查看代码,编写你想要的方法: using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.Services;[Web

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的简单应用

网友评论