1.1、Web Service基本概念
Web Service也叫XML Web Service WebService是一種可以接收從Internet或者Intranet上的其它系統(tǒng)中傳遞過來的請求,輕量級的獨立的通訊技術(shù)。是:通過SOAP在Web上提供的軟件服務(wù),使用WSDL文件進行說明,并通過UDDI進行注冊。
XML:(Extensible Markup Language)擴展型可標記語言。面向短期的臨時數(shù)據(jù)處理、面向萬維網(wǎng)絡(luò),是Soap的基礎(chǔ)。
Soap:(Simple Object Access Protocol)簡單對象存取協(xié)議。是XML Web Service 的通信協(xié)議。當用戶通過UDDI找到你的WSDL描述文檔后,他通過可以SOAP調(diào)用你建立的Web服務(wù)中的一個或多個操作。SOAP是XML文檔形式的調(diào)用方法的規(guī)范,它可以支持不同的底層接口,像HTTP(S)或者SMTP。
WSDL:(Web Services Description Language) WSDL 文件是一個 XML 文檔,用于說明一組 SOAP 消息以及如何交換這些消息。大多數(shù)情況下由軟件自動生成和使用。
UDDI (Universal Description, Discovery, and Integration) 是一個主要針對Web服務(wù)供應(yīng)商和使用者的新項目。在用戶能夠調(diào)用Web服務(wù)之前,必須確定這個服務(wù)內(nèi)包含哪些商務(wù)方法,找到被調(diào)用的接口定義,還要在服務(wù)端來編制軟件,UDDI是一種根據(jù)描述文檔來引導(dǎo)系統(tǒng)查找相應(yīng)服務(wù)的機制。UDDI利用SOAP消息機制(標準的XML/HTTP)來發(fā)布,編輯,瀏覽以及查找注冊信息。它采用XML格式來封裝各種不同類型的數(shù)據(jù),并且發(fā)送到注冊中心或者由注冊中心來返回需要的數(shù)據(jù)。
1.2、XML Web Service的特點
Web Service的主要目標是跨平臺的可互操作性。為了實現(xiàn)這一目標,Web Service 完全基于XML(可擴展標記語言)、XSD(XML Schema)等獨立于平臺、獨立于軟件供應(yīng)商的標準,是創(chuàng)建可互操作的、分布式應(yīng)用程序的新平臺。因此使用Web Service有許多優(yōu)點:
1、跨防火墻的通信
如果應(yīng)用程序有成千上萬的用戶,而且分布在世界各地,那么客戶端和服務(wù)器之間的通信將是一個棘手的問題。因為客戶端和服務(wù)器之間通常會有防火墻或者代理服務(wù)器。傳統(tǒng)的做法是,選擇用瀏覽器作為客戶端,寫下一大堆ASP頁面,把應(yīng)用程序的中間層暴露給最終用戶。這樣做的結(jié)果是開發(fā)難度大,程序很難維護。要是客戶端代碼不再如此依賴于HTML表單,客戶端的編程就簡單多了。如果中間層組件換成Web Service的話,就可以從用戶界面直接調(diào)用中間層組件,從而省掉建立ASP頁面的那一步。要調(diào)用Web Service,可以直接使用Microsoft SOAP Toolkit或.net這樣的SOAP客戶端,也可以使用自己開發(fā)的SOAP客戶端,然后把它和應(yīng)用程序連接起來。不僅縮短了開發(fā)周期,還減少了代碼復(fù)雜度,并能夠增強應(yīng)用程序的可維護性。同時,應(yīng)用程序也不再需要在每次調(diào)用中間層組件時,都跳轉(zhuǎn)到相應(yīng)的“結(jié)果頁”。
2、應(yīng)用程序集成
企業(yè)級的應(yīng)用程序開發(fā)者都知道,企業(yè)里經(jīng)常都要把用不同語言寫成的、在不同平臺上運行的各種程序集成起來,而這種集成將花費很大的開發(fā)力量。應(yīng)用程序經(jīng)常需要從運行的一臺主機上的程序中獲取數(shù)據(jù);或者把數(shù)據(jù)發(fā)送到主機或其它平臺應(yīng)用程序中去。即使在同一個平臺上,不同軟件廠商生產(chǎn)的各種軟件也常常需要集成起來。通過Web Service,應(yīng)用程序可以用標準的方法把功能和數(shù)據(jù)“暴露”出來,供其它應(yīng)用程序使用。
XML Web services 提供了在松耦合環(huán)境中使用標準協(xié)議(HTTP、XML、SOAP 和 WSDL)交換消息的能力。消息可以是結(jié)構(gòu)化的、帶類型的,也可以是松散定義的。
3、B2B的集成
B2B 指的是Business to Business,as in businesses doing business with other businesses,商家(泛指企業(yè))對商家的電子商務(wù),即企業(yè)與企業(yè)之間通過互聯(lián)網(wǎng)進行產(chǎn)品、服務(wù)及信息的交換。通俗的說法是指進行電子商務(wù)交易的供需雙方都是商家(或企業(yè)、公司),她們使用了Internet的技術(shù)或各種商務(wù)網(wǎng)絡(luò)平臺,完成商務(wù)交易的過程。
Web Service是B2B集成成功的關(guān)鍵。通過Web Service,公司可以只需把關(guān)鍵的商務(wù)應(yīng)用“暴露”給指定的供應(yīng)商和客戶,就可以了,Web Service運行在Internet上,在世界任何地方都可輕易實現(xiàn),其運行成本就相對較低。Web Service只是B2B集成的一個關(guān)鍵部分,還需要許多其它的部分才能實現(xiàn)集成。用Web Service來實現(xiàn)B2B集成的最大好處在于可以輕易實現(xiàn)互操作性。只要把商務(wù)邏輯“暴露”出來,成為Web Service,就可以讓任何指定的合作伙伴調(diào)用這些商務(wù)邏輯,而不管他們的系統(tǒng)在什么平臺上運行,使用什么開發(fā)語言。這樣就大大減少了花在B2B集成上的時間和成本。
4、軟件和數(shù)據(jù)重用
Web Service在允許重用代碼的同時,可以重用代碼背后的數(shù)據(jù)。使用Web Service,再也不必像以前那樣,要先從第三方購買、安裝軟件組件,再從應(yīng)用程序中調(diào)用這些組件;只需要直接調(diào)用遠端的Web Service就可以了。另一種軟件重用的情況是,把好幾個應(yīng)用程序的功能集成起來,通過Web Service “暴露”出來,就可以非常容易地把所有這些功能都集成到你的門戶站點中,為用戶提供一個統(tǒng)一的、友好的界面。可以在應(yīng)用程序中使用第三方的Web Service 提供的功能,也可以把自己的應(yīng)用程序功能通過Web Service 提供給別人。兩種情況下,都可以重用代碼和代碼背后的數(shù)據(jù)。
從以上論述可以看出,Web Service 在通過Web進行互操作或遠程調(diào)用的時候是最有用的。不過,也有一些情況,Web Service根本不能帶來任何好處,Web Service有一下缺點:
1、單機應(yīng)用程序
目前,企業(yè)和個人還使用著很多桌面應(yīng)用程序。其中一些只需要與本機上的其它程序通信。在這種情況下,最好就不要用Web Service,只要用本地的API就可以了。COM非常適合于在這種情況下工作,因為它既小又快。運行在同一臺服務(wù)器上的服務(wù)器軟件也是這樣。當然Web Service 也能用在這些場合,但那樣不僅消耗太大,而且不會帶來任何好處。
2、局域網(wǎng)的一些應(yīng)用程序
在許多應(yīng)用中,所有的程序都是在Windows平臺下使用COM,都運行在同一個局域網(wǎng)上。在這些程序里,使用DCOM會比SOAP/HTTP有效得多。與此相類似,如果一個.net程序要連接到局域網(wǎng)上的另一個.net程序,應(yīng)該使用.net Remoting。其實在.net Remoting中,也可以指定使用SOAP/HTTP來進行Web Service 調(diào)用。不過最好還是直接通過TCP進行RPC調(diào)用,那樣會有效得多。
1.3、XML Web Service的應(yīng)用
1.最初的 XML Web Service 通常是可以方便地并入應(yīng)用程序的信息來源,如股票價格、天氣預(yù)報、體育成績等等。
2.以 XML Web Service 方式提供現(xiàn)有應(yīng)用程序,可以構(gòu)建新的、更強大的應(yīng)用程序,并利用 XML Web Service 作為構(gòu)造塊。
例如,用戶可以開發(fā)一個采購應(yīng)用程序,以自動獲取來自不同供應(yīng)商的價格信息,從而使用戶可以選擇供應(yīng)商,提交訂單,然后跟蹤貨物的運輸,直至收到貨物。而供應(yīng)商的應(yīng)用程序除了在Web上提供服務(wù)外,還可以使用XML Web Service檢查客戶的信用、收取貨款,并與貨運公司辦理貨運手續(xù)。
注意:WebService方法不能返回對象,只能返回值類型數(shù)據(jù).如字符串數(shù)組 字符串 數(shù)值和基本XML的數(shù)據(jù)如:DataSet DataTable.
建立一個名為WebService1.asmx的WebService
view plaincopy to clipboardprint?
using System;
using System.Data;
using System.Data.SqlClient;
using System.Web;
using System.Collections;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.ComponentModel;
namespace WebService1
{
/// <summary>
/// Service1 的摘要說明
/// </summary>
[WebService(Namespace = "
http://www.ebook.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.None)]//為了實現(xiàn)方法重載這里設(shè)為WsiProfiles.None
[ToolboxItem(false)]
public class Service1 : System.Web.Services.WebService
{
[WebMethod]//描述語言語法[property()]
public int Add(int a, int b)
{
return a + b;
}
[WebMethod(MessageName = "Add3")]//為帶有三個參數(shù)的Add方法取別名
public int Add(int x, int y, int z)
{
return x + y + z;
}
[WebMethod]
public int GetCount1()
{
if (Application["count"] == null)
{
Application["count"] = "0";
}
int a = int.Parse(Application["count"].ToString());
a++;
Application["count"] = a;
return a;
}
[WebMethod(EnableSession = true)]//啟用會話
public int GetCount2()
{
if (Session["count"] == null)
{
Session["count"] = "0";
}
int a = int.Parse(Session["count"].ToString());
a++;
Session["count"] = a;
return a;
}
[WebMethod]
public DataSet GetDataSet()
{
DataSet ds = new DataSet();
string connstr = "server=.;database=pubs;uid=sa;pwd=";
string sql = "select * from jobs";
using (SqlConnection conn = new SqlConnection(connstr))
{
SqlDataAdapter da = new SqlDataAdapter(sql, conn);
da.Fill(ds);
}
return ds;
}
}
}
using System;
using System.Data;
using System.Data.SqlClient;
using System.Web;
using System.Collections;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.ComponentModel;
namespace WebService1
{
/// <summary>
/// Service1 的摘要說明
/// </summary>
[WebServiceBinding(ConformsTo = WsiProfiles.None)]//為了實現(xiàn)方法重載這里設(shè)為WsiProfiles.None
[ToolboxItem(false)]
public class Service1 : System.Web.Services.WebService
{
[WebMethod]//描述語言語法[property()]
public int Add(int a, int b)
{
return a + b;
}
[WebMethod(MessageName = "Add3")]//為帶有三個參數(shù)的Add方法取別名
public int Add(int x, int y, int z)
{
return x + y + z;
}
[WebMethod]
public int GetCount1()
{
if (Application["count"] == null)
{
Application["count"] = "0";
}
int a = int.Parse(Application["count"].ToString());
a++;
Application["count"] = a;
return a;
}
[WebMethod(EnableSession = true)]//啟用會話
public int GetCount2()
{
if (Session["count"] == null)
{
Session["count"] = "0";
}
int a = int.Parse(Session["count"].ToString());
a++;
Session["count"] = a;
return a;
}
[WebMethod]
public DataSet GetDataSet()
{
DataSet ds = new DataSet();
string connstr = "server=.;database=pubs;uid=sa;pwd=";
string sql = "select * from jobs";
using (SqlConnection conn = new SqlConnection(connstr))
{
SqlDataAdapter da = new SqlDataAdapter(sql, conn);
da.Fill(ds);
}
return ds;
}
}
}view plaincopy to clipboardprint?
建立一個名為Form1.cs的窗體view plaincopy to clipboardprint?
<PRE class=csharp name="code">using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using accp.com.cn;
namespace WebServiceTest
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
int a = int.Parse(num1.Text);
int b = int.Parse(num2.Text);
try
{
//用try保護 URL地址固定
localhost.Service1 service=new localhost.Service1();
service.Url = "
http://localhost:1067/Service1.asmx?WSDL";
int c = service.Add(a, b);
txtCount.Text = c.ToString();
this.dgv.DataSource=service.GetDataSet().Tables[0];
}
catch (Exception ex)
{
throw ex;
}
}
}
}</PRE>
從webservice中分離出Serice1.cs代碼
C:\Program Files\Microsoft Visual Studio 8\SDK\v2.0\Bin>wsdl /l:CS /n:accp.com.cn /out:d:\ <A href="
http://localhost:1067/Service1.asmx?WSDL">http://localhost:1067/Service1.asmx?WSDL</A>
view plaincopy to clipboardprint?using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using accp.com.cn; namespace WebServiceTest { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { int a = int.Parse(num1.Text); int b = int.Parse(num2.Text); try { //用try保護 URL地址固定 localhost.Service1 service=new localhost.Service1(); service.Url = "
http://localhost:1067/Service1.asmx?WSDL"; int c = service.Add(a, b); txtCount.Text = c.ToString(); this.dgv.DataSource=service.GetDataSet().Tables[0]; } catch (Exception ex) { throw ex; } } } } using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using accp.com.cn;
namespace WebServiceTest
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
int a = int.Parse(num1.Text);
int b = int.Parse(num2.Text);
try
{
//用try保護 URL地址固定
localhost.Service1 service=new localhost.Service1();
int c = service.Add(a, b);
txtCount.Text = c.ToString();
this.dgv.DataSource=service.GetDataSet().Tables[0];
}
catch (Exception ex)
{
throw ex;
}
}
}
}
從webservice中分離出Serice1.cs代碼
C:\Program Files\Microsoft Visual Studio 8\SDK\v2.0\Bin>wsdl /l:CS /n:accp.com.cn /out:d:\
http://localhost:1067/Service1.asmx?WSDLview plaincopy to clipboardprint?
生成DLL
C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727 csc /out:d:\accp.com.cn.dll /t:library
生成DLL
C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727 csc /out:d:\accp.com.cn.dll /t:libraryview plaincopy to clipboardprint?
d:\Service1.cs
d:\Service1.csview plaincopy to clipboardprint?
view plaincopy to clipboardprint?
WebService中的設(shè)置
WebService中的設(shè)置view plaincopy to clipboardprint?
<system.web>
<webServices >
<protocols>
<clear/>
<add name="Documentation"/>
<add name="HttpSoap"/>
</protocols>
</webServices>
</system.web>
<system.web>
<webServices >
<protocols>
<clear/>
<add name="Documentation"/>
<add name="HttpSoap"/>
</protocols>
</webServices>
</system.web>view plaincopy to clipboardprint?
首先清除所協(xié)議,再啟用Documentation HttpSoap即可.其它不用啟用.
首先清除所協(xié)議,再啟用Documentation HttpSoap即可.其它不用啟用.