Summary:
With the development of society, people have new understandings to science and technology applications, and this makes our technical staff have higher requirements for development platforms. Everyone knows, or heard, .NET has a very powerful version control, allows us to easily carry out the version of the assembly, realize the flexible configuration of the program and simple upgrade, say goodbye to DLL hell, this article will use a specific An example shows how to use this feature.
concept:
(1) The version of the DOTNET
The version in the DOTNET consists of 4 physical numbers, as shown (1)
Figure (1)
In the assembly, we can set it by adding the AssemblyVersion feature.
Such as [Assembly: AssemblyVersion ("2.0.2.11")]
(2) GAC:
The code cache within the computer range, which stores specially installed assemblies, which are shared by many applications on your computer. The application deployed in the global assembly cache must have a strong name. If an assembly is registered in the GAC, it will not copy the copy of the program directory that will not be copied by other programs. (This article only discusses the assembly registered to the GAC)
text:
(1) Let's start writing the first component now
Its version number is 1.0.0.0 has a class, containing a simple method, which returns a string
I have done it as follows.
// First, you will become a strong name to the file.
SN -K C: /Version.snk
// write file
V1.cs
Using system;
Using system.reflection;
[assembly: assemblyMblyKeyFile (@ "c: /version.snk")] // Strong name signature because it is registered to the GAC
[assmbly: askEMBLYVERSION ("1.0.0.0")] // Settings version number
Namespace V1
{
Public Class V1
{
Public V1 ()
{
}
Public string getversion ()
{
Return "Hello, I am version 1";
}
}
}
Compiled into components v1.dll
Copy this v1.dll to folder C: / V1
Register v1.dll to GAC
Gacutil -i c: /v1/v1.dll
The system shows the successful registration
(2) Then we write a console program to test this component
TestVersion.cs
///
/ / / Add a reference to V1.dll (C: /V1/V1.dll) because it is in the GAC, so V1.dll will not exist in the local directory.
///
Using system;
Namespace testversion
{
Class testversion
{
Static void main (string [] args)
{
V1.v1 v1 = new v1.v1 ();
Console.writeLine (v1.getversion ());
Console.read ();
}
}
}
TeestVersion.exe
The result of the screen output is:
"Hello, I am version 1"
(3) Ok, everything is very beautiful, after a few years, we have to upgrade the program.
Now to write V1.dll upgrade version, version number is 2.0.0.0
V1.cs
Using system;
Using system.reflection;
[assembly: assemblykeyKeyFile (@ "c: /version.snk")] /// use the strong name above
[Assembly: AssemblyVersion ("2.0.0.0")] /// Note that the main version number Namespace V1 has been changed here.
{
Public Class V1
{
Public V1 ()
{
}
Public string getversion ()
{
Return "Hello, I am Version 2"; /// Modify the output of the output so that we know that it is running
}
}
}
Compiled into components v1.dll
Copy this v1.dll to folder C: / V2
Register v1.dll to GAC
Gacutil -i c: /v2/v1.dll
(4). Take a look at what is running Testversion.exe again.
Yes, and the original no change is still
"Hello, I am version 1"
Why isn't "Hello, I am version 2"?
We open the assembly TestVersion.exe with ILDASM.EXE.
Yes, it is currently using version 1.0.0.0
(5) We started to upgrade
Replace the version used by TestVersion.exe by 1.0.0.0 with version 2.0.0.0
In fact, it is very simple, give us TestVersion.exe
Add a configuration file TestVersion.exe.config
XML Version = "1.0"?>
dependentassembly>
askBLYBINDING>
runtime>
configure>
Brief description:
AssemblyIdentity is mainly identified by the program collection
Where Name is the name of the assembly publickeyToken is a tab that is a lasp; how to get this information? Very simple, open% systemdir% / askEMBLY (mine C: / Winnt / askEMBLY)
You can see this information (as shown below, you may not be different from me)
It can be seen that there are two V1 (correct, we have indeed registered two into, version), we started using 1.0.0.0, write down the public keytoken Write to PublickeyToken
How to jump in the main configuration version of the BindingRedirect node
OldVersion = "1.0.0.0" Newversion = "2.0.0.0" is very clear, that is, if the V1.DLL used by TestVersion.exe is the old version, the assembly version number is 1.0.0.0, then use the new, version number Set of 2.0.0.0
It should be noted that OldVersion can be defined using the range, such as OldVersion = "1.0-1.9", but if there is no use of scope representations, must write the full version number
NewVersion cannot use the scope representation, please write the full version number
Ok, now run TestVersion.exe again.
The output is: "Hello, I am version 2", applause !!! Is it simple!
(6) If we now open the assembly TestVersion.exe with ILDASM.exe, see the content will not change
Indeed, it is the role of the configuration file, and the whole process does not change the code for the calling program TestVersion.exe!
(7) What will the configurement error?:
It is possible to mistaken, such as newversion, do not have this upgrade version at all, will throw an exception system.io.filenotFoundException, please pay attention to handling
Note:
In fact, we can use Microsoft .NET Framework configuration in the control panel to configure version management, easier, space relationship, need to contact me to use it (bcatcat@hotmail.com)
to sum up:
It can be seen that .NET is powerful and simple to provide us, and you can achieve your goals through a simple configuration. I hope this article can play the role of tiles