智能客户端之动态更新下载

智能客户端在.NET1.1.NET2.0方面,对应智能客户端的最重要特性智能升级支持是不相同的,在.NET 2.0以前,用的是Updater Application Block for .NET,也有不少资料介绍如果使用这些类。可这个Block不支持.NET 2.0,那是如何实现智能升级的那,一直纳闷。

偶尔这天,翻到资料,在msdn

http://msdn2.microsoft.com/zh-cn/library/6hbb4k3e(VS.80).aspx

有《部署 .NET Framework 应用程序》的文章,对于一般性应用,简单到极点,就是ClickOne,一个简单发布,不用修改任何程序。

但对于那些大型应用,类都发布成一个个DLL,用户不同权限执行不同的DLL。如何有选择性的下载DLL,成了实现这类目标的关键。

msdnhttp://msdn2.microsoft.com/zh-cn/library/ms228997(VS.80).aspx)和随机帮助,都有文章《使用 ClickOnce 部署 API 按需下载程序集》,有步骤说明如何做,就是偏偏没有代码,最后,终于在网络上找到源码(https://209.34.241.67/winformsue/archive/2006/01/17/513922.aspx),调试通过,贴上

//ClickOnceLibrary.cs
using System;
using System.Collections.Generic;
using System.Text;

namespace OnDemandAssembly
{
    
public class DynamicClass
    
{
        
private string sMessage="Hello";
        
public string Message
        
{
            
get
            
{
                
return sMessage;
            }

            
set
            
{
                sMessage 
= value;
            }

        }



    }

}


//ClickOnceOnDemand.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using System.Deployment.Application;
using OnDemandAssembly;
using System.Reflection;


using System.Security.Permissions;

namespace ClickOnceOnDemand
{
    
public partial class Form1 : Form
    
{
        
//<SNIPPET2>
        
// Maintain a dictionary mapping DLL names to download file groups. This is trivial for this sample,
        
// but will be important in real-world applications where a feature is spread across multiple DLLs,
        
// and you want to download all DLLs for that feature in one shot. 
        Dictionary<String, String> DllMapping = new Dictionary<String, String>();

        [SecurityPermission(SecurityAction.Demand, ControlAppDomain 
= true)]

        
public Form1()
        
{
            InitializeComponent();
            DllMapping[
"ClickOnceLibrary"= "ClickOnceLibrary";
            AppDomain.CurrentDomain.AssemblyResolve 
+= new ResolveEventHandler(CurrentDomain_AssemblyResolve);
        }



        
/*
         * Use ClickOnce APIs to download the assembly on demand.
         
*/

        Assembly CurrentDomain_AssemblyResolve(
object sender, ResolveEventArgs args)
        
{
            Assembly newAssembly 
= null;

            
if (ApplicationDeployment.IsNetworkDeployed)
            
{
                ApplicationDeployment deploy 
= ApplicationDeployment.CurrentDeployment;

                
// Get the DLL name from the Name argument.
                string[] nameParts = args.Name.Split(',');
                
string dllName = nameParts[0];
                
string downloadGroupName = DllMapping[dllName];

                
try
                
{
                    deploy.DownloadFileGroup(downloadGroupName
+".dll");
                }

                
catch (DeploymentException de)
                
{
                    MessageBox.Show(
"Downloading file group failed. Group name: " + downloadGroupName + "; DLL name: " + args.Name);
                    
throw (de);
                }


                
// Load the assembly.
                
// Assembly.Load() doesn't work here, as the previous failure to load the assembly
                
// is cached by the CLR. LoadFrom() is not recommended. Use LoadFile() instead.
                try
                
{
                    newAssembly 
= Assembly.LoadFile(Application.StartupPath + @"\" + dllName + ".dll");
                }

                
catch (Exception e)
                
{
                    
throw (e);
                }

            }

            
else
            
{
                
//Major error - not running under ClickOnce, but missing assembly. Don't know how to recover.
                throw (new Exception("Cannot load assemblies dynamically - application is not deployed using ClickOnce."));
            }



            
return (newAssembly);
        }

        
//</SNIPPET2>

        
//<SNIPPET3>
        private void getAssemblyButton_Click(object sender, EventArgs e)
        
{
            DynamicClass dc 
= new DynamicClass();
            MessageBox.Show(
"Message: " + dc.Message);
        }

        
//</SNIPPET3>



    }

}

posted on 2006-11-21 19:55  孤岛  阅读(683)  评论(3)    收藏  举报

导航