VS2008中C#开发webservice简单实例

56 篇文章 1 订阅
51 篇文章 4 订阅

1.创建工程

文件-> 新建->网站 如下图。

 

工程建好后,会自动添加如下代码:

复制代码

 1 using System;
 2 using System.Linq;
 3 using System.Web;
 4 using System.Web.Services;
 5 using System.Web.Services.Protocols;
 6 using System.Xml.Linq;
 7 
 8 [WebService(Namespace = "http://tempuri.org/")]
 9 [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
10 // 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。
11 // [System.Web.Script.Services.ScriptService]
12 public class Service : System.Web.Services.WebService
13 {
14     public Service () {
15 
16         //如果使用设计的组件,请取消注释以下行 
17         //InitializeComponent(); 
18     }
19 
20     [WebMethod]
21     public string HelloWorld() {
22         return "Hello World";
23     }
24     
25 }

复制代码

 

可以运行一遍看看效果。

2.添加代码增强webservice的功能

增加加减乘除的功能。

代码如下:

复制代码

 1 using System;
 2 using System.Linq;
 3 using System.Web;
 4 using System.Web.Services;
 5 using System.Web.Services.Protocols;
 6 using System.Xml.Linq;
 7 
 8 [WebService(Namespace = "http://tempuri.org/")]
 9 [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
10 // 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。
11 // [System.Web.Script.Services.ScriptService]
12 public class Service : System.Web.Services.WebService
13 {
14     public Service () {
15 
16         //如果使用设计的组件,请取消注释以下行 
17         //InitializeComponent(); 
18     }
19 
20 
21     //[WebMethod]
22     //public string HelloWorld()
23     //{
24     //    return "Hello World";
25     //}
26 
27     [WebMethod(Description = "求和的方法")]
28     public double addition(double i, double j)
29     {
30         return i + j;
31     }
32 
33     [WebMethod(Description = "求差的方法")]
34     public double subtract(double i, double j)
35     {
36         return i - j;
37     }
38 
39     [WebMethod(Description = "求积的方法")]
40     public double mutiplication(double i, double j)
41     {
42         return i * j;
43     }
44 
45     [WebMethod(Description = "求商的方法")]
46     public double division(double i, double j)
47     {
48         if(j!=0)
49             return i/j;
50         else
51             return 0;
52     }
53 }

复制代码

运行效果如下:

 

在这个URL后面添加?wsdl就可以获取该webservice的wsdl。

 

3.使用生成的webservice

VS2008-> 文件->新建->网站->ASP.NET网站->website2

接下来添加刚才生成的webservice应用:

website2邮右键->添加web引用

URL是运行website1之后的网址(在使用刚才的webservice时,需要先把那个服务运行起来才行)

URL写好后,点击前往->添加应用->ok。

引入的web引用中有一个wsdl文件(此处对wsdl的提示与本文无关系)。wsdl文件如下:

复制代码

  1 <?xml version="1.0" encoding="utf-8"?>
  2 <wsdl:definitions xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tm="http://microsoft.com/wsdl/mime/textMatching/" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:mime="http://schemas.xmlsoap.org/wsdl/mime/" xmlns:tns="http://tempuri.org/" xmlns:s="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/" xmlns:http="http://schemas.xmlsoap.org/wsdl/http/" targetNamespace="http://tempuri.org/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/">
  3   <wsdl:types>
  4     <s:schema elementFormDefault="qualified" targetNamespace="http://tempuri.org/">
  5       <s:element name="addition">
  6         <s:complexType>
  7           <s:sequence>
  8             <s:element minOccurs="1" maxOccurs="1" name="i" type="s:double" />
  9             <s:element minOccurs="1" maxOccurs="1" name="j" type="s:double" />
 10           </s:sequence>
 11         </s:complexType>
 12       </s:element>
 13       <s:element name="additionResponse">
 14         <s:complexType>
 15           <s:sequence>
 16             <s:element minOccurs="1" maxOccurs="1" name="additionResult" type="s:double" />
 17           </s:sequence>
 18         </s:complexType>
 19       </s:element>
 20       <s:element name="subtract">
 21         <s:complexType>
 22           <s:sequence>
 23             <s:element minOccurs="1" maxOccurs="1" name="i" type="s:double" />
 24             <s:element minOccurs="1" maxOccurs="1" name="j" type="s:double" />
 25           </s:sequence>
 26         </s:complexType>
 27       </s:element>
 28       <s:element name="subtractResponse">
 29         <s:complexType>
 30           <s:sequence>
 31             <s:element minOccurs="1" maxOccurs="1" name="subtractResult" type="s:double" />
 32           </s:sequence>
 33         </s:complexType>
 34       </s:element>
 35       <s:element name="mutiplication">
 36         <s:complexType>
 37           <s:sequence>
 38             <s:element minOccurs="1" maxOccurs="1" name="i" type="s:double" />
 39             <s:element minOccurs="1" maxOccurs="1" name="j" type="s:double" />
 40           </s:sequence>
 41         </s:complexType>
 42       </s:element>
 43       <s:element name="mutiplicationResponse">
 44         <s:complexType>
 45           <s:sequence>
 46             <s:element minOccurs="1" maxOccurs="1" name="mutiplicationResult" type="s:double" />
 47           </s:sequence>
 48         </s:complexType>
 49       </s:element>
 50       <s:element name="division">
 51         <s:complexType>
 52           <s:sequence>
 53             <s:element minOccurs="1" maxOccurs="1" name="i" type="s:double" />
 54             <s:element minOccurs="1" maxOccurs="1" name="j" type="s:double" />
 55           </s:sequence>
 56         </s:complexType>
 57       </s:element>
 58       <s:element name="divisionResponse">
 59         <s:complexType>
 60           <s:sequence>
 61             <s:element minOccurs="1" maxOccurs="1" name="divisionResult" type="s:double" />
 62           </s:sequence>
 63         </s:complexType>
 64       </s:element>
 65     </s:schema>
 66   </wsdl:types>
 67   <wsdl:message name="additionSoapIn">
 68     <wsdl:part name="parameters" element="tns:addition" />
 69   </wsdl:message>
 70   <wsdl:message name="additionSoapOut">
 71     <wsdl:part name="parameters" element="tns:additionResponse" />
 72   </wsdl:message>
 73   <wsdl:message name="subtractSoapIn">
 74     <wsdl:part name="parameters" element="tns:subtract" />
 75   </wsdl:message>
 76   <wsdl:message name="subtractSoapOut">
 77     <wsdl:part name="parameters" element="tns:subtractResponse" />
 78   </wsdl:message>
 79   <wsdl:message name="mutiplicationSoapIn">
 80     <wsdl:part name="parameters" element="tns:mutiplication" />
 81   </wsdl:message>
 82   <wsdl:message name="mutiplicationSoapOut">
 83     <wsdl:part name="parameters" element="tns:mutiplicationResponse" />
 84   </wsdl:message>
 85   <wsdl:message name="divisionSoapIn">
 86     <wsdl:part name="parameters" element="tns:division" />
 87   </wsdl:message>
 88   <wsdl:message name="divisionSoapOut">
 89     <wsdl:part name="parameters" element="tns:divisionResponse" />
 90   </wsdl:message>
 91   <wsdl:portType name="ServiceSoap">
 92     <wsdl:operation name="addition">
 93       <wsdl:documentation xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/">求和的方法</wsdl:documentation>
 94       <wsdl:input message="tns:additionSoapIn" />
 95       <wsdl:output message="tns:additionSoapOut" />
 96     </wsdl:operation>
 97     <wsdl:operation name="subtract">
 98       <wsdl:documentation xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/">求插的方法</wsdl:documentation>
 99       <wsdl:input message="tns:subtractSoapIn" />
100       <wsdl:output message="tns:subtractSoapOut" />
101     </wsdl:operation>
102     <wsdl:operation name="mutiplication">
103       <wsdl:documentation xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/">求积的方法</wsdl:documentation>
104       <wsdl:input message="tns:mutiplicationSoapIn" />
105       <wsdl:output message="tns:mutiplicationSoapOut" />
106     </wsdl:operation>
107     <wsdl:operation name="division">
108       <wsdl:documentation xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/">求商的方法</wsdl:documentation>
109       <wsdl:input message="tns:divisionSoapIn" />
110       <wsdl:output message="tns:divisionSoapOut" />
111     </wsdl:operation>
112   </wsdl:portType>
113   <wsdl:binding name="ServiceSoap" type="tns:ServiceSoap">
114     <soap:binding transport="http://schemas.xmlsoap.org/soap/http" />
115     <wsdl:operation name="addition">
116       <soap:operation soapAction="http://tempuri.org/addition" style="document" />
117       <wsdl:input>
118         <soap:body use="literal" />
119       </wsdl:input>
120       <wsdl:output>
121         <soap:body use="literal" />
122       </wsdl:output>
123     </wsdl:operation>
124     <wsdl:operation name="subtract">
125       <soap:operation soapAction="http://tempuri.org/subtract" style="document" />
126       <wsdl:input>
127         <soap:body use="literal" />
128       </wsdl:input>
129       <wsdl:output>
130         <soap:body use="literal" />
131       </wsdl:output>
132     </wsdl:operation>
133     <wsdl:operation name="mutiplication">
134       <soap:operation soapAction="http://tempuri.org/mutiplication" style="document" />
135       <wsdl:input>
136         <soap:body use="literal" />
137       </wsdl:input>
138       <wsdl:output>
139         <soap:body use="literal" />
140       </wsdl:output>
141     </wsdl:operation>
142     <wsdl:operation name="division">
143       <soap:operation soapAction="http://tempuri.org/division" style="document" />
144       <wsdl:input>
145         <soap:body use="literal" />
146       </wsdl:input>
147       <wsdl:output>
148         <soap:body use="literal" />
149       </wsdl:output>
150     </wsdl:operation>
151   </wsdl:binding>
152   <wsdl:binding name="ServiceSoap12" type="tns:ServiceSoap">
153     <soap12:binding transport="http://schemas.xmlsoap.org/soap/http" />
154     <wsdl:operation name="addition">
155       <soap12:operation soapAction="http://tempuri.org/addition" style="document" />
156       <wsdl:input>
157         <soap12:body use="literal" />
158       </wsdl:input>
159       <wsdl:output>
160         <soap12:body use="literal" />
161       </wsdl:output>
162     </wsdl:operation>
163     <wsdl:operation name="subtract">
164       <soap12:operation soapAction="http://tempuri.org/subtract" style="document" />
165       <wsdl:input>
166         <soap12:body use="literal" />
167       </wsdl:input>
168       <wsdl:output>
169         <soap12:body use="literal" />
170       </wsdl:output>
171     </wsdl:operation>
172     <wsdl:operation name="mutiplication">
173       <soap12:operation soapAction="http://tempuri.org/mutiplication" style="document" />
174       <wsdl:input>
175         <soap12:body use="literal" />
176       </wsdl:input>
177       <wsdl:output>
178         <soap12:body use="literal" />
179       </wsdl:output>
180     </wsdl:operation>
181     <wsdl:operation name="division">
182       <soap12:operation soapAction="http://tempuri.org/division" style="document" />
183       <wsdl:input>
184         <soap12:body use="literal" />
185       </wsdl:input>
186       <wsdl:output>
187         <soap12:body use="literal" />
188       </wsdl:output>
189     </wsdl:operation>
190   </wsdl:binding>
191   <wsdl:service name="Service">
192     <wsdl:port name="ServiceSoap" binding="tns:ServiceSoap">
193       <soap:address location="http://localhost:12989/WebSite1/Service.asmx" />
194     </wsdl:port>
195     <wsdl:port name="ServiceSoap12" binding="tns:ServiceSoap12">
196       <soap12:address location="http://localhost:12989/WebSite1/Service.asmx" />
197     </wsdl:port>
198   </wsdl:service>
199 </wsdl:definitions>

复制代码

 

我们在这就练习调用webservice的四个方法,做一个简单的调用的例子,先在网站的前台添加几个控件(Default.aspx),代码如下:

复制代码

 1 <%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>
 2 
 3 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 4 
 5 <html xmlns="http://www.w3.org/1999/xhtml" >
 6  <head id="Head1" runat="server">
 7       <title>Webservice调用实例</title>
 8   </head>
 9   <body>
10       <form id="form1" runat="server">
11          <div>
12              <asp:TextBox ID="Num1" runat="server"></asp:TextBox>
13              <select id="selectOper" runat = "server">
14                  <option>+</option>
15                  <option>-</option>
16                  <option>*</option>
17                  <option>/</option>
18              </select>
19              <asp:TextBox ID="Num2" runat="server"></asp:TextBox>
20              <span id = E runat = "server"></span>
21              <asp:TextBox ID="Result" runat="server"></asp:TextBox>
22          </div>
23  </form>
24  </body>
25  </html>

复制代码

到此一个一个简单的WebService的开发和调用就已经完成了,在实际应用中可以根据自己的需要,写一些功能强大的,复杂的WebService,不管多么复杂,整个流程都是这样的。

修改Default.cs

复制代码

 1 using System;
 2 using System.Configuration;
 3 using System.Data;
 4 using System.Linq;
 5 using System.Web;
 6 using System.Web.Security;
 7 using System.Web.UI;
 8 using System.Web.UI.HtmlControls;
 9 using System.Web.UI.WebControls;
10 using System.Web.UI.WebControls.WebParts;
11 using System.Xml.Linq;
12 
13 public partial class _Default : System.Web.UI.Page 
14 {
15     protected void Page_Load(object sender, EventArgs e)
16     {
17         //在页面加载的时候动态创建一个按钮,在它的事件里调用Webservice
18         Button btn = new Button();
19         btn.Width = 20;
20         btn.Text = " = ";
21         btn.Click += new EventHandler(btn_Click);
22         E.Controls.Add(btn);
23     }
24     /// <summary>
25     /// 定义动态创建Button的Click事件,在这个事件中调用Webservice
26     /// </summary>
27     /// <param name="sender"></param>
28     /// <param name="e"></param>
29     void btn_Click(object sender, EventArgs e)
30     {
31         if (Num1.Text != "" && Num2.Text != "")
32         {
33             //实例化引用的webservice对象
34             localhost.Service WebserviceInstance = new localhost.Service();
35             int Oper = selectOper.SelectedIndex;
36             switch (Oper)
37             {
38                 //通过实例化的webservice对象来调用Webservice暴露的方法
39                 case 0:
40                     Result.Text = WebserviceInstance.addition(double.Parse(Num1.Text), double.Parse(Num2.Text)).ToString();
41                     break;
42                 case 1:
43                     Result.Text = WebserviceInstance.subtract(double.Parse(Num1.Text), double.Parse(Num2.Text)).ToString();
44                     break;
45                 case 2:
46                     Result.Text = WebserviceInstance.mutiplication(double.Parse(Num1.Text), double.Parse(Num2.Text)).ToString();
47                     break;
48                 case 3:
49                     Result.Text = WebserviceInstance.division(double.Parse(Num1.Text), double.Parse(Num2.Text)).ToString();
50                     break;
51             }
52         }
53     }
54 }

复制代码

可以运行该网站了。

 

转:https://www.cnblogs.com/LCCRNblog/p/3716406.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
前言. 第1章 Windows窗体技术 案例1.1 多文档MDI应用程序 案例1.2 QQ窗体 案例1.3 卡通窗体, 案例1.4 带有分隔栏并更换主界面背景窗体 案例1.5 半透明渐显动画和渐变窗体 案例1.6 浮动的窗体 案例1.7 在屏幕央并总在最前窗体 案例1.8 椭圆形窗体 案例1.9 可移动的五边形窗体 案例1.10 文字窗体 案例1.11 动态滑入滑出的闪烁窗体 案例1.12 以树形显示的窗体 案例1.13 图案动画窗体 本章小结 第2章 图形图像处理技术 案例2.1 波形图特效 案例2.2 在图像写入文字 案例2.3 图像缩放与翻转 .案例2.4 图像的涂沫 案例2.5 百叶窗图像效果 案例2.6 图像的纹理和遮罩动画效果 案例2.7 图像的积木和浮雕效果 案例2.8 文字的360°旋转效果 案例2.9 可以随意移动的GIF动画 案例2.10 图像的属性及选择点的RGB颜色值 案例2.11 运行的时钟 本章小结 第3章 多媒体处理技术 案例3.1 MP3播放器 案例3.2 Flash播放器 案例3.3 Gif播放器 案例3.4 图像滚动展示动画效果 案例3.5 电子相册 案例3.6 交通信号灯 案例3.7 随机变换图像屏幕保护程序 案例3.8 MP3音乐屏幕保护程序 本章小结 第4章 数据库处理技术 案例4.1 利用控件连接Access数据库 案例4.2 利用代码连接加密Access数据库 案例4.3 读取并浏览Access数据库的数据 案例4.4 利用控件连接SQL Server数据库 案例4.5 利用ADO.NET访问SQL Server数据库 案例4.6 连接Excel电子表格 案例4.7 利用SQL语句向数据库表插入数据 案例4.8 利用存储过程修改数据库的数据 案例4.9 分页显示数据库表的数据 案例4.10 动态查询数据库表的数据 案例4.11 SQL Server服务的开启和断开 本章小结 第5章 水晶报表和打印处理技术 案例5.1 水晶报表使用SQL Server数据库 案例5.2 利用水晶报表分组统计数据库的数据 案例5.3 图表在水晶报表的应用 案例5.4 控制水晶报表的字段个数、字体颜色及记录显示 案例5.5 水晶报表子报表的应用 案例5.6 利用打印组件实现窗体数据的打印 案例5.7 打印输出图形图像 案例5.8 调用Excel打印表格数据 案例5.9 调用Word打印表格数据 本章小结 第6章 文件及注册表处理技术 案例6.1 文件的动态创建与删除 案例6.2 文件夹的动态创建与删除.. 案例6.3 文件的打开和保存 案例6.4 文件的加密和解密 案例6.5 文件的动态查找 案例6.6 动态复制多个文件 案例6.7 动态获取系统当前目录及程序当前目录 案例6.8 文件属性的查看与修改 案例6.9 桌面图标和驱动器的显示与隐藏 案例6.10 禁示修改IE浏览器的主页 本章小结 第7章 硬件处理技术 案例7.1 计算机的注销、关闭和重新启动 案例7.2 任务栏和“开始”按钮的显示与隐藏 案例7.3 声卡的检测及PC喇叭的控制 案例7.4 动态控制光驱的打开和关闭 案例7.5 鼠标的控制 案例7.6 设置系统默认输入法 案例7.7 锁定计算机 案例7.8 屏幕保护程序的启动 案例7.9 利用串口进行通信 本章小结 第8章 数据结构与算法 案例8.1 单向链表数据结构 案例8.2 堆栈数据结构 案例8.3 队列数据结构 案例8.4 冒泡排序和选择排序 案例8.5 希尔排序和插入排序 案例8.6 搬砖问题算法 案例8.7 爱因斯坦的阶梯问题算法 案例8.8 求最大公因子的欧几里德算法 案例8.9 IP地址的合法性检查算法 案例8.10 由旧身份证自动生成新身证算法 本章小结 第9章 网络开发技术 案例9.1 动态获取本机名和IP地址 案例9.2 动态获取本机网卡地址 案例9.3 动态修改本机IP地址 案例9.4 网络连接和传输信息 案例9.5 网络在线检测 案例9.6 网络在线浏览器 案例9.7 HTTP动态下载 案例9.8 动态查看网页标题信息 案例9.9 电子邮件的发送 本章小结 第10章 Web程序开发技术 案例10.1 利用AdRotator控件随机显示广告图像 案例10.2 利用Calendar控件动态查询系统日期 案例10.3 会员登录系统 案例10.4 利用Application对象实现网站计数器 案例10.5 利用Session对象实现留言板 案例10.6 动态上传图像并显示 案例10.7 利用控件显示数据库表的数据 案例10.8 分页显示数据库表的数据 案例10.9 动态选择、编辑、删除数据库表的数据 本章小结 第11章 应用程序的打包 案例11.1 Windows应用程序的打包 案例11.2 Web应用程序的打包 案例11.3 带有数据库文件的应用程序的打包 案例11.4 带有特定文件的应用程序打包 案例11.5 带有注册表信息的应用程序打包 本章小结 第12章 消费管理系统 12.1 系统总体设计 12.2 系统数据库设计 12.3 实例效果 12.4 消费管理登录系统 12.5 消费管理系统主界面 12.6 消费管理系统的查询功能 12.7 消费信息管理功能 12.8 消费信息报表输出界面 本章小结
当然,我可以为你提供一个简单C# WebService接口开发的示例。请参考以下代码: ```csharp using System; using System.Web.Services; namespace WebServiceExample { [WebService(Namespace = "http://www.example.com/")] [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] public class HelloWorldService : WebService { [WebMethod] public string HelloWorld() { return "Hello, World!"; } [WebMethod] public int AddNumbers(int a, int b) { return a + b; } } } ``` 在这个示例,我们创建了一个名为`HelloWorldService`的WebService类。该类继承自`WebService`基类,并使用`[WebService]`和`[WebServiceBinding]`属性进行配置。 我们定义了两个Web方法:`HelloWorld`和`AddNumbers`。`HelloWorld`方法返回一个简单的字符串,而`AddNumbers`方法接受两个整数参数并返回它们的和。 要使用这个WebService,你需要将该代码编译为一个可执行文件并部署到一个支持ASP.NET的Web服务器上。一旦部署完成,你可以通过向WebService的URL发送HTTP请求来调用这些方法。 例如,要调用`HelloWorld`方法,你可以向以下URL发送GET请求:`http://yourserver/HelloWorldService.asmx/HelloWorld` 要调用`AddNumbers`方法,你可以向以下URL发送GET请求:`http://yourserver/HelloWorldService.asmx/AddNumbers?a=5&b=10` 请注意,在实际开发,你可能需要对Web方法进行身份验证、数据验证和错误处理等操作。上述示例只是一个简单的入门示例,帮助你了解C# WebService接口的基本概念。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值