首页 > 编程语言 >使用C#的WebService实现客户端软件的更新

使用C#的WebService实现客户端软件的更新

时间:2023-06-13 16:01:42浏览次数:67  
标签:WEB WebService dbo C# SoftUpdate private aSvc 客户端 string


由于项目原因,要实施的客户离作者太远,考虑提供软件的在线升级功能.我们如何实现呢!先讲下思路.

思路:
先实现WEB端的开发,主要考虑使用WEBService技术,提供远程服务的调用函数,返回一个文件的字节内容,然后写一个升级程序客户端,分发给客户使用的机器中,(可以随客户的软件一起安装).该客户端程序主要连接webserivce,然后将文件保存到本地机(客户的机器)中.就可以实现!

实现的细节:
要考虑提供给客户软件版本问题,低版本的升级,最新版本的就不用升级.还要考虑用户名与密码在WEB端的认证!

使用技术:
ASP.Net WebService开发,客户端的异步调用WebService方法.数据库技术!

开始实现:

1.建立数据库,使用SQLSERVER2000
1)软件项目表:softlist(softid, softname, resume, loginname, loginpwd)
softid:编号
softname:软件名称
resume:介绍
loginname:客户登录名
loginpwd:密码

2)各个软件的版本表 SoftListVersion(softid, subid, version, UpdatePath, olefile)
softid:主表的软件编号
subid:各版本数据编号
version:软件版本
filename:升级文件名
olefile:升级文件的二进制内容,是image类型,(我主要存放MSI的安装包文件类型,可以使用C#做此类安装包文件)

3)建立一个视图,chkVersion,用于检查版本号
SELECT dbo.SoftListVersion.subid, dbo.softlist.softname, dbo.SoftListVersion.version
FROM dbo.softlist INNER JOIN
        dbo.SoftListVersion ON dbo.softlist.softid = dbo.SoftListVersion.softid

4)再建立一个视图,vOleFile,用于下载文件
SELECT dbo.SoftListVersion.subid, dbo.softlist.softname, dbo.SoftListVersion.filename,
        dbo.SoftListVersion.olefile, dbo.SoftListVersion.version
FROM dbo.softlist INNER JOIN
        dbo.SoftListVersion ON dbo.softlist.softid = dbo.SoftListVersion.softid

2.写一个WEBSERVICE
1)启动VS.Net2003,建立一个叫babyWebSvc的项目,项目类型为(ASP.Net WEB服务)
2)添加一个SoftUpdate.asmx的WEB服务

3)添加一个方法SearchVersion

[WebMethod(Description="返回当前软件升级包的最高版本")]
public string SearchVersion(string softname)
{
   string sVersion = "";
   webmod.dbConnStart(); //(连接)作者自己的连接数据库类,用户自己完成数据库连接
   string strSQL = "select MAX(version) as MaxVerID from chkVersion where softname = @softname";
   SqlCommand sqlCmd = new SqlCommand(strSQL,webmod.sqlConn);
   sqlCmd.CommandTimeout = 0;
   sqlCmd.Parameters.Add("@softname",SqlDbType.VarChar).Value = softname;
   SqlDataReader sqlRd = sqlCmd.ExecuteReader();
   if(sqlRd.HasRows)
   {
    sqlRd.Read();
    sVersion = Convert.ToString(sqlRd["MaxVerID"]);
   }
   sqlRd.Close();
   
   webmod.dbConnEnd(); //(断开连接)作者自己的连接数据库类,用户自己完成数据库连接   return sVersion;
}

4)添加下载文件内容的方法DownloadSoft

[WebMethod(Description="返回需要下载的文件字节")]
public byte[] DownloadSoft(string UserName,string PassWord,string SoftDnldName,string SoftHeightVersion)
{
   //(连接)作者自己的连接数据库类,用户自己完成数据库连接
   webmod.dbConnStart();   //检查用户合法性
   bool bMember = CheckAuth(UserName,PassWord);//该WebService内的一个检查用户合法性的函数,用户可以自己完成
   if(!bMember)
   {
    webmod.dbConnEnd();
    return null;
   }   byte[] b = null;
   //我们取出指定软件名称的最高版本的升级包
   string strSQL = "select olefile from vOleFile where (filename=@softname) and version=@ver";
   SqlCommand sqlCmd = new SqlCommand(strSQL,webmod.sqlConn);
   sqlCmd.CommandTimeout = 0;
   sqlCmd.Parameters.Add("@softname",SqlDbType.VarChar).Value = SoftDnldName;
   sqlCmd.Parameters.Add("@ver",     SqlDbType.VarChar).Value = SoftHeightVersion;
   SqlDataReader sqlRd = sqlCmd.ExecuteReader();
   if(sqlRd.HasRows)
   {
    sqlRd.Read();
    b = (byte[])sqlRd["olefile"];//文件的字节内容
   }
   sqlRd.Close();   //(断开连接)作者自己的连接数据库类,用户自己完成数据库连接
   webmod.dbConnEnd();   return b;
}

3.WEB服务的方法完成后,你自己可以启动,测试,我们现在来写客户端的升级程序,假定你在开发时的WEBSERVICE的URL为:http://localhost/babywebsvc/SoftUpdate.asmx,注意这个URL,我们是要在客户端引用的
4.启动VS.Net2003,建立一个C#的Windows项目,在默认的FORM上添加一个按钮,
5.添加一个新的文件类型(应用程序配置文件)App.config
App.Config文件的内容

<?xml version="1.0" encoding="utf-8"?>
<configuration>
   <appSettings>
    <add key="user" value="test"/>
    <add key="pwd" value="test"/>
    <add key="babyRecordSoftName" value="TEST.EXE"/><!--记录在远程的数据库中的软件名称-->
    <add key="Version" value="1.0"/>
   </appSettings>
</configuration>

6.我们在Form启动的LOAD事件中,添加如下代码

private void Form1_Load(object sender, System.EventArgs e)
{
//读出版本号,该版本号是在AssemblyInfo.cs中由系统本身设置的,[assembly: AssemblyVersion("1.0")]
//以后要更改,可以改此处的AssemblyInfo.cs中的版本号,例:[assembly: AssemblyVersion("1.1")]
//我们的WEBSERVICE中需要这个数据做为参数string sVersion = Application.ProductVersion;
//写到App.Cofing文件中,每次调用WEBSERVICE方法时,从App.Cofing中读取版本,你也可以直接使用Application.ProductVersion,我是为了统一管理,全部从config中读取
this.SaveAppConfig("Version",sVersion);
}//SaveAppConfig函数的内容
public static void SaveAppConfig(string AppKey,string AppValue)
{
   XmlDocument xDoc = new XmlDocument();
   xDoc.Load(Application.ExecutablePath + ".config");   XmlNode xNode;
   XmlElement xElem1;
   XmlElement xElem2;   xNode = xDoc.SelectSingleNode("//appSettings");
   xElem1 = (XmlElement)xNode.SelectSingleNode("//add[@key=" + AppKey + "]");
   if ( xElem1 != null ) xElem1.SetAttribute("value",AppValue);
   else
   {
    xElem2 = xDoc.CreateElement("add");
    xElem2.SetAttribute("key",AppKey);
    xElem2.SetAttribute("value",AppValue);
    xNode.AppendChild(xElem2);
   }
   xDoc.Save(Application.ExecutablePath + ".config");
}

7.主要部分,开始调用webservice的方法!
准备工作:1)添加一个WEB引用,(先点菜单"项目"-"添加WEB引用"),在弹出中中输入url的路径:http://localhost/babywebsvc/SoftUpdate.asmx
   2)假定你在开发时的WEBSERVICE的URL:http://localhost/babywebsvc/SoftUpdate.asmx   3)填入WEB引用名:AutoUpdateWebSvc
   4)点下按纽完成WEB引用的添加

8.在你的Button1_click事件中添加如下CODE,主要使用异步调用

private string svcUser = "";
private string svcPwd = "";
private string svcSoftName = "";
private string svcCurrVersion = "";
private string svcDnldFileName = "Test.MSI";//下载下来的文件名,
private byte[] fbyte = null; //下载后的升级文件的内容
private void Button1_Click(object sender, System.EventArgs e)
{
//读取App.config文件中的配置信息
svcUser = System.Configuration.ConfigurationSettings.AppSettings["user"]; //需要人证的用户名
svcPwd = System.Configuration.ConfigurationSettings.AppSettings["pwd"];   //认证密码
svcSoftName = System.Configuration.ConfigurationSettings.AppSettings["babyRecordSoftName"];//软件名称
svcCurrVersion = System.Configuration.ConfigurationSettings.AppSettings["Version"];//当前版本号   try
   {
    AutoUpdateWebSvc.SoftUpdate aSvc = new AutoUpdateWebSvc.SoftUpdate();    //此处可以改成自己实际应用时的URL,不管WEB引用是动态还是静态,调用都会指向该URL
    aSvc.Url = "http://localhost/babyWebSvc/SoftUpdate.asmx";    if(Button1.Text.Trim() == "检 查")
    {
     //检查最新版本
     System.AsyncCallback cb = new AsyncCallback(SearchVersionCallBack);//异步回调方法,并检查是否有高版本的升级软件存在
     aSvc.BeginSearchVersion(svcSoftName,cb,aSvc);
    }
    else if(Button1.Text.Trim() == "升 级")
    {
     //开始调用下载服务
     InvokeDownload(); //函数体见下面的CODE
    }
    
   }
   catch(Exception ex)
   {
    MessageBox.Show(ex.Message);
   }
}//检查最新版本的异步回调方法
private void SearchVersionCallBack(System.IAsyncResult ar)
{
   if(ar==null)return;
   if(ar.IsCompleted)
   {
    try
    {
     AutoUpdateWebSvc.SoftUpdate aSvc = (AutoUpdateWebSvc.SoftUpdate)ar.AsyncState;
     string sVersion = aSvc.EndSearchVersion(ar);
     aSvc.Dispose();     
     if(svcCurrVersion.Trim() == sVersion.Trim())
      MessageBox.Show"你的软件当前版本已经是最新的了,无需进行升级...");
     else if((string.Compare(svcCurrVersion.Trim(),sVersion.Trim()))==-1)
     {
      
      MessageBox.Show("你的软件当前版本比较低,可以进行升级...");
      Button1.Text = "升 级";
     }    }
    catch(Exception ex)
    {
     MessageBox.Show(ex.Message);
    }
   }
} 
//调用远程的WEB服务,开始下载
private void InvokeDownload()
{
   try
   {
    AutoUpdateWebSvc.SoftUpdate aSvc = new AutoUpdateWebSvc.SoftUpdate();
    //此处可以改成自己实际应用时的URL,不管WEB引用是动态还是静态,调用都会指向该URL
    aSvc.Url = "http://localhost/babyWebSvc/SoftUpdate.asmx";    //开始下载
    System.AsyncCallback cb = new AsyncCallback(DownloadSoftCallBack);//异步回调方法,保存文件
    aSvc.BeginDownloadSoft(svcUser,svcPwd,svcDnldFileName,lblVersion.Text.Trim(),cb,aSvc);
    
   }
   catch(Exception ex)
   {
    MessageBox.Show(ex.Message);
   }
}//下载方法执行完成后,异步回调方法
private void DownloadSoftCallBack(System.IAsyncResult ar)
{
   if(ar==null)
   {
    MessageBox.Show("升级过程中出现错误,不能进行升级,请稍后再试...");
    return;
   }
   if(ar.IsCompleted)
   {
    try
    {
     AutoUpdateWebSvc.SoftUpdate aSvc = (AutoUpdateWebSvc.SoftUpdate)ar.AsyncState;
     fbyte = aSvc.EndDownloadSoft(ar);
     aSvc.Dispose();     //使用线程,保存文件
     Thread th = new Thread(new ThreadStart(Save2Disk));
     th.Start();    }
    catch(Exception ex)
    {
     MessageBox.Show("升级过程中出现错误,"+ex.Message);
    }
   }
}//将下载下来的字节数组保存成文件
private void Save2Disk()
{
   try
   {
    FileInfo finfo = new FileInfo(Application.ExecutablePath+svcDnldFileName);
    if(finfo.Exists)finfo.Delete();//文件存在就删除它
    Stream stream = finfo.OpenWrite();    prosBar.Maximum = fbyte.Length;//prosBar是一个进度条
    prosBar.Minimum = 0;
    prosBar.Step = 1;
    int i=0;
    foreach(byte b in fbyte)
    {
     stream.WriteByte(b);
     prosBar.Value += 1;
    }
    stream.Flush();
    stream.Close();    DialogResult dr = MessageBox.Show("下载完成,是否现在就安装升级程序...","提示信息",MessageBoxButtons.OKCancel,MessageBoxIcon.Information,MessageBoxDefaultButton.Button1);
    if(dr == DialogResult.OK)
    {
     ExecSetup();//启动下载下来的安装程序,用户可以自己完成
    }
   }
   catch(Exception ex)
   {
    MessageBox.Show("升级过程中出现错误,"+ex.Message);
   }
   uiButton2.Enabled = true;
}

9:总结,客户端调用,是从,点击Buttton1开始,搜索版本号,SearchVersion,当找到高版本升级包时,开始执行下载的方法DownloadSoft,然后保存到本地Save2Disk.
不管客户端的调用是同步还是异步,WEBService的方法都是一样写的,只不过同步调用,是直接使用WEBService中的方法名称,异步调用则会由系统自动生成BeginXXX()与EndXXX()的方法名称,提供给你使用

标签:WEB,WebService,dbo,C#,SoftUpdate,private,aSvc,客户端,string
From: https://blog.51cto.com/guosisoft/6471013

相关文章

  • 基于.NET的Web Service技术的分布式异构数据库的集成
    摘要:本文分析了WebService的特点,提出了一种基于Microsoft.NET的WebService技术访问分布异构数据库的体系结构,并采用.NET技术实现了原型系统。在原型系统中,使用WebService将分布于Internet上的不同的数据库系统中的数据集成,向访问数据库的应用程序提供统一的数据操作接口,实现......
  • Apache Http Server 路径穿越漏洞复现(CVE-2021-41773)
    ApacheHttpServer路径穿越漏洞复现ApacheHttpServer路径穿越漏洞概述ApacheHttpServer简介ApacheHTTPServer(简称Apache)是Apache软件基金会的一个开放源码的网页服务器软件,可以在大多数电脑操作系统中运行。由于其跨平台和安全性,被广泛使用,是最流行的Web服务器......
  • 【pyqt】报错TypeError: decorated slot has no signature compatible with RecorderP
    一、场景  运行pyqt报错TypeError:decoratedslothasnosignaturecompatiblewithRecorderPlayerProxy.sig_mode_update[object] 二、代码@Slot(int)defupdate_mode(self,mode):...... 三、解决方法  将int去除即可  参考链接:p......
  • NVIDIA Maxine Video Effects SDK 編程指南 - 实践小记
    NVIDIAMaxineVideoEffectsSDK編程指南 -实践小记本篇博客重点只说VideoEffect的部分,此外还有AudioEffect的部分、还有AR部分,不在本篇范围内。本文由重庆Debug原创NVIDIAMaxineVideoEffects支持基于AI的视觉效果,这些效果可以输入标准网络摄像头画面数据,同时也可以......
  • c#排序算法
    1.没有一种排序算法是万能的最快算法,因为最快的排序算法取决于数据的性质和排序要求。然而,对于一般情况下的排序问题,以下算法通常被认为是最快的:快速排序(QuickSort):这是一种基于分治思想的常见排序算法。其平均时间复杂度为O(nlogn)。因为其平均情况下时间复杂度相对较快,加上......
  • package.json中的版本号
    NPM的语义版本控制在发布NPM模块新版本时,建议遵循“语义版本控制”考虑使用这样的版本号x.y.z控制,如下所示:版本号规则:主版本:做了不兼容的API修改,不会向前兼容,一般也称为大版本,当项目依赖需要升级到大版本时需要注意。次版本:通常是做了向前兼容的新功能增加,一般也称为......
  • 利用dotnet core的代码生成实现类型转换
    利用dotnetcore的代码生成的特性,自动生成类型转换的代码。类似于AutoMaper,但是代码生成近似于手写代码,不用反射,性能更好生成通过比较属性名字(不区分大小写)属性支持简单类型,类,List,Dictionary(key最好是string类型)在需要转换的类上标记特性:ConvertFrom、ConvertTo[Conv......
  • Remove Duplicates from Sorted Array
    Example1:Input:nums=[1,1,2]Output:2,nums=[1,2,_]Explanation:Yourfunctionshouldreturnk=2,withthefirsttwoelementsofnumsbeing1and2respectively.Itdoesnotmatterwhatyouleavebeyondthereturnedk(hencetheyareunderscores)......
  • javascript反编译工具javascript-obfuscator的环境搭建
    javascript-obfuscator的项目和文档地址:https://github.com/javascript-obfuscator/javascript-obfuscatorwindows端安装nodejs环境打开nodejs安装包,一直点NEXT,默认设置安装即可。安装后:#测试nodejs和npm是否已安装npm-v#如果有输出版本号,例如输出9.5.0,表示安装成功#查看......
  • C语言-观察者模式
    点击查看代码#include<stdio.h>#defineMAX_OBSERVERS10typedefstructObserver{intOberver_value;void(*update)(structObserver*observer,intvalue);}Observer;typedefstructSubject{intvalue;Observer*observes[MAX_OBSE......