.NET平台内建了对Web Service的支持,包括Web Service的构建和使用。与其它开发平台不同,使用.NET平台,你不需要其他的工具或者SDK就可以完成Web Service的开发了。.NET Framework本身就全面支持Web Service,包括服务器端的请求处理器和对客户端发送和接受SOAP消息的支持。下来我们就使用 一个非常简单的示例来介绍Web Service的创建、部署和使用。

   

一、用Visual Studio创建一个最简单的Web Service

  首先,打开VS,打开“File->New->Web Site”,选择“ASP.NET Web Service”。

  查看Service.cs代码,你会发现已经为Web Service文件建立了缺省的框架。原始代码为:

 

using System;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Linq;

[WebService(Namespace 
= "http://tempuri.org/")]
[WebServiceBinding(ConformsTo 
= WsiProfiles.BasicProfile1_1)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
// [System.Web.Script.Services.ScriptService]
public class Service : System.Web.Services.WebService
{
    
public Service () {
        
//Uncomment the following line if using designed components 
        
//InitializeComponent(); 
    }

    [WebMethod]
    
public string HelloWorld() {
        
return "Hello World";
    }
}

相关文章: