"Proficient in C #" 14 chapters-. NET assemblies Get started

Source: Internet
Author: User

In the book, the beginning of this chapter is the custom namespace and the use of namespaces, in my current limited experience, the assembly is a class library after compiling, the resulting one in the reference namespace, and then use the file has already defined fields, properties and methods of the file, Use. dll as the suffix name. This is why the beginning of the book is to start with the name space. So what is a namespace, the most intuitive way is to define a class, in the inside of the class you will find that there is always a namespace keyword, followed by a string as the name, the name is the name of the namespace, that is, the name of the assembly we want to say next, we first talk about the namespace , and then say the assembly. Namespaces use a using to reference different assemblies, and if you use a namespace, because the name of the namespace is too lengthy, and each time it is too cumbersome to write, you can use the using to define a name for the method, and then use that namespace to directly use the plastic name. Without having to write the full name. And inside a namespace we can define a namespace again, to form nested namespaces, and as such, some people say that the code is more secure, but it seems to me that there is no egg. So, what is the assembly? Book listing This describes: An assembly is a versioned, self-describing binary file that is hosted by the common language runtime. Why is it that the common language runtime is the host? You know, the CLR can compile code into an IL intermediate language so that our code can be reused across languages and can be supported in any way. NET platform, which facilitates the reuse of code. Versioning is the ability to define different versions of an assembly, what does this do? This allows the two different versions of the assembly to coexist in a single file without creating a conflict. At this point I think: why do I need two different versions of the assembly, I just have to change the content of the first version is not OK? This may be more convenient, the assembly is not completely independent, there may be some dependencies between them, this is the case, if the old version of the assembly is not preserved, which may cause some dependencies and its programs do not work properly, so it is best to keep different versions of the Assembly, This will also explain how the program formulates which assembly should be used, before we need to explain the private assembly and the shared assembly.

In my opinion, the so-called private assembly is the program when referencing it will automatically generate a copy of the assembly in the program directory or subdirectory, that is, the program uses a copy of the Assembly, so that in a program if you use the assembly multiple copies, which is a many-to-many relationship, Shared assembly purposes and private assemblies are actually the same, designed to be used by multiple projects in one assembly, except because it is deployed in a public folder, so it is, or is, a copy of it that is shared by all projects referencing it (because the assembly was deployed in the past, actually creating a copy in the public folder), of course , the reference path must be the path to the assembly in that public folder. So is this folder free or can you create it yourself? Of course not, the name of this folder is called the Global Assembly Cache (GAC), and its exact location is installed on the machine. NET platform version, before 4.0, the GAC is the assembly subdirectory of the Windows directory in C disk, and after 4.0, the GAC becomes C:\windows\Microsoft.NET\assembly\GAC_MSIL, In particular, it is important to note that the. dll files can only be deployed as shared assemblies in the GAC, and the executable assembly (. exe) cannot be installed. So how do I install the. dll file into the GAC? This is going to be used after installing the. NET platform, the Developer Command tool in the own Tools folder, open Command tool, enter gacutil/? Query Gacuti.exe usage, mainly use to-I install strong-named assemblies into the GAC, use-u to unload an assembly from the GAC, use-l to display information about an assembly in Gav, and a friendly name with an assembly (removing the name of the. dll suffix) to query information about the assembly. With the strong name mentioned above, be aware that you need to give the assembly a strong name before deploying the assembly, so what is the strong name? In simple terms, a strong name is used to identify a given. The publisher of the net binary file. The role in. NET is like the global unique notation, which consists of a series of related data, namely the friendly name of the assembly, the version number, the public key, the optional culture identity for localization, the embedded digital signature (which consists of a hash value based on the content of the assembly and a private key). So what does a strong name do, and why does a shared assembly enforce that only strong-named assemblies are required? Because a strong name consists of a hash value, which is generated based on the content of the assembly, it can be said to be unique, that is, whenever a strong name is set, even if someone maliciously creates an assembly of the same name to replace the original assembly, it passes through when it is used. NET detector is detected and then stops running. To generate a public private key, you need to use the Sn.exeA file that contains two different keys, typically with *.snk as the file name extension. So how do you generate a strong name? About 2003 ago, there was only one way to generate strong names, using the command line, or using the Developer Command Prompt tool under Tools, to generate strong names using SN, as for the specific method I don't have to do more, because we now have a better way, Want to know exactly what to use SN's self-turn book to p433 page or search on the internet itself. So what's the second way? That is, using the VS feature to add a strong name to the assembly, first, right click on the class library, select the property, the left has a column of options, select the signature, tick to sign the assembly, and then select New in the Select Strong name key file, it is recommended to use the class library friendly name as the key file name, Remember to use. SNK as the end, as for the password is generally not used, this is to see the individual bar. This makes it possible to create a strong-named assembly that can then be deployed into the GAC, using shared assemblies in the same way as normal assemblies, but when referencing, open the GAC folder in the browser, select the assembly that you want to reference, and then use it as usual.

Now let's talk about the operation to make the program correctly point to the version of the Assembly that I need for the device, for example:

<dependentAssembly>
<assemblyidentity name= "Microsoft.owin" publickeytoken= "31bf3856ad364e35" culture= "neutral"/>
<bindingredirect oldversion= "0.0.0.0-2.1.0.0" newversion= "2.1.0.0"/>
</dependentAssembly>

This example is what I keyed directly from the Web. config, creating the <assemblyIdentity> child element inside the <dependentAssembly> element, It is used to specify the friendly name and an optional region (culture) attribute of the assembly that is listed in the customer manifest, and if you want to use the default locale, simply set the attribute to null or ignore. It is also necessary for <bindingRedirect> to define the version (Oldversion) that the program manifest is currently pointing to, and the version that is substituted in the GAC (NewVersion). However, if the program is a desktop reference program, then you need to create a program in the app. config, remember that the name must be the application, after the program compiled in the bin\debug will have a *. Exe.config as the suffix of the file, then the above content will be saved in just. However, this will be a lot of trouble in modifying the version, because I need to change the config file to publish the past (client program), so the workload is very large, this has a publisher policy assembly, the role is to run the assembly of the publisher in the deployment of the Assembly, the same time a *. The binary version of the config file is also installed in the GAC, so there is no need to include the config file with the client application. We use the Al.exe tool to create the publisher policy assembly at the command line, which also requires the following parameters: 1. The location of the config file or XML file that contains the redirect directive. 2. The name of the generated publisher policy assembly. 3. Location of the. snk file that is used to sign the publisher policy assembly. 4. The version number of the publisher policy assembly that was created. It is such a string: The major version number. The assembly name of the. dll, but I do not have to press the book or the information found on the Internet can not successfully install the publisher policy assembly, if there is a successful installation, I hope you can leave a message, teach me, thank you. In this chapter of the book There is also a <codeBase> element that instructs the CLR to probe a dependent assembly that is located on a local directory other than the network endpoint or the client application directory. When it points to a remote computer, the associated strong-named assembly is downloaded from the GAC's download cache. However, if the directory is relative to the client application, it is possible to probe for assemblies that do not have strong names.

"Proficient in C #" 14 chapters-. NET assemblies Get started

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.