. NET to eliminate the dependency in the DLL

Source: Internet
Author: User
Tags add definition key reference

We often encounter problems with the. Net Class Library project, and I want to generate a DLL for other programs to use, but in my project I need to refer to some other reference, like ActiveX controls or other DLLs, These controls or DLLs are not strong name and cannot be written to the GAC (Global Assembly Cache), so you must take these referenced controls or DLLs to complete registration when you register to use your DLL. However, this is not what we need, we just want to provide a DLL for people to use the line, why do you want to bring a bunch of controls or DLLs? Others are unwilling to do so. So, what is the solution to this problem?

As you all know, in. NET, if you try to write a DLL into the GAC, other controls or DLLs you use in your project need to have strong name in addition to your DLL project using strong name, otherwise, when you register your DLL, will prompt for missing dependency. This is annoying, but it should be good for versioning. Since this is the case, we only add strong name to the referenced control or DLL and write it to the GAC, so that when you register your DLL, you go to the GAC to find the control or DLL it uses. So, how do we do that? Roughly two steps:


1. Add strong name to the control or DLL you want to reference


This is something you have to do, or you won't be able to write them in the GAC. NET SDK provides a tool to generate a strong name for an assembly, not just the generation, but also the verification of the management and signature, just according to the different option. Please refer to MSDN for more information. The following is the creation of a key pair through SN, stored in keyfile.snk:


Sn-k keyfile.snk

This generates a key pair. If your project does not refer to other controls or DLLs without strong names, just want to write your own DLL to the GAC, then in the AssemblyInfo.cs of your project, add AssemblyKeyFile, which is the keyfile.snk file you just generated.


[Assembly:assemblykeyfile (".. \\.. \\keyfile.snk ")]


The path here refers to the relative path of the compiled project output, so if this is written in this way (and you can also modify it), you should copy the keyfile.snk you just generated to the project's directory to ensure that the SNK file is found at compile time. This allows the compiled DLL to be written to the GAC:


Gacutil-i Yourdll.dll


If your project references other controls or DLLs that don't have a strong name, you'll need to do the next step.


2. Add a strong name to a control or DLL without a strong name


There are two tools to use here, too. Net-brought, TlbImp and AxImp. TLBIMP is an equivalent definition of converting a type definition from a COM type library to a CLR assembly, aximp converting a type definition from a COM type library to a Windows Forms control, For more information, refer to MSDN.


Suppose you want to add a strong name to Tom.dll and Msflxgrd.ocx:
  

TlbImp tom.dll keyfile:tom.dll.snk Out:tom.dll

AxImp Msflxgrd.ocx keyfile:MSFlxGrd.ocx.snk AxMSFlxGrd.ocx.snk


Here, Tom.dll.snk, MSFLlxGrd.ocx.snk, and AxMsFlxGrd.ocx.snk are created according to the first step, and each DLL corresponds to a SNK. Tom.dll is the name of the converted DLL and has no other meaning. It is also necessary to note that Msflxgrd.ocx produces two DLLs, respectively MSFlexGridLib.dll and AxMSFlexGridLib.dll. The individual understands that a axmsflexgridlib is generated when the form is initialized, and who has a better explanation?


This adds a strong name to the Tom.dll and Msflxgrd.ocx controls, converts them to the corresponding DLLs, and writes them to the GAC:


Gacutil-i Tom.dll
Gacutil-i MSFlexGridLib.dll
Gacutil-i AxMSFlexGridLib.dll


The rest of the work is to refer them back to your project, recompile, so that the generated DLL does not need any dependency when registering, because they are already in the GAC, open c:\winnt\assembly to see.


If you want to remove them from the GAC, then:


Gacutil-u, Tom.
Gacutil-u MSFlexGrid
Gacutil-u Axmsflexgrid

Tom, MSFlexGrid and Axmsflexgrid are the names in the GAC and there is no need to add any paths.



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.