web| Client | online
Objective:
For project reasons, the client to be implemented is too far away from the author (Itbaby) to consider providing an online upgrade of the software. How do we do that? Let's talk about the idea first.
Ideas:
First implement web-side development, mainly consider the use of WebService technology, provide remote service call function, return a file byte content, and then write an upgrade client, distributed to the customer's use of the machine, (can be installed with the customer's software). The client program primarily connects to Webserivce, and then saves the file to the local machine (the customer's machine). It can be achieved!
Details of implementation:
To consider providing a version of the customer software issue, a lower version of the upgrade, the latest version of the need to upgrade. Also consider the user name and password on the web side of the authentication!
Use technology:
asp.net webservice development, the client calls the WebService method asynchronously. Database Technology!
If reproduced please indicate the source, Http://blog.csdn.net/zerodj
Author Homepage: http://itbaby.jss.cn
Note: In itbaby.jss.cn, technical articles are no longer updated and all are transferred to BLOG.CSDN.NET/ZERODJ.
Start implementation:
1. Create a database, the author (Itbaby) uses SQLSERVER2000
1 software Project list: Softlist (Softid, Softname, resume, loginname, loginpwd)
Softid: Number
Softname: Software Name
Resume: Introduction
LoginName: Customer Login Name
LOGINPWD: Password
2 versions of each software table Softlistversion (Softid, SubID, version, Updatepath, Olefile)
Softid: The software number of the primary table
SubID: Each version data number
Version: Software versions
FileName: Upgrade filename
Olefile: Upgrade the binary content of the file, is the image type, (I mainly store the MSI installation package file type, you can use C # to do this kind of installation package file)
3 Establish a view, chkversion, 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 Create a second view, volefile, for downloading 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. Write a WebService
1 to start vs.net2003, create a project called BABYWEBSVC, Project type (asp.net Web service)
2) Add a softupdate.asmx Web service
3) Add a method searchversion
[WebMethod (description= "returns the highest version of the current software upgrade package")]
Public String Searchversion (String softname)
{
string sversion = ";
Webmod.dbconnstart (); (connection) The author's own connection database class, in which the user completes the database connection
string strSQL = "Select MAX (Version) as Maxverid from chkversion where soft name = @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 ()//(disconnected) the author's own connection database class, the user completes the database connection by itself
return sversion;
}
4 the way to add content to the download file Downloadsoft
[WebMethod (description= "returns the file bytes to download")]
Public byte[] Downloadsoft (string username,string password,string softdnldname,string softheightversion)
{
(connect) The author's own connection database class, the user completes the database connection
Webmod.dbconnstart ();
Check user legality
BOOL Bmember = Checkauth (Username,password);//A function within the webservice that examines the legality of the user, the user can do it himself
if (!bmember)
{
Webmod.dbconnend ();
return null;
}
Byte[] B = null;
We remove the upgrade package with the highest version of the specified software name
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[The byte content of the "olefile"];//file
}
Sqlrd.close ();
(disconnected) The author's own connection database class, the user completes the database connection
Webmod.dbconnend ();
return b;
}
3.WEB Service After the method is complete, you can start your own test, we now write the client's upgrade program, assuming that the URL of your webservice at development time is: Http://localhost/babywebsvc/SoftUpdate.asmx, Note that this URL we are going to refer to in the client
4. Start vs.net2003, build a C # Windows project, add a button to the default form,
5. Add a new file type (application configuration file) app.config
Contents of app.config file
<?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 "/><!--record the name of the software in the remote database-->
<add key= "Version" value= "1.0"/>
</appSettings>
</configuration>
6. We add the following code in the Load event that form starts
private void Form1_Load (object sender, System.EventArgs e)
{
Read the version number, which is set in AssemblyInfo.cs by the system itself, [Assembly:assemblyversion ("1.0")]
To change in the future, you can change the version number in the AssemblyInfo.cs here, for example: [Assembly:assemblyversion ("1.1")]
We need this data as a parameter in our WebService.
string sversion = Application.productversion;
Write to the app.cofing file, every time you call the WebService method, read the version from the app.cofing, you can also directly use application.productversion, I am for unified management, all from config read
This. Saveappconfig ("Version", sversion);
}
The contents of the Saveappconfig function
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. The main part, start calling the WebService Method!
Preparation: 1 Add a Web reference (first click "Project"-"Add Web Reference"), and in the pop-up, enter the path to the URL: http://localhost/babywebsvc/SoftUpdate.asmx
2) Assuming that you are developing the WebService Url:http://localhost/babywebsvc/softupdate.asmx
3) Fill in the Web reference name: autoupdatewebsvc
4 Click Next button to complete the add of Web references
8. Add the following code to your button1_click event, mainly using asynchronous calls
private string svcuser = "";
private string svcpwd = "";
private string svcsoftname = "";
private string svccurrversion = "";
private string svcdnldfilename = "Test.msi";//downloaded file name,
Private byte[] Fbyte = null; Content of the upgraded file after downloading
private void Button1_Click (object sender, System.EventArgs e)
{
Read configuration information from the app.config file
Svcuser = system.configuration.configurationsettings.appsettings["user"]; User name that requires a witness
SvcPwd = system.configuration.configurationsettings.appsettings["pwd"]; Authentication password
Svcsoftname = system.configuration.configurationsettings.appsettings["Babyrecordsoftname"];//software name
Svccurrversion = system.configuration.configurationsettings.appsettings["Version"];//current version number
Try
{
Autoupdatewebsvc.softupdate asvc = new Autoupdatewebsvc.softupdate ();
This can be changed to its actual application URL, whether the Web reference is dynamic or static, the call will point to the URL
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 if there is a high version of the upgrade software
Asvc.beginsearchversion (SVCSOFTNAME,CB,ASVC);
}
else if (Button1.Text.Trim () = "Upgrade")
{
Start calling the 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 "Your current version of the software is up to date, no need for upgrades ...");
else if ((String.Compare (Svccurrversion.trim (), Sversion.trim ()) ==-1)
{
MessageBox.Show ("Your current version of the software is lower, you can upgrade ...");
Button1.Text = "Upgrade";
}
}
catch (Exception ex)
{
MessageBox.Show (ex. message);
}
}
}
Call a remote Web service and start the download
private void Invokedownload ()
{
Try
{
Autoupdatewebsvc.softupdate asvc = new Autoupdatewebsvc.softupdate ();
This can be changed to its actual application URL, whether the Web reference is dynamic or static, the call will point to the URL
Asvc.url = "Http://localhost/babyWebSvc/SoftUpdate.asmx";
//Start download
system.asynccallback cb = new AsyncCallback (Downloadsoftcallback) ;//Asynchronous callback method, save file
Asvc.begindownloadsoft (Svcuser,svcpwd,svcdnldfilename,lblversion.text.trim (), cb,asvc);
}
catch (Exception ex)
{
MessageBox.Show (ex. message);
}
}
After the download method has finished executing, the asynchronous callback method
private void Downloadsoftcallback (System.IAsyncResult ar)
{
if (ar==null)
{
MessageBox.Show ("An error occurred during the upgrade and cannot be upgraded, please try again later ...");
Return
}
if (AR. iscompleted)
{
Try
{
Autoupdatewebsvc.softupdate asvc = (autoupdatewebsvc.softupdate) ar. asyncstate;
Fbyte = Asvc.enddownloadsoft (AR);
Asvc.dispose ();
Using threads, saving files
Thread th = new Thread (new ThreadStart (Save2disk));
Th. Start ();
}
catch (Exception ex)
{
MessageBox.Show ("An error occurred during the upgrade," +ex.) message);
}
}
}
Save a downloaded byte array as a file
private void Save2disk ()
{
Try
{
FileInfo finfo = new FileInfo (application.executablepath+svcdnldfilename);
if (finfo. Exists) Finfo. Delete ();//file exists remove it
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 ("Download complete, install upgrade now ...", "Hint info", Messageboxbuttons.okcancel, Messageboxicon.information,messageboxdefaultbutton.button1);
if (dr = = DialogResult.OK)
{
Execsetup ()//Start downloaded installer, user can complete
}
}
catch (Exception ex)
{
MessageBox.Show ("An error occurred during the upgrade," +ex.) message);
}
Uibutton2.enabled = true;
}
9: Summary, Client call, is from, click Buttton1 Start, search version number, searchversion, when you find a high version upgrade package, start the download method Downloadsoft, and then save to the local save2disk.
Regardless of whether the client's call is synchronous or asynchronous, the WebService method is written in the same way, except that the synchronous invocation is directly using the method name in WebService, and the asynchronous invocation is automatically generated by the system beginxxx () and EndXxx () by the method name, provided to you using the