All articles| All Pictures| All Softwares| All Video| Go home page| Write articles| Upload pictures

Reading number is top 10 articles
asp.net2.0母版页的概述
学习动态网页技术PHP中错误处理的一些方法_php资料_编程技术
新瓶旧酒ASP.NET,AJAX(4),-,(客户端脚本编程,JavaScript基本类型扩展)_[Asp.Net教程]
用javascript实现无刷新更新数据_JavaScript技术_编程技术
SQL Server安全之加密术和SQL注入攻击_[SQL Server教程]
Microsoft,SQLServer安装示例_mssql学习_编程技术
visual c++中虚基类的用法
[delphi语法4]数据类型:枚举类型
ASP.NET如何跨站抓取页面_[Asp.Net教程]
ASP.NET之上传文件管理策略_.net资料_编程技术
Reading number is top 10 pictures
Ashlynn Brooke a group sexy photo4
2012 national geographic daily picture6
Lewd,it is too lewd.
So beauty, will let you spray blood5
非笑不可:最强爆笑图片精选
The little girl with long hair3
The little girl with long hair1
The money of more than 100 countries and regions19
The Soviet union swimsuit exposure in the 70 year3
Beauty ZhiHuiLin2
Download software ranking
I'm come from Beijing1
I for your crazy
Twenty piece of palm leaf
Unix video tutorial13
Boxer Classic video1
Visual C++界面编程技术
Boxer vs Yellow4
matrix1
Tram sex maniac 2 (H) rar bag8
C#COM编程指南
qq published in(发表于) 2014/7/11 9:23:27 Edit(编辑)
C#教程:创建Web服务

C#教程:创建Web服务

C#教程:创建Web服务

创建Web服务

创建Web服务的步骤如下所示。

(1)在ASP.NET 2005中创建Web服务和创建类很相似,其过程包括声明一个Web服务和定义一个Web服务接口方法。新建一个名为MrWebService的项目,在Visual Studio 2005开发环境中,选择“文件”/“新建”/“网站”选项,弹出“新建网站”对话框,在该对话框中选择“ASP.NET Web服务”选项,并命名为“mrWebService”,如图1所示。



图1 创建ASP.NET Web服务

(2)单击【确定】按钮,项目创建成功,并在“解决方案资源管理器”窗口中显示一个以.cs为扩展名的文件,如图2所示和一个以.asmx为扩展名的Web服务文件,如图3所示。在Web服务文件中包含一条“@Webservice”指令。Web服务至少由一个类组成,该类可以写在同一个.asmx文件中,也可以采用“Code Behind”(代码分离)方法,并通过“@WebService”指令引入。

(3)在“解决方案资源管理器”窗口中,双击“App_Code”文件夹下的Service.cs文件,进入其代码视图。Service.cs文件代码如下:



图2 以.cs结尾文件



图3 以.asmx结尾文件

using System;

using System.Web;

using System.Web.Services;

using System.Web.Services.Protocols;

[WebService(Namespace = "http://tempuri.org/")]

[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]

public class Service : System.Web.Services.WebService

{

public Service () {

}

[WebMethod]

public string HelloWorld() {

return "Hello World";

}

}

(4)在上述代码中,系统自带一个HelloWorld方法,此方法返回一个字符串。下面新建两个自定义方法:Add方法和Ascii方法,其中Add方法用来实现两数相加的功能,而Ascii方法用来实现返回字符的ASCII值功能。

Add方法和Ascii方法关键代码如下:

[WebMethod(Description="第一个测试函数,返回两个数的和")]

public int Add(int num1, int num2)

{

return num1 + num2;

}

[WebMethod(Description="第二个测试函数,返回字符ASCII")]

public char Ascii(string strAscII)

{

return Char.Parse(strAscII);

}

(5)设置WebMethod属性。

如果某一个成员函数能被外部调用,则必须满足两个条件:第一,该成员函数为public类型;第二,该成员函数必须具有“[Webmethod]”关键字修饰,因为只有具备了“[WebMethod]”的类,才可以通过SOAP被远程访问。如果要使某方法可以被Web服务调用,在其前面加上“[WebMethod]”属性即可。例如,下面代码中的HelloWorld方法、Add方法和Ascii方法都可以使用Web服务进行调用,而sub方法不能被调用。代码如下:

[WebMethod]

public string HelloWorld() {

return "Hello World";

}

[WebMethod(Description="第一个测试函数,返回两个数的和")]

public int Add(int num1, int num2)

{

return num1 + num2;

}

[WebMethod(Description="第二个测试函数,返回字符ASCII")]

public char Ascii(string strAscII)

{

return Char.Parse(strAscII);

}

public int sub(int num1,int num2)

{

return num1 * num2;

}

上面所建的Web服务的运行结果如图4所示。如图4所示,由于sub方法前面没有“[WebMethod]”属性,所以Web服务中没有,它也不能被远程访问。

WebMethod属性中可以指定的节及说明如表1所示。



图4 WebMethod属性效果



表1 WebMethod属性节




添加到del.icio.us 添加到新浪ViVi 添加到百度搜藏 添加到POCO网摘 添加到天天网摘365Key 添加到和讯网摘 添加到天极网摘 添加到黑米书签 添加到QQ书签 添加到雅虎收藏 添加到奇客发现 diigo it 添加到饭否 添加到飞豆订阅 添加到抓虾收藏 添加到鲜果订阅 digg it 貼到funP 添加到有道阅读 Live Favorites 添加到Newsvine 打印本页 用Email发送本页 在Facebook上分享


Disclaimer Privacy Policy About us Site Map

If you have any requirements, please contact webmaster。(如果有什么要求,请联系站长)
Copyright ©2011-
uuhomepage.com, Inc. All rights reserved.