C# webservice 编写、发布、调用

采用的工具VS2010生成工程

1. 生成webservice工程:建 ASP.NET 空WEB 应用程序。

2. 在建好的ASP.NET 空WEB应用程序中新建项“web 服务”。

完成上述内容工程结构如下图

下面主要的操作就是在webservice1.asmx.cs文件中进行,里面写了几个服务,两个简单服务两个查询数据库服务;

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.Services;

using System.Data.SqlClient;

using System.Data;

using MySql.Data;

using MySql;

using MySql.Data.MySqlClient;

 

namespace webservice

{

    /// <summary>

    /// WebService1 的摘要说明

    /// </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 WebService1 : System.Web.Services.WebService

    {

 

        [WebMethod]

        public string HelloWorld()

        {

            return "Hello World";

        }

 

        [WebMethod]

        public int GetSum(int a, int b)

        {

            return a + b;

        }

 

        [WebMethod]

        public DataSet query()

        {

            DataSet ds = new DataSet();

            string connstring = "Database=test;Data Source=localhost;User Id=root;Password=root";

            MySqlConnection mycn = new MySqlConnection(connstring);

            mycn.Open();

            MySqlDataAdapter mda = new MySqlDataAdapter("select * from stock_data", mycn);

            mda.Fill(ds,"stock_data");           

            return ds;

        }

 

        [WebMethod]

        public DataSet goldprice()

        {

            DataSet ds = new DataSet();

            string connstring = "Database=test;Data Source=localhost;User Id=root;Password=root";

            MySqlConnection mycn = new MySqlConnection(connstring);

            mycn.Open();

            MySqlDataAdapter mda = new MySqlDataAdapter("select * from goldprice", mycn);

            mda.Fill(ds, "stock_data");

            return ds;

        }

 

         

    }

}

 到目前为至C# 的webservice的服务算是写好了。下面我们需要启动些服务F5

运行成功的界面如下图

二:发布此webservice

1. 与发布asp.net是一样的,首先在IIS的网站节点下添加网站;如下图

2. webservice发布到IIS的虚拟目录;选择webservice工程右击点发布进行发布如下图

3. 修改此网站的高级设置: 把应用程序池修改为ASP.NET v4.0即可。

4. 浏览此网站http://192.168.56.1/WebService1.asmx若运行的效果与在VS2010运行的效果一样则部署成功了。

三:webservice服务调用

1. 新建一个测试工程,在工程的引用节点中右击“添加服务引用”把浏览器中的URL填写到地址框中点前往,并修改命名空间;如下图

2. 然后再重新生成一下测试项目,最后在测试项目中定义一个测试方法并调用webservice中的方法。代码如下

1

2

3

4

5

6

7

8

9

10

private void button1_Click(object sender, RoutedEventArgs e)

        {

            YDTF.WebService1SoapClient aa = new YDTF.WebService1SoapClient();

            button1.Content = aa.HelloWorld();

            Menus m = new Menus();

            m.Name = "Name";

            m.NameSpace = "NameSpace";

            DataSet ds = aa.query();

            int i = ds.Tables[0].Rows.Count;

        }

3. 单步调试一下若运行结果和预料一样则成功了。 

来源:网络


智能推荐

C#调用WebService实例和开发

C#调用WebService实例和开发 一、基本概念   Web Service也叫XML Web Service WebService是一种可以接收从Internet或者Intranet上的其它系统中传递过来的请求,轻量级的独立的通讯技术。是:通过SOAP在Web上提供的软件服务,使用WSDL文件进行说明,并通过UDDI进行注册。简单的理解就是:webservice就是放在服务器上的函数,所有人...

C#调用WebService实例和开发

一、基本概念 Web Service也叫XML Web Service WebService是一种可以接收从Internet或者Intranet上的其它系统中传递过来的请求,轻量级的独立的通讯技术。是:通过SOAP在Web上提供的软件服务,使用WSDL文件进行说明,并通过UDDI进行注册。简单的理解就是:webservice 就是放在服务器上的函数,所有人都可以调用,然后返回信息。 比如googl...

c#创建及调用Webservice

VS2010 c#创建及调用Webservice 目录(?)[+] VS2010创建Webservice程序    在Visual Studio 2010中已经找不到直接创建WebService的模板方式了,但下面的方法可心实现:    在 Visual Studio 2010 的新建 Web 应用程序或者 Web 网站窗口中,如果你选择 .NET 4.0,...

C#的webservice服务引用调用

1.添加服务引用 2.http://172.23.35.198:8022/WebService1.asmx??wsdl 3.确定 4.调用方法   ConsoleApp2.ServiceReference2.WebService1SoapClient client = new ServiceReference2.WebService1SoapClient();    ...

C#调用WebService获取天气信息

概述   本文使用C#开发Winform应用程序,通过调用<WebXml/>(URL:http://www.webxml.com.cn)的WebService服务WeatherWS来获取天气预报数据。本程序所使用的Web服务的URL为:http://ws.webxml.com.cn/WebServices/WeatherWS.asmx,此服务为“2400多个城市天气预报We...

猜你喜欢

C#调用webservice简单实例

  如何利用IIS创建webservice不多做阐述,直接讲C#代码中如何调用已创建好的webservice。   首先在VS2010中新建一个工程项目,然后右键点击工程名选择添加服务引用。      在URL一栏中输入要调用的webservice地址,点击前往确认是否有服务出现,然后在命名空间输入自己想要的名称后确定。      服务引用添加成功后可以在资源管理器窗口里看到      然后就可以...

C# vs2010 调用webservice

如何调用已经发布的webservice,主要分为2个步骤(vs2010环境 C#) 发布webservice,使其处于服务状态。 创建一个asp.net程序(java等也可以),对webservice的方法进行调用。 发布webservice,可以参见 http://mingsuper.blog.51cto.com/8269088/1439419 创建asp.net 1)创建空的asp.net程序...

Delphi调用 C# 写的webservice

2019独角兽企业重金招聘Python工程师标准>>> Delphi调用 C# webservice总结   用delphi的THTTPRIO控件调用了c#写的webservice。 下面是我调试时遇到的一些问题: 1:导入wsdl文件:file--new----other----webservice---WSDLimporter---输入wsdl地址:如下:http:...

微服务

 微服务,关键其实不仅仅是微服务本身,而是系统要提供一套基础的架构,这种架构使得微服务可以独立的部署、运行、升级,不仅如此,这个系统架构还让微服务与微服务之间在结构上“松耦合”,而在功能上则表现为一个统一的整体。这种所谓的“统一的整体”表现出来的是统一风格的界面,统一的权限管理,统一的安全策略,统一的上线过程,统一的日志和审计方法,统一的调...

ubuntu16.04安装docker

教程来自官网:https://docs.docker.com/install/linux/docker-ce/ubuntu/#extra-steps-for-aufs 这个很流行,而且我也喜欢这个概念,我喜欢新鲜的东西(可以说相当的井底之蛙了)。所以尝试安装: 首先是仓库的选择,两种,核心在4之上可以用overlay2,也比较推荐用这个,特别是16.04及以上版本已经默认是包含了overlay2了...

问答精选

Homepage Slow to Load

When I go to the homepage of my website after clearing my cache and history, it oftentimes takes up to 10 s to load, but it is sporadic. Sometimes it loads quickly; other times it does not. Because we...

Syntax on Preg_match

I'm swapping a program out that is several years old, and updating the PHP, and MySQL deprication, and am getting jammed up on syntax for preg_match versus ereg. I tried putting slashes everywhere, an...

Windows batch file to check for a directory before application launch

How can I check if an NTFS drive exists before launching an application. If it's not present I'd like to display an error message. This is what I've got, but I'm no good at scripting. itunes.bat Messa...

Oracle REGEXP_SUBSTR will not match the dot character

I'm trying to extract information from strings like: to info like: Currently I'm not able to parse correctly when the module_name part contains . chars. See table below. The example below show how I e...

How to remove multiple spaces and new lines from a string in PHP?

I have a form with a text area, I need to remove from the string entered here eventuals multiple spaces and multiple new lines. I have written this function to remove the multiple spaces This function...

相关问题

相关文章

热门文章

推荐文章

相关标签

推荐问答