Creating an Ultra-Thin Client
Chat with any modern developer about a new system you require and they'll instantly start talking about Web-based applications. Why? There's a very simple reason. Because 'fat' Windows programs are just too difficult to maintain.
Who has which version? How can you ensure all your employees start using the new system at the same time? There were workarounds... but why bother, when you can simply create a Web application?
The problem is, 'thin' Web applications are inflexible. They don't give you full control. Your typical Web page can't send something to the user's printer, nor save files to a special area on the hard drive. They're often slow or unavailable.
In brief, Windows programs are much better in terms of control, but worse in terms of maintainability. Web applications are great in terms of maintainability, but terrible in terms of control.
But what if you could solve this problem, by automatically updating your Windows applications? You can, with just a few lines of extra code. And over the next couple of screens, I'm going to share my two favourite techniques for doing just that.
Downloading Code 'Live' from a Web Server
The first technique works best for smaller applications, or programs that have portions of them that update frequently. It's based on something called reflection, a new .NET method of allowing your code to 'see' other code, a sort of more advanced late binding.
Here's how it works: You begin by creating the portion of your application that requires updating (it may even be an entire program in its own right). Then, you make your assembly available on your Web server, typically on the Intranet.
Next, you create an application that loads your assembly from the Intranet and manipulates it, using reflection to perhaps display a form from inside the assembly. This application can either be a small 'loader' application, or a regular program with a few lines of code for displaying the frequently changing portion.
It may sound complicated, but there are really just a couple of lines of code you'll need to use. And here they are:
' Load assembly Dim MyAssembly As System.Reflection.Assembly = _ System.Reflection.Assembly.LoadFrom("http://address/app.exe") ' Create instance of the form and show it Dim MyForm As Form = _ MyAssembly.CreateInstance("YourAssemblyNamespace.Form1") MyForm.Show()
Here, the code loads the assembly from a Web address, creates a new instance of the 'Form1' class within the assembly, and then shows that form. You could add this sort of code to the Sub Main method of your 'loader' application, or behind one of your menu items—and hey presto, you've got a live application. When you need to update, simply replace your assembly on the Web server.
Also, .NET is pretty clever. If the assembly you've 'loaded' references another assembly, it will go back to your Web server to check for it. That means, feeling exceptionally smart, you might just want to split your 'live' application out into multiple parts. For example, in this newly downloaded assembly, you may refer to another assembly and display a form from within that too. When the local machine spots this code, .NET will go back to the Web server, attempt to load that assembly, then continue with your code. It's a method of splitting up portions of your program and accessing them on demand. This stops any large delays in downloading the code because the application is now split into many smaller 'parts', all of which can work together without a problem.
But, as ever, there's something you need to watch out for. It's security. By default, the .NET Framework only partially trusts applications downloaded from the Internet and hence restricts exactly what they can do on your machine.
To sort this situation, from the Control Panel, choose Administrative Tools > Microsoft .NET Framework Wizards. Double-click the 'Trust an Assembly' option, specify your assembly Internet address, and grant it full permissions. There are other workarounds too, such as adding the site your assembly is hosted on to the list of Internet Explorer 'Trusted Sites', and then using the 'Adjust .NET Security' wizard to grant all trusted sites with full permissions—but they're all for another day. Look up 'code access security' in the Help Index for more information.