您的位置:首页 > 其它

.NET基础示例系列之二十一:Web Service

2007-12-08 21:39 429 查看
1.基本概念

我没有考据过Web Service的概念具体是什么时间提出的,肯定已经不是很新鲜。对我来说“潮流只能等,不能追”,我是随其波而逐其流,跟着学习学习。

Web Service的目标是使不同的应用程序域能在Web上实现相互之间的访问、操作。将来或许有一些人或者公司,他们在网络上发布Web Service接口,公众或者公司在需要某些服务功能(如一些非常复杂的计算)时,可以在网络上搜索有无此类功能的接口,找到之后便可以在自己的程序中调用这些接口,并付给接口作者一定的费用,使用者就可以不必自己去实现所需的功能了。

另外,相关的概念还有:

SOAP:它是Simple Object Access Protocol的缩写,即“简单对象访问协议”。简单说来,SOAP就是用来访问Web Service的协议,它的本质工作是定义了访问Web Service的信息发送格式。

WSDL:它是Web Services Description Language的缩写,用来描述Web服务XML语言。它以一定的格式描述某个Web Service接口的名称、参数、返回值等等内容(详细可以参见下面的示例),它就是一个接口文档,当你在教别人怎么调用你的接口时,可以给它一个对应的wsdl文件。

2.创建Web Service接口

使用Visual Studio 2005可以十分方便地创建一个Web Service程序:新建网站>>选择“Asp.net Web服务”模板,输入名称,如“WebSrvSample”,点击确定即可。初始情况下,工程中包含一个.asmx文件、一个.cs文件。我们的代码就写在.cs文件里:

using System;

using System.Web;

using System.Web.Services;

using System.Web.Services.Protocols;

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

[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]

public class ComplexCalculationSrv : System.Web.Services.WebService

<?xml version="1.0" encoding="utf-8"?>

<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://www.SomeNobody.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://www.SomeNobody.org/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/">

<wsdl:types>

<s:schema elementFormDefault="qualified" targetNamespace="http://www.SomeNobody.org/">

<s:element name="DoVeryComplexCalculation">

<s:complexType />

</s:element>

<s:element name="DoVeryComplexCalculationResponse">

<s:complexType>

<s:sequence>

<s:element minOccurs="1" maxOccurs="1" name="DoVeryComplexCalculationResult" type="s:double" />

</s:sequence>

</s:complexType>

</s:element>

</s:schema>

</wsdl:types>

<wsdl:message name="DoVeryComplexCalculationSoapIn">

<wsdl:part name="parameters" element="tns:DoVeryComplexCalculation" />

</wsdl:message>

<wsdl:message name="DoVeryComplexCalculationSoapOut">

<wsdl:part name="parameters" element="tns:DoVeryComplexCalculationResponse" />

</wsdl:message>

<wsdl:portType name="ComplexCalculationSrvSoap">

<wsdl:operation name="DoVeryComplexCalculation">

<wsdl:documentation xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/">非常复杂的计算</wsdl:documentation>

<wsdl:input message="tns:DoVeryComplexCalculationSoapIn" />

<wsdl:output message="tns:DoVeryComplexCalculationSoapOut" />

</wsdl:operation>

</wsdl:portType>

<wsdl:binding name="ComplexCalculationSrvSoap" type="tns:ComplexCalculationSrvSoap">

<soap:binding transport="http://schemas.xmlsoap.org/soap/http" />

<wsdl:operation name="DoVeryComplexCalculation">

<soap:operation soapAction="http://www.SomeNobody.org/DoVeryComplexCalculation" style="document" />

<wsdl:input>

<soap:body use="literal" />

</wsdl:input>

<wsdl:output>

<soap:body use="literal" />

</wsdl:output>

</wsdl:operation>

</wsdl:binding>

<wsdl:binding name="ComplexCalculationSrvSoap12" type="tns:ComplexCalculationSrvSoap">

<soap12:binding transport="http://schemas.xmlsoap.org/soap/http" />

<wsdl:operation name="DoVeryComplexCalculation">

<soap12:operation soapAction="http://www.SomeNobody.org/DoVeryComplexCalculation" style="document" />

<wsdl:input>

<soap12:body use="literal" />

</wsdl:input>

<wsdl:output>

<soap12:body use="literal" />

</wsdl:output>

</wsdl:operation>

</wsdl:binding>

<wsdl:service name="ComplexCalculationSrv">

<wsdl:port name="ComplexCalculationSrvSoap" binding="tns:ComplexCalculationSrvSoap">

<soap:address location="http://localhost/WebSrvSample/ComplexCalculationSrv.asmx" />

</wsdl:port>

<wsdl:port name="ComplexCalculationSrvSoap12" binding="tns:ComplexCalculationSrvSoap12">

<soap12:address location="http://localhost/WebSrvSample/ComplexCalculationSrv.asmx" />

</wsdl:port>

</wsdl:service>

</wsdl:definitions>

3.接口调用

下面演示如何在一个新的程序中调用刚上完成的Web Service接口,可以新建一个WinFrom工程,比如“UseWebSrv”。在项目的引用中添加Web引用,在弹出的窗口中输入接口url地址:

http://hostip/WebSrvSample/ComplexCalculationSrv.asmx,点击“前往”,以测试连接是否成功。随后在“Web引用名”处为你的web引用取个名称,如“WebSrvSample”,点击确定。随后编写调用代码:

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Text;

using System.Windows.Forms;

using UseWebSrv.WebSrvSample;

namespace UseWebSrv

{

public partial class Form1 : Form

{

public Form1()

{

InitializeComponent();

}

private void button1_Click(object sender, EventArgs e)

{

ComplexCalculationSrv srv = new ComplexCalculationSrv();

double d = srv.DoVeryComplexCalculation();

this.textBox1.Text = d.ToString();

}

}

}
注意引用的名称空间即此项目的名称空间+你刚才为Web引用取的名称。

4.其它问题

实验中发现在Web Service接口代码中不能使用方法的重载,就是不允许存在方法名一样的两个方法,即使它们的参数不同。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: