Even More Windows Secrets for Visual Basic .NET, Page 5
Best of All Worlds: Creating an Ultra-Thin Client
Talk to 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 new 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 when opened? You can, by using reflection in .NET, a new technique allowing your code to 'see' other code—a sort of more-advanced late binding.
Here's how it works: First, you create your application and put the resulting assembly (.EXE or .DLL) on the Internet or an Intranet. Next, you create a small 'loader' application for your users. This application then uses reflection to load your assembly from the Internet and manipulate it—perhaps displaying a form from within it.
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( _ "app.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.
So, you may create your 'loader' application, then put code similar to the above behind a button, or the form's Load event. This will download the latest version of you application from the Net, ready for you to display a form within it (for example).
Also, .NET is pretty clever. If the application you've 'loaded' references an external assembly, it will go back to your Web server to check for it. That means, if you're feeling exceptionally smart, you might just want to split your one large application out into multiple parts. For example, in this newly downloaded application, 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, and then continue with your code.
This stops any large delays in downloading your main application, as your application is now split into many smaller 'parts,' all of which can work together without problem.
If you're not too concerned with speed, however, just stick with the basics: creating a loader to download the latest version of your main application from the Net. Whenever you want clients to use a new version, simply replace your existing online assemblies—and Bob's your Uncle.
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.
Well, that's how to automatically update your Windows programs and have the best of both worlds: complete control and no maintenance worries.
Enter stage left: return of the fat client. Hurrah and huzzah!
Coming up in the next article, part one of ASP.NET Secrets:
- Displaying Web Warning Messages: Technique 1
- Displaying Web Warning Messages: Technique 2
- Unveiled: How to Create a Default 'Enter' Button!
- Wonders of the Little-Known SmartNavigation Property
- The Secret Behind User Controls
See you then!
About the Author
Karl Moore is a technology author living in Yorkshire, England. He runs his own consultancy group, White Cliff Computing Ltd, and is author of two best-selling books exposing the secrets behind Visual Basic .NET. When he's not writing for magazines, speaking at conferences, or making embarrassing mistakes on live radio, Karl enjoys a complete lack of a social life. You can visit Karl on the Web at www.karlmoore.com.
# # #