[ZT] What is the role of publickeytoken and publickeytoken?

Source: Internet
Author: User
Tags dotnet
What is the role of publickeytoken and publickeytoken?

Concept:
(1) Composition of versions in DOTNET
The version in DOTNET consists of four physical numbers. (1)

Figure (1)
InProgramIn the set, we can set it by adding the assemblyversion feature,
For example, [Assembly: assemblyversion ("2.0.2.11")]
(2) GAC:
Within the computer rangeCodeCache, which stores specially installed assemblies that are shared by many applications on the computer. The application deployed in the Global Assembly Cache must have a strong name. If a set of programs is registered in GAC and referenced by other sets of programs, copies will not be copied to the referenced program directory. (This article only discusses the Assembly registered to GAC)

--------------------------------------------------------------------------------

Body:
(1) Now we start to write the first component.
Its version is 1.0.0.0. There is a class that contains a simple method. This method returns a string
The Code is as follows. I have made annotations.
// Generate a strong name in the file
Sn-k c: \ version. SNK
// Write a Class Object

 

V1.cs
Using System;
Using System. reflection;
[Assembly: assemblykeyfile ( @" C: version. SNK " )] // Because you have registered with GAC, use a strong name for signature.
[Assembly: assemblyversion ( " 1.0.0.0 " )] // Set version number
Namespace V1
{
Public Class V1
{
Public V1 ()
{
}
Public String Getversion ()
{
Return " Hello, I'm version 1. " ;
}
}
}

Compile to component v1.dll
Copy this v1.dll to the C: \ V1 folder.
Register v1.dll with GAC
Gacutil-I c: \ V1 \ v1.dll
The system displays that the registration is successful.
(2) then we compile a console program to test this component.
Testversion. CS

 

///
/// Add a reference to v1.dll (C: v1v1. dll). Because it is in GAC, v1.dll does not exist in the local directory.
///
Using System;
Namespace Testversion
{

Class Testversion
{
Static Void Main ( String [] ARGs)
{
V1.v1 = New V1.v1 ();
Console. writeline (v1.getversion ());
Console. Read ();
}
}
}

Compile and run (testversion.exe)
The screen output result is:
"Hello, I'm version 1"
(3) Well, everything is wonderful. After several years, we have to upgrade the program.
Now, compile the v1.dll upgrade version 2.0.0.0.
V1.cs

Using System;
Using System. reflection;
[Assembly: assemblykeyfile ( @" C: version. SNK " )] /// Use the preceding strong name
[Assembly: assemblyversion ( " 2.0.0.0 " )] /// Note that the main version number has been changed.
Namespace V1
{
Public Class V1
{
Public V1 ()
{

}< br> Public string getversion ()
{< br> return " Hi, I'm version 2 " ; /// modified the output result so that we can know which one is running
}< BR >}< br>

Compile to component v1.dll
Copy this v1.dll to the C: \ V2 folder.
Register v1.dll with GAC
Gacutil-I c: \ V2 \ v1.dll
(4). Now let's take a look at the output of testversion.exe.
Yes, it remains unchanged from the original
"Hello, I'm version 1"
Why not "Hello, I am version 2 "?
Let's use ildasm.exeto open the testversion.exe program.

Yes, it currently uses version 1.0.0.0
(5) Let's start upgrading the version.
Replace the version used by testversion.exe with version 2.0.0.0 from 1.0.0.0.
In fact, it is very simple to give us testversion.exe
Add the configuration file testversion.exe. config

<? XML version = " 1.0 " ?>
< Configuration >
< Runtime >
< Assemblybinding xmlns = " URN: Schemas-Microsoft-com: ASM. V1 " >
< Dependentassembly >
< Assemblyidentity name = " V1 " Publickeytoken = " 758fe4e9db9d8133 " />
< Bindingredirect oldversion = " 1.0.0.0 " Newversion = " 2.0.0.0 " />
</ Dependentassembly >
</ Assemblybinding >
</ Runtime >
</ Configuration >

Simple Description:
Assemblyidentity identifies a set of programs.
Where name is the Assembly name publickeytoken is the Public Key mark; how to obtain this information? Very simple. Open % SystemDir % \ assembly (my c: \ winnt \ assembly)
You can see this information (for example, your machine may be different from me)

We can see that there are two V1 (correct, we did register two in, and the version is correct). We started to use 1.0.0.0. Write down the public key token and write it to publickeytoken.
How to jump to the main configuration version of The bindingredirect Node
Oldversion = "1.0.0.0" newversion = "2.0.0.0" is clear. If testversion.exe uses an earlier version of v1.dll and the assembly version is 1.0.0.0, a new version of 2.0.0.0 is used.
It should be noted that oldversion can be defined by range, such as oldversion = "1.0-1.9". However, if range notation is not used, you must write the full version number.
Newversion cannot use range notation. Please write the full version number.
Now, run testversion.exe again.
The output is: "Hello, I am version 2". Applause !!!
That's how easy it is!
(6) If we use ildasm.exeto open the testversion.exe program, the content will not change.
Indeed, the configuration file has been started. The entire process has not changed the code of the calling program testversion.exe!
(7) What if the configuration is incorrect? :
Possible errors. For example, if newversion is written incorrectly and there is no such upgrade version, an exception system. Io. filenotfoundexception will be thrown. Please handle this.

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.