利用Web Services实现软件自动升级

时间:2022-02-27 04:07:07

利用Web Services实现软件自动升级

利用web services实现软件自动升级 曹庆旭 (黔东南民族职业技术学院,贵州,凯里,556000)

摘 要:软件维护升级工作是软件生命周期最重要的环节。为了解决以往c/s(client/server)模式下的客户端软件升级效率低的问题,设计了c/s应用系统自动升级处理程序。该程序利用web services技术、c#和xml语言,通过网络来完成c/ s应用系统的自动升级。与原有手工升级、ftp 文件服务器升级和第三方控件升级相比,升级效率更高。该方案具有较好的参考价值。

关键词:c#;web services;xml;软件自动升级

中图法分类号: 文献标志码:

1 引言

随着计算机网络应用技术的不断发展,在开发mis系统时,大多采用基于c/s(客户机/服务器)模式或b/s(浏览器/服务器)模式。现在b/s模式以其真正意义上的瘦客户机/胖服务器模式优势占据了主导地位。但是由于客户机/服务器模式具有的数据流量小、响应时间短、安全性高等特点,在解决几十个到几百个用户的局域网中,仍然是一个不错的选择[1-3]。在c/s模式下,应用程序的每次升级都需要在每个客户端重新安装应用程序,这是一项十分繁琐的事情。面对这个实际问题,这里设计了一个通过软件实现自动升级技术方案,弥补了这一缺陷,有较好的参考价值。

2 设计思路

判断一个文件是否要更新,可以通过判断文件的大小、修改日期和文件的版本号来实现[3-5]。发现最新的则提示用户是否升级。

在web services中实现一个getver的webmethod方法,其作用是获取当前的最新版本。然后将现在版本与最新版本比较,如果有新版本,则进行升级。

3 自动升级的技术实现

(1)编写升级模板文件update.xml

准备一个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的一个组件,该组件负责控制对从web 站点返回的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()

{

//验证用户是否登陆

if(sheader==null)

return null;

if(!dataprovider.getinstance.checklogin(sheader.username,sheader.password))

return null;

//取得更新的xml模板内容

xmldocument doc = new xmldocument();

doc.load(server.mappath("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);

//将xml中的value用实际内容替换

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);

//这里是文件的实际内容,使用了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(pareto(nver)<=0)

update();

在本代码中start.getservice是websvs的一个静态实例。功能是:首先检查版本,将结果与当前版本进行比较,如果为新版本则执行update方法。

update的作用是将升级的xml文件下载下来,保存为执行文件目录下的一个update.xml文件。任务完成,退出程序,等待update.exe 来进行升级。

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的功能主要是:首先就是找到主进程;如果没有关闭,则用process.kill()来关闭主程序。然后则用一个xmldocument来load程序生成的update.xml文件。用xml文件里指定的路径和文件名来生成指定的文件,在这之前先前已经存在的文件删除。更新完毕后,则重新启动主应用程序。这样更新就完成了。

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 = "正在重新启动应用程序...";

system.diagnostics.process.start("customerapplication.exe");

close();

application.exit();

}

这里为了简单起见,没有使用异步方法,当然使用异步方法能更好的避免并发调用产生的冲突,这个需要读者自己去添加。

4 结束语

借助web services实现软件的自动升级,不仅设计简单,实现起来也很容易,取得了良好的效应,大大减轻了维护的工作量。本方案具有较好的参考价值。

参考文献

[1] 杨继家,张丽静,张晓蕾.面向c/s模式下的客户端软件自动升级的实现[j].微计算机应用,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] 何航校,蒋兆远.一种改进的通用客户端自动升级模型及实现[j].兰州交通大学学报(自然科学版),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] 乌云高娃.动态升级在mis 系统中的实现与应用[j].计算机工程与设计,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的软件在线升级技术[j].成都信息工程学院学报,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模式管理信息系统实现自动升级和维护的方法[j].佳木斯大学学报(自然科学版),2005(4),200-202

yu ying,dong xu-yuan,gao hong.methods to perform upgrade and maintenance in c/s mode management information system[j],2005(4),200-202

implementation of software auto-update by web services

cao qing-xu

(qiandongnan vocational &technical institute,kaili,guizhou,556000)

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

上一篇:试论网络会计存在的词题及主要对策 下一篇:略一种基于混沌搜索的文化算法及其应用