WebService using C # to achieve the client software upgrade

2009-09-15  来源:本站原创  分类:Development  人气:491 

Due to the reasons for the project, to implement customers too far from the author, considering the provision of online software upgrade capability. How do we achieve it! To talk about the next idea.

Ideas:
WEB client to achieve the development, the main WEBService consider the use of technology to provide remote service call function, return the contents of a file of bytes, and then write a program to upgrade the client and distributed to customers to use the machine, (the software can work together with customers installation). The client program to connect major webserivce, and then save the file to the local machine (client machines) in. can be achieved!

The realization of the details:
To be considered available to the client software version of the problem, and low version of the upgrade, the latest version of the upgrade will not need to. Should also be considered in the username and password client authentication WEB!

The use of technology:
ASP.Net WebService development, asynchronous client call WebService method. Database technology!

The beginning of the realization of:

1. The establishment of databases, the use of SQLSERVER2000
1) software list: softlist (softid, softname, resume, loginname, loginpwd)
softid: No.
softname: Software Name
resume: Introduction
loginname: client login name
loginpwd: password

2) various software version table SoftListVersion (softid, subid, version, UpdatePath, olefile)
softid: the main form of software code
subid: the version number of data
version: Software Version
filename: upgrade file name
olefile: binary file contents of the upgrade is the image type, (I kept the installation of MSI package file types, you can use C # to do such installation package file)

3) the establishment of a view, chkVersion, used to check the version number
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) the establishment of a view, vOleFile, used to download files
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 to write a
1) Start VS.Net2003, the establishment of a project called babyWebSvc project type (ASP.Net WEB services)
2) Add a SoftUpdate.asmx of WEB services

3) Add a method SearchVersion

[WebMethod (Description = "Return to the current software version upgrade package of the highest")]
public string SearchVersion (string softname)
(
string sVersion = "";
webmod.dbConnStart (); / / (connection) to connect the author's own database category, the completion of the user's own database connection
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 (); / / (disconnect) the author's own database connection class, the completion of the user's own database connection

return sVersion;
)

4) Add the method of download the contents of the documents DownloadSoft

[WebMethod (Description = "Return to the document will need to download bytes")]
public byte [] DownloadSoft (string UserName, string PassWord, string SoftDnldName, string SoftHeightVersion)
(
/ / (Connection) to connect the author's own database category, the completion of the user's own database connection
webmod.dbConnStart ();

/ / Check the legitimacy of the user
bool bMember = CheckAuth (UserName, PassWord); / / the WebService with the legality of a checkpoint function of the user, the user can complete the
if (! bMember)
(
webmod.dbConnEnd ();
return null;
)

byte [] b = null;

/ / We removed the name of the specified maximum version of the software upgrade package
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 "];// file byte content
)
sqlRd.Close ();

/ / (Disconnect) the author's own database connection class, the completion of the user's own database connection
webmod.dbConnEnd ();

return b;
)

3.WEB after the completion of service, you can start testing, we are now to write the client upgrade process, it is assumed that when you WEBSERVICE in the development of the URL for: http://localhost/babywebsvc/SoftUpdate.asmx, to pay attention to this URL, we are quoted on the client
4. Start VS.Net2003, the establishment of a C # for Windows projects, the default FORM to add a button,
5. To add a new file type (application configuration file) App.config
App.Config document

<? 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"/> <! - recorded in the long-range software of the database name ->
<add key="Version" value="1.0"/>
</ appSettings>
</ configuration>

6. We start in the Form of the LOAD event, add the following code

private void Form1_Load (object sender, System.EventArgs e)
(
/ / Read the publication of the number, the version number in AssemblyInfo.cs is set by the system itself, [assembly: AssemblyVersion ( "1.0")]
/ / To change the future, you can change here is the version number in AssemblyInfo.cs, for example: [assembly: AssemblyVersion ( "1.1")]
/ / We need this data WEBSERVICE as a parameter

string sVersion = Application.ProductVersion;

/ / Write the document App.Cofing each call WEBSERVICE method, read from App.Cofing version, you can directly use Application.ProductVersion, I managed to unify all read from the config
this.SaveAppConfig ( "Version", sVersion);
)

/ / SaveAppConfig function of the content
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. A major part of the beginning of the method to call webservice!
Preparations: 1) Add a WEB reference, (the first point of the menu "Project" - "add to quote WEB"), enter in the pop-up in the url path: http://localhost/babywebsvc/SoftUpdate.asmx
2) the assumption that when you WEBSERVICE in the development of the URL: http://localhost/babywebsvc/SoftUpdate.asmx
3) Fill WEB cited were: AutoUpdateWebSvc
4) completion point under the button to add quoted WEB

8. Button1_click in your case, add the following CODE, the main use of asynchronous calls

private string svcUser = "";
private string svcPwd = "";
private string svcSoftName = "";
private string svcCurrVersion = "";
private string svcDnldFileName = "Test.MSI"; / / download from the file name,
private byte [] fbyte = null; / / download the upgrade file contents
private void Button1_Click (object sender, System.EventArgs e)
(
/ / Read the configuration file App.config information
svcUser = System.Configuration.ConfigurationSettings.AppSettings [ "user"]; / / the user name need witnesses
svcPwd = System.Configuration.ConfigurationSettings.AppSettings [ "pwd"]; / / Authentication Password
svcSoftName = System.Configuration.ConfigurationSettings.AppSettings [ "babyRecordSoftName "];// Software Name
svcCurrVersion = System.Configuration.ConfigurationSettings.AppSettings [ "Version "];// the current version number

try
(
AutoUpdateWebSvc.SoftUpdate aSvc = new AutoUpdateWebSvc.SoftUpdate ();

/ / Here can be changed at the time of the practical application of their own URL, regardless of WEB is dynamic or static reference, call the URL will point to
aSvc.Url = "http://localhost/babyWebSvc/SoftUpdate.asmx";

if (Button1.Text.Trim () == "check")
(
/ / Check the latest version
System.AsyncCallback cb = new AsyncCallback (SearchVersionCallBack); / / Asynchronous callback method, and check whether there is a high version of the upgrade software
aSvc.BeginSearchVersion (svcSoftName, cb, aSvc);
)
else if (Button1.Text.Trim () == "upgrade")
(
/ / The beginning of call download service
InvokeDownload (); / / function body, see the following CODE
)

)
catch (Exception ex)
(
MessageBox.Show (ex.Message);
)
)

/ / Check the latest version of the asynchronous callback method
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 "the current version of your software is up-to-date, and the need to upgrade ...");
else if ((string.Compare (svcCurrVersion.Trim (), sVersion.Trim ()))==- 1)
(

MessageBox.Show ( "your current version of the software is relatively low, you can upgrade ...");
Button1.Text = "upgrade";
)

)
catch (Exception ex)
(
MessageBox.Show (ex.Message);
)
)
)

/ / Call remote WEB services, start the download
private void InvokeDownload ()
(
try
(
AutoUpdateWebSvc.SoftUpdate aSvc = new AutoUpdateWebSvc.SoftUpdate ();
/ / Here can be changed at the time of the practical application of their own URL, regardless of WEB is dynamic or static reference, call the URL will point to
aSvc.Url = "http://localhost/babyWebSvc/SoftUpdate.asmx";

/ / Start the download
System.AsyncCallback cb = new AsyncCallback (DownloadSoftCallBack); / / Asynchronous callback method, save the file
aSvc.BeginDownloadSoft (svcUser, svcPwd, svcDnldFileName, lblVersion.Text.Trim (), cb, aSvc);

)
catch (Exception ex)
(
MessageBox.Show (ex.Message);
)
)

/ / Download method of implementation is complete, asynchronous callback method
private void DownloadSoftCallBack (System.IAsyncResult ar)
(
if (ar == null)
(
MessageBox.Show ( "error during the upgrade can not upgrade, please try again later ...");
return;
)
if (ar.IsCompleted)
(
try
(
AutoUpdateWebSvc.SoftUpdate aSvc = (AutoUpdateWebSvc.SoftUpdate) ar.AsyncState;
fbyte = aSvc.EndDownloadSoft (ar);
aSvc.Dispose ();

/ / Use thread, save the file
Thread th = new Thread (new ThreadStart (Save2Disk));
th.Start ();

)
catch (Exception ex)
(
MessageBox.Show ( "error during the upgrade," + ex.Message);
)
)
)

/ / Will be downloaded into the byte array to preserve documents
private void Save2Disk ()
(
try
(
FileInfo finfo = new FileInfo (Application.ExecutablePath + svcDnldFileName);
if (finfo.Exists) finfo.Delete ();// file exists delete it on
Stream stream = finfo.OpenWrite ();

prosBar.Maximum = fbyte.Length; / / prosBar is a progress bar
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 ( "the download is complete, whether or not to install the upgrade process now ..."," message", MessageBoxButtons.OKCancel, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1);
if (dr == DialogResult.OK)
(
Download ExecSetup ();// start the installation process, users can complete their own
)
)
catch (Exception ex)
(
MessageBox.Show ( "error during the upgrade," + ex.Message);
)
uiButton2.Enabled = true;
)

9: To sum up, the client call is from, click Buttton1 started, search the version number, SearchVersion, when to find the high-version upgrade package to begin the implementation of the method to download DownloadSoft, and then saved to the local Save2Disk.
Whether the client is synchronous or asynchronous calls, WEBService write the same way, the only synchronous calls, direct way to use the name of WEBService, asynchronous call will be automatically generated by the system BeginXXX () and EndXXX () of method name, and made available to you

相关文章