How to deploy a strong naming component in. net

Source: Internet
Author: User

Private deployment: What is private deployment? This is relative to public deployment. We know that % SystemRoot %/assembly (% SystemRoot % generally refers to the system Windows Directory) on the machine where dotnetframework is installed) the file in the directory is.. Net to run the required components. However, the components required in our own executable programs are the DLL components (also called assembly files) stored in the root directory of the program ). Deployment of these files is called Private deployment. For more information about how to use this tool, see msdn. I will not describe it in detail here.) components to be called for running programs are deployed in the program root directory. Later I will talk about how to organize DLL components.

Start our official itinerary:

The Assembly file, DLL, and component mentioned in the following content have the same meaning.

I: component creation

II: executable program

III: You can find mine !~! (DLL private deployment to any folder under the root directory)

IV: no one can replace me! (DLL/EXE: use the public key/private key to sign the Assembly to prevent tampering)

I: component creation

Here we first compile a DLL component (the code here does not have any practical functions, only for demonstration)

Run csc.exe/out: utildata. dll/T: Library/R: dataprovider. CS

Obtain the utildata. dll file.

Using system;
Using system. Collections. Generic;
Using system. text;

Namespace utildata
...{
Public class dataprovider
...{
Public dataprovider ()...{}
Public dataprovider (int32 _ id, string _ name, string _ sex)
...{
Id = _ id;
Name = _ name;
Sex = _ sex;
}

Private int32 ID;
Private string name;
Private string sex;

Public int32 ID
...{
Get... {return this. ID ;}
Set... {If (value> = 0)... {This. ID = value ;}}
}
Public string name
...{
Get... {return this. Name ;}
Set... {If (value! = NULL)... {This. Name = value ;}}
}

Public String sex
...{
Get... {return this. Sex ;}
Set... {If (value! = NULL)... {This. Sex = value ;}}
}

Public String getdataprovider (int32 ID)
...{
Return "you ID is:" + id. tostring ();
}

Public override string tostring ()
...{
// Return base. tostring ();
Stringbuilder sb = new stringbuilder ();
SB. append ("My ID:" + this. ID );
SB. append ("My name:" + this. Name );
SB. append ("My sex:" + this. Sex );
Return sb. tostring ();
}

}
}

 

II: executable program

The winapplication form Program Creation Interface is as follows: add reference and find the generated DLL component

Test button background code:

// My code
Utildata. dataprovider dp = NULL;
Private void btncall_click (Object sender, eventargs E)
...{
Dp = new utildata. dataprovider (001, "fang", "man ");
MessageBox. Show (DP. tostring ());
}

To generate a project, we can find that the structure of the DEBUG directory in the project is as follows:

III: You can find mine !~! (DLL private deployment to any folder under the root directory)

In practical applications, we often find that DLL components are distributed in different folders under the application root directory. What should we do?

Does CLR find the components required for running? The configuration file name is privatepublish.exe.config. clrruns the program according to the information configured in. config in privatepublish.exe.

Privatepublish.exe. config file. The entries listed here are relatively simple. For details, see msdn. Lib is the directory for storing DLL components.

If multiple directories are separated by semicolons (for example, the DLL component directory requires two folders), the CLR will automatically find the required assembly file.

<Configuration>
<Runtime>
<Assemblybinding xmlns = "urn: Schemas-Microsoft-com: ASM. V1">
<Probing privatepath = "lib"/>
</Assemblybinding>
</Runtime>
</Configuration>

Directory structure

IV: no one can replace me! (DLL/EXE: use the public key/private key to sign the Assembly to prevent tampering)

What is strong naming? A strong naming component contains four features: component name, language feature, version, and public key tag. These four features can uniquely identify an assembly.

Here, we will introduce a sn.exe utility, which is located in the. NET installation directory. (For more information, see msdn)

The purpose is to get the public key mark, so execute the command line

Sn.exe-K mykey. Keys creates a public/private key pair file, which is required by the Assembly.

Sn.exe-P mykey. Keys mykey. publickey creates a public key file based on the public key/private key file.

The sn.exe-TP mykey. pubilckey console displays the public key and Public Key mark on my machine to run this step screenshot

Execute the following command to produce the utildata. dll file with strong names:

Csc.exe/out: utildata. dll/T: Library/Keyfile: My. Keys/R: dataprovider. CS

This is the only difference from the previously generated utildata. dll is that the Keyfile: My. Keys switch is specified. Of course, you can also add the same switch to the EXE file and perform a strong name on the EXE file.

You can specify the winexe file signature for the keys file in the project property-signature tag.

Now, the private deployment and strong naming of the Assembly have been completed.

Do a small experiment to verify what the tampering with the strong-naming assembly will cause

Restorator open utildata. dll,

Click Save ~! We have tampered with utildata. dll.

We have a problem running privatepublish.exe.

Why? Because the Assembly has been tampered with, the modified utildata. dll data cannot be recognized by the Assembly calling it, so an error occurred while loading the assembly ~!

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.