利用Web Services实现软件自动升级
摘 要:軟件維護(hù)升級工作是軟件生命周期最重要的環(huán)節(jié)。為了解決以往C/S(Client/Server)模式下的客戶端軟件升級效率低的問題,設(shè)計了C/S應(yīng)用系統(tǒng)自動升級處理程序。該程序利用Web Services技術(shù)、C#和XML語言,通過網(wǎng)絡(luò)來完成C/ S應(yīng)用系統(tǒng)的自動升級。與原有手工升級、FTP 文件服務(wù)器升級和第三方控件升級相比,升級效率更高。該方案具有較好的參考價值。
關(guān)鍵詞:C#;Web Services;XML;軟件自動升級
中圖法分類號: 文獻(xiàn)標(biāo)志碼:
1 引言
隨著計算機(jī)網(wǎng)絡(luò)應(yīng)用技術(shù)的不斷發(fā)展,在開發(fā)MIS系統(tǒng)時,大多采用基于C/S(客戶機(jī)/服務(wù)器)模式或B/S(瀏覽器/服務(wù)器)模式。現(xiàn)在B/S模式以其真正意義上的瘦客戶機(jī)/胖服務(wù)器模式優(yōu)勢占據(jù)了主導(dǎo)地位。但是由于客戶機(jī)/服務(wù)器模式具有的數(shù)據(jù)流量小、響應(yīng)時間短、安全性高等特點(diǎn),在解決幾十個到幾百個用戶的局域網(wǎng)中,仍然是一個不錯的選擇[1-3]。在C/S模式下,應(yīng)用程序的每次升級都需要在每個客戶端重新安裝應(yīng)用程序,這是一項(xiàng)十分繁瑣的事情。面對這個實(shí)際問題,這里設(shè)計了一個通過軟件實(shí)現(xiàn)自動升級技術(shù)方案,彌補(bǔ)了這一缺陷,有較好的參考價值。
2 設(shè)計思路
判斷一個文件是否要更新,可以通過判斷文件的大小、修改日期和文件的版本號來實(shí)現(xiàn)[3-5]。發(fā)現(xiàn)最新的則提示用戶是否升級。
在Web Services中實(shí)現(xiàn)一個GetVer的WebMethod方法,其作用是獲取當(dāng)前的最新版本。然后將現(xiàn)在版本與最新版本比較,如果有新版本,則進(jìn)行升級。
3 自動升級的技術(shù)實(shí)現(xiàn)
(1)編寫升級模板文件Update.xml
準(zhǔn)備一個XML文件 (Update.xml) ,作為一個升級用的模板。
……
<description>升級記錄</description>
<filelist count="4" sourcepath="./update/">
……
<item name="CustomerApplication.exe" size="">
<value />
</item>
<item name="Interop.SHDocVw.dll" size="">
<value />
</item>
……
SHDocVw.DLL 是Internet Explorer的一個組件,該組件負(fù)責(zé)控制對從Web 站點(diǎn)返回的URL和信息的處理。
(2)編寫Web Services的GetVer方法
GetVer方法用于取得軟件的更新版本。
[WebMethod(Description="取得更新版本")]
Public string GetVer()
{
XmlDocument doc = new XmlDocument();
doc.Load(Server.MapPath("update.xml"));
XmlElement root = doc.DocumentElement;
return root.SelectSingleNode("version").Inertest;
}
(3)編寫Web Services的GetUpdateData方法
GetUpdateData方法用于在線更新軟件。
[WebMethod(Description="在線更新軟件")]
[SoapHeader("sHeader")]
public System.Xml.XmlDocument GetUpdateData()
{
//驗(yàn)證用戶是否登陸
if(sHeader==null)
return null;
if(!DataProvider.GetInstance.CheckLogin(sHeader.Username,sHeader.Password))
return null;
//取得更新的xml模板內(nèi)容
XmlDocument doc = new XmlDocument();
doc.Load(Server.MapPath("update.xml"));c XmlElement root = doc.DocumentElement;
//看看有幾個文件需要更新
XmlNode updateNode = root.SelectSingleNode("filelist");
string path = updateNode.Attributes["sourcepath"].Value;
int count = int.Parse(updateNode.Attributes["count"].Value);
//將xml中的value用實(shí)際內(nèi)容替換
for(int i=0;i= updateNode.ChildNodes[i];
string fileName = path itemNode.Attributes["name"].Value;
FileStream fs = File.OpenRead(Server.MapPath(fileName));
itemNode.Attributes["size"].Value = fs.Length.ToString();
BinaryReader br = new BinaryReader(fs);
//這里是文件的實(shí)際內(nèi)容,使用了Base64String編碼
itemNode.SelectSingleNode("value").InnerText = Convert.ToBase64String(br.ReadBytes((int)fs.Length),0,(int)fs.Length);
br.Close();
fs.Close();
}
return doc;
}
(4)編寫客戶端的UpDate方法
首先引用此Web Services,例如命名為:WebSvs,
string nVer = Start.GetService.GetVer();
if(Application.ProductVersion.CompareTo(nVer)<=0)
update();
在本代碼中Start.GetService是WebSvs的一個靜態(tài)實(shí)例。功能是:首先檢查版本,將結(jié)果與當(dāng)前版本進(jìn)行比較,如果為新版本則執(zhí)行UpDate方法。
update的作用是將升級的XML文件下載下來,保存為執(zhí)行文件目錄下的一個Update.xml文件。任務(wù)完成,退出程序,等待Update.Exe 來進(jìn)行升級。
void update()
{
this.statusBarPanel1.Text = "正在下載...";
System.Xml.XmlDocument doc = ((System.Xml.XmlDocument)Start.GetService.GetUpdateData());
doc.Save(Application.StartupPath @"\update.xml");
System.Diagnostics.Process.Start(Application.StartupPath @"\update.exe");
Close();
Application.Exit();
}
(5)編寫客戶端的Update.Exe
Update.exe的功能主要是:首先就是找到主進(jìn)程;如果沒有關(guān)閉,則用Process.Kill()來關(guān)閉主程序。然后則用一個XmlDocument來Load程序生成的update.xml文件。用xml文件里指定的路徑和文件名來生成指定的文件,在這之前先前已經(jīng)存在的文件刪除。更新完畢后,則重新啟動主應(yīng)用程序。這樣更新就完成了。
private void Form1_Load(object sender, System.EventArgs e)
{
System.Diagnostics.Process[] ps = System.Diagnostics.Process.GetProcesses();
foreach(System.Diagnostics.Process p in ps)
{
//MessageBox.Show(p.ProcessName);
if(p.ProcessName.ToLower()=="customerapplication")
{
p.Kill();
break;
}
}
XmlDocument doc = new XmlDocument();
doc.Load(Application.StartupPath @"\update.xml");
XmlElement root = doc.DocumentElement;
XmlNode updateNode = root.SelectSingleNode("filelist");
string path = updateNode.Attributes["sourcepath"].Value;
int count = int.Parse(updateNode.Attributes["count"].Value);
for(int i=0;i= updateNode.ChildNodes[i];
string fileName = itemNode.Attributes["name"].Value;
FileInfo fi = new FileInfo(fileName);
fi.Delete();
//File.Delete(Application.StartupPath @"\" fileName);
this.label1.Text = "正在更新:" fileName " (" itemNode.Attributes["size"].Value ") ...";
FileStream fs = File.Open(fileName,FileMode.Create,FileAccess.Write); fs.Write(System.Convert.FromBase64String(itemNode.SelectSingleNode("value").InnerText),0,int.Parse(itemNode.Attributes["size"].Value));
fs.Close();
}
label1.Text = "更新完成";
File.Delete(Application.StartupPath @"\update.xml");
label1.Text = "正在重新啟動應(yīng)用程序...";
System.Diagnostics.Process.Start("CustomerApplication.exe");
Close();
Application.Exit();
}
這里為了簡單起見,沒有使用異步方法,當(dāng)然使用異步方法能更好的避免并發(fā)調(diào)用產(chǎn)生的沖突,這個需要讀者自己去添加。
4 結(jié)束語
借助Web Services實(shí)現(xiàn)軟件的自動升級,不僅設(shè)計簡單,實(shí)現(xiàn)起來也很容易,取得了良好的效應(yīng),大大減輕了維護(hù)的工作量。本方案具有較好的參考價值。
參考文獻(xiàn)
[1] 楊繼家,張麗靜,張曉蕾.面向C/S模式下的客戶端軟件自動升級的實(shí)現(xiàn)[J].微計算機(jī)應(yīng)用,2005(5),290-293
YANG Ji-jia,ZHANG Li-jing,ZHANG Xiao-lei.An realization of Automatically updating orienting to C/S Application System[J].Microcomputer Applications,2005(5),290-293
[2] 何航校,蔣兆遠(yuǎn).一種改進(jìn)的通用客戶端自動升級模型及實(shí)現(xiàn)[J].蘭州交通大學(xué)學(xué)報(自然科學(xué)版),2005(8),110-113
HE Hang-xiao,JIANG Zhao-yuan.An Improved Universal Auto Upgrade Model of Client and its Realization.Journal of Lanzhou Jiaotong University (Natural Sciences),2005(8),110-113
[3] 烏云高娃.動態(tài)升級在MIS 系統(tǒng)中的實(shí)現(xiàn)與應(yīng)用[J].計算機(jī)工程與設(shè)計,2005(10),2854-2856
WUYUN Gao-wa.Implementation and application of dynamic upgrade technique in MIS[J].Computer Engineering and Design,2005(10),2854-2856
[4] 葉利華,陶宏才,梁田.基于COM的軟件在線升級技術(shù)[J].成都信息工程學(xué)院學(xué)報,2005(2),73-75
YE Li-hua,TAO Hong-cai,LIANG Tian.Software live updating technology based on COM[J].Journal of Chengdu University of Information Technology,2005(2),73-75
[5] 余穎,董旭源,高宏.C/S模式管理信息系統(tǒng)實(shí)現(xiàn)自動升級和維護(hù)的方法[J].佳木斯大學(xué)學(xué)報(自然科學(xué)版),2005(4),200-202
Implementation of software auto-update by Web Services
Abstract: The software maintaining and updating is an important section in the software life cycle. This paper makes use of the technology of Web Services、C# and XML Language, to resolve the poor efficiency of client’s updating in old C/S application system. The automatically updating program can detect the new version, download the updating file, automatically backup and rollback. It makes the software updating by the client automatically, and it is practical.
Keywords: C#;Web Services;XML;software auto-update
轉(zhuǎn)載于:https://www.cnblogs.com/China-Dragon/archive/2009/05/14/1456981.html
總結(jié)
以上是生活随笔為你收集整理的利用Web Services实现软件自动升级的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 元气骑士如何获得机器人成就皮肤_元气骑士
- 下一篇: python定义符号常量_python从