Autodeploy the Easy Way
You can autodeploy assemblies to the client with a couple lines of code.
VSLive! SF, Day 1, February 12, 2002 - Like most people, you have probably run into deployment issues with COM and ActiveX components in the past. This is one of the biggest problems Microsoft developers face.
Microsoft .NET tackles this issue head-on by avoiding the Windows registry and providing a strong versioning scheme for assemblies (components). Better still, with just a couple lines of code, you can autodeploy assemblies to the client. This means that all the DLLs containing your application's UI and business logic will download automatically to the client machine, with no effort on your part.
To use autodeployment, you need to create a tiny .NET program installed on the client machine. This program pulls down only the first DLL of your application-the rest follow automatically, with this code:
Dim appAsm As [Assembly]
Dim formType As Type
Dim obj As Object
Dim af As Form
appAsm = [Assembly].LoadFrom( _
"http://localhost/myapp/myapp.dll")
formType = appAsm.GetType("myapp.MainForm")
obj = Activator.CreateInstance(formType)
af = CType(obj, Form)
Application.Run(af)
The code assumes that myapp.dll will contain a form named MainForm. That form is the application's starting point. This works great, unless you also want to use serialization, another important .NET feature.
You use serialization to make an exact copy, or clone, of an object. You can make this copy within your application, but more importantly you can make it across the network. By copying an object across the network, you can create and populate an object on your server, then return it to the client and have it actually move to the client machine. This means you can create truly distributed objects.
Unfortunately, serialization won't work in code autodownloaded using the previous code snippet. The deserialization process will fail because it can't find the assembly that contains your object's code. This is because assemblies loaded by using the Assembly object's LoadFrom method are treated slightly differently than assemblies loaded normally.
You can overcome that problem with this code:
Dim appAsm As [Assembly]
Dim formType As Type
Dim obj As Object
Dim af As Form
Dim currentDomain As AppDomain
currentDomain = AppDomain.CurrentDomain
AddHandler currentDomain.AssemblyResolve, _
AddressOf MyResolveEventHandler
appAsm = [Assembly].LoadFrom( _
"http://localhost/myapp/myapp.dll")
formType = appAsm.GetType("myapp.MainForm")
obj = Activator.CreateInstance(formType)
af = CType(obj, Form)
Application.Run(af)
Back to top
|