Repost A clickonce article!

Source: Internet
Author: User
Use the clickonce Technology in vs2005 to download components as needed

By default, when you run a clickonce application for the first time, all the Assembly contained in the application is downloaded. However, in some special scenarios, we may not want it to do this. Instead, we want to download it when testing or using a certain component.

For example, we may have the following application scenarios:

1. The client program I developed is charged. However, free users can also use some features. In terms of technical implementation, I encapsulated the functions used by paying users into. DLL components, I hope free users cannot get. DLL. Only paid users can obtain. DLL, and load. DLL charges.

2. My entire application is very large. I don't want to download all the applications every time I update them. Users only need to download the functional components they use.

 

Next we will implement a simple on-demand download example. We can fully implement the Application Scenario 1 mentioned above on the basis of this example.

Demo steps:

1. Create a class library project

Define the name of the output application of the Class Library: ondemandassembly. That is, the file name generated after this class library compilation is ondemandassembly. dll.

This class library has the following code. We use this code to simulate some of the functions that need to be loaded on demand or paid in the above mentioned scenario. We will call this function in the main program:

Using system;

Namespace ondemandassembly
{
Public class dynamicclass
{
Public String message
{
Get
{
Return "guo hongjun tests the clickonce On-Demand Loading Function. ";
}
}

}
}

 

2. Create a window Application

We will load the above class library in this window application.

 

To facilitate the demonstration of this program, make sure that the window program has the following features:

1. There is a Textbox Control, which displays the application directory in the onload event of the window, so that we can monitor whether the above ondemandassembly. dll component is loaded.

The Code is as follows:

Private void form1_load (Object sender, eventargs E)
{
This. textbox1.text = application. startuppath;
}

2. Click a button to display the message in the ondemandassembly. dll component. The Code is as follows:

Private void button#click (Object sender, eventargs E)
{
Dynamicclass o = new dynamicclass ();
MessageBox. Show (O. Message );
}

 

The window program has special settings for clickonce:

We will configure some special parameters for clickonce release to ensure that the required components can be downloaded as needed.

This article does not describe in detail the steps and parameters of clickonce settings. If you are not familiar with clickonce, we recommend that you read some of the clickonce getting started articles to read this article.

For example: the http://blog.oracle.com.cn/155011/viewspace_3603.html here provides clickonce article (here is a word compressed file download, the article in the compressed Word file)

 

Select our window program, select Properties from the right-click menu, and select the publish tab on the properties page.

Click the application file button to set the file to be released. For example:

 

The window opened by the preceding button is as follows:

In the window that we opened, set the ondemandassembly. dll file publishing status to include,

In the download group, create a new download group for the ondemandassembly. dll file. Here we name this new download group demandassembly01.

Note:

By default, when clickonce is used for the first installation, or when the program is upgraded, the system only downloads the files in the required group. If the file systems in other groups are not downloaded, We need to encode the files.

 

 

Other clickonce settings are the same as those we usually use. This section is ignored.

 

We will release this program at this time. After downloading and installing it, we will find that the ondemandassembly. dll file is not in the installation directory. click the button of this program and an exception will be reported, and the required application assembly cannot be found.

Ondemandassembly. dll component.

 

The following code is implemented. If the application cannot find the ondemandassembly. dll component, download the function of this component from the Internet.

1. added the processing logic of the assemblyresolve event to the window constructor.

Public form1 ()
{
Initializecomponent ();

Appdomain. currentdomain. assemblyresolve + = new resolveeventhandler (currentdomain_assemblyresolve );
}

Assembly currentdomain_assemblyresolve (Object sender, resolveeventargs ARGs)
{

}

Note: The appdomain. assemblyresolve event occurs when parsing the Assembly fails.

 

2. encode the processing logic of the assemblyresolve event.

Using system. reflection;
Using system. Deployment. Application;

// If multiple files need to be downloaded as needed, each file is mapped to the download group, which is recorded by this object.

Dictionary <string, string> dllmapping = new dictionary <string, string> ();

Public form1 ()
{
Initializecomponent ();
Dllmapping ["ondemandassembly"] = "demandassembly01 ";
Appdomain. currentdomain. assemblyresolve + = new resolveeventhandler (currentdomain_assemblyresolve );
}

Assembly currentdomain_assemblyresolve (Object sender, resolveeventargs ARGs)
{
Assembly newassembly = NULL;

// Clickonce deployment Method
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];

// Download the required file 
Try
{
Deploy. downloadfilegroup (downloadgroupname );
}
Catch (deploymentexception de)
{
MessageBox. Show ("downloading file group failed. group name:" + downloadgroupname + "; DLL name:" + args. Name );
Throw (de );
}

// Load the component to the application assembly
// 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 );
}

 

Some problems:

Q: In the above Code, if ondemandassembly. dll has the latest version and is released, but the client is still an old version, we have not processed this logic?

A: The problem is that you do not understand the principle of clickonce, ondemandassembly. if the DLL has the latest version, you must re-release the clickonce code of another version. At this time, the released version is different. Under the clientClickonce programs of different versions exist in different directories.(Of course, the same is true for servers ).

After checking that your new version program is released, the system creates a directory for the new version number. If ondemandassembly is not used in this directory. the DLL function does not have ondemandassembly. DLL components, and the above issues can be ignored.

 

References:

Msdn: Downloading assemblies on demand with the clickonce deployment API

Use the clickonce Technology in vs2005 to download components as needed

By default, when you run a clickonce application for the first time, all the Assembly contained in the application is downloaded. However, in some special scenarios, we may not want it to do this. Instead, we want to download it when testing or using a certain component.

For example, we may have the following application scenarios:

1. The client program I developed is charged. However, free users can also use some features. In terms of technical implementation, I encapsulated the functions used by paying users into. DLL components, I hope free users cannot get. DLL. Only paid users can obtain. DLL, and load. DLL charges.

2. My entire application is very large. I don't want to download all the applications every time I update them. Users only need to download the functional components they use.

 

Next we will implement a simple on-demand download example. We can fully implement the Application Scenario 1 mentioned above on the basis of this example.

Demo steps:

1. Create a class library project

Define the name of the output application of the Class Library: ondemandassembly. That is, the file name generated after this class library compilation is ondemandassembly. dll.

This class library has the following code. We use this code to simulate some of the functions that need to be loaded on demand or paid in the above mentioned scenario. We will call this function in the main program:

Using system;

Namespace ondemandassembly
{
Public class dynamicclass
{
Public String message
{
Get
{
Return "guo hongjun tests the clickonce On-Demand Loading Function. ";
}
}

}
}

 

2. Create a window Application

We will load the above class library in this window application.

 

To facilitate the demonstration of this program, make sure that the window program has the following features:

1. There is a Textbox Control, which displays the application directory in the onload event of the window, so that we can monitor whether the above ondemandassembly. dll component is loaded.

The Code is as follows:

Private void form1_load (Object sender, eventargs E)
{
This. textbox1.text = application. startuppath;
}

2. Click a button to display the message in the ondemandassembly. dll component. The Code is as follows:

Private void button#click (Object sender, eventargs E)
{
Dynamicclass o = new dynamicclass ();
MessageBox. Show (O. Message );
}

 

The window program has special settings for clickonce:

We will configure some special parameters for clickonce release to ensure that the required components can be downloaded as needed.

This article does not describe in detail the steps and parameters of clickonce settings. If you are not familiar with clickonce, we recommend that you read some of the clickonce getting started articles to read this article.

For example: the http://blog.oracle.com.cn/155011/viewspace_3603.html here provides clickonce article (here is a word compressed file download, the article in the compressed Word file)

 

Select our window program, select Properties from the right-click menu, and select the publish tab on the properties page.

Click the application file button to set the file to be released. For example:

 

The window opened by the preceding button is as follows:

In the window that we opened, set the ondemandassembly. dll file publishing status to include,

In the download group, create a new download group for the ondemandassembly. dll file. Here we name this new download group demandassembly01.

Note:

By default, when clickonce is used for the first installation, or when the program is upgraded, the system only downloads the files in the required group. If the file systems in other groups are not downloaded, We need to encode the files.

 

 

Other clickonce settings are the same as those we usually use. This section is ignored.

 

We will release this program at this time. After downloading and installing it, we will find that the ondemandassembly. dll file is not in the installation directory. click the button of this program and an exception will be reported, and the required application assembly cannot be found.

Ondemandassembly. dll component.

 

The following code is implemented. If the application cannot find the ondemandassembly. dll component, download the function of this component from the Internet.

1. added the processing logic of the assemblyresolve event to the window constructor.

Public form1 ()
{
Initializecomponent ();

Appdomain. currentdomain. assemblyresolve + = new resolveeventhandler (currentdomain_assemblyresolve );
}

Assembly currentdomain_assemblyresolve (Object sender, resolveeventargs ARGs)
{

}

Note: The appdomain. assemblyresolve event occurs when parsing the Assembly fails.

 

2. encode the processing logic of the assemblyresolve event.

Using system. reflection;
Using system. Deployment. Application;

// If multiple files need to be downloaded as needed, each file is mapped to the download group, which is recorded by this object.

Dictionary <string, string> dllmapping = new dictionary <string, string> ();

Public form1 ()
{
Initializecomponent ();
Dllmapping ["ondemandassembly"] = "demandassembly01 ";
Appdomain. currentdomain. assemblyresolve + = new resolveeventhandler (currentdomain_assemblyresolve );
}

Assembly currentdomain_assemblyresolve (Object sender, resolveeventargs ARGs)
{
Assembly newassembly = NULL;

// Clickonce deployment Method
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];

// Download the required file 
Try
{
Deploy. downloadfilegroup (downloadgroupname );
}
Catch (deploymentexception de)
{
MessageBox. Show ("downloading file group failed. group name:" + downloadgroupname + "; DLL name:" + args. Name );
Throw (de );
}

// Load the component to the application assembly
// 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 );
}

 

Some problems:

Q: In the above Code, if ondemandassembly. dll has the latest version and is released, but the client is still an old version, we have not processed this logic?

A: The problem is that you do not understand the principle of clickonce, ondemandassembly. if the DLL has the latest version, you must re-release the clickonce code of another version. At this time, the released version is different. Under the clientClickonce programs of different versions exist in different directories.(Of course, the same is true for servers ).

After checking that your new version program is released, the system creates a directory for the new version number. If ondemandassembly is not used in this directory. the DLL function does not have ondemandassembly. DLL components, and the above issues can be ignored.

 

References:

Msdn: Downloading assemblies on demand with the clickonce deployment API

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.