Google
WWW Yariv Hammer's Code Site

Tuesday, February 14, 2006

Multi-File Assemblies

This article is one of a series of articles exploring .NET Assemblies.
Exploring .NET Assemblies
Multi-File Assemblies
Strong Named Assemblies
Placing Assemblies in the Global Assembly Cache (GAC)
Administrative Configuration of Applications
The articles are intended for programmers who are using .NET for a while, and wish to know more about .NET assemblies.

Introduction
Assemblies are normally built into a single file (exe or dll). There is a feature to compile parts of the assembly into net-modules, and then build an assembly that consists of all the net-modules.

Creating Multi-File Assemblies
The feature is not supported by the IDE, so we must compile in command line (using csc.exe r vbc.exe).
Lets assume we have the following code:
Class A (A.cs):
---------------------------------------------------
using System;
namespace ClassLibrary1
{
public class A
{
public void foo()
{
Console.WriteLine("A.foo()");
}
}
}
---------------------------------------------------
Class B (B.cs)
------------------------------------------------
using System;
namespace ClassLibrary1
{
public class B
{
public void foo()
{
A a = new A();
a.foo();
Console.WriteLine("A.foo()");
}
}
}
----------------------------------------------
As you can see B uses class A.

Now start Command-Line (Start->Programs->.NET 2003->Tools->Command Line Prompt).
Change directory to the folder of the files ( cd MyFolder ).
In that folder we have a.cs and b.cs .
Type the following lines (one by one):
----------------------------------------------
csc /t:module a.cs
csc /t:module b.cs /addmodule:a.netmodule
al /out:MyAssembly.dll /t:library a.netmodule b.netmodule
---------------------------------------------

Lets go over the code line by line:
csc /t:module a.cs
This line will compile a.cs (and any other file you might specify) into one netmodule file.
After the line is executed you can see in the folder a file called a.netmodule.
If you view the a.netmodule using ildasm, you can see that the structure of a.netmodule is similar to a regular assembly.

csc /t:module b.cs /addmodule:a.netmodule
As in class A, cladd B which resides in b.cs is compiled into a netmodule. the /addmodule directive is used to add a "reference" to a.netmodule. The reason we need this is because B used class A, so much like dlls we need a reference to all the netmodules containing classes that are used in the current netmodule we are trying to compile.
Again, a file called b.netmodule now exists.

al /out:MyAssembly.dll /t:library a.netmodule b.netmodule
al is the Assembly Linker. We use it in order to generate our Dll Assembly (using the /out directive). All the netmodules are specified.
After running this code we see a file MyAssembly.dll in the folder. Lets run ildasm on this assembly. There is only a Manifest to the assembly, and no types. Inside the manifest you will see the references to the netmodules. You can see that the dll do not contain the types, but it reference to them. This is important, because now we can replace the netmodules without the need to compile the assembly.

Note: Because the IDE do not support Multi-file assemblies, you will need to create batch files which compile the code for you. This can be quite tedious.

Advantages of Multi File Assemblies
Although the obvious disadvantage stated above, there are some benefits to this kind of deployment:
1. We can use multiple languages inside one assembly. Each language should be compiled in its own netmodule. This can't be done otherwise.
2. Modularity - We can replace netmodules without affecting other netmodules.
3. Late Loading - The netmodule will only be loaded if and when used.
4. Less network traffic while deploying. Only the changed netmodule will be transferred.

Also this can be useful when we have an infrastructure assembly (which is shared by many solutions), with only a subset of classes changing from solution to solution. A netmodule can be useful to compile the changing part, without the need to compile the other parts of the assembly.

0 Comments:

Post a Comment

<< Home

Feel free to use everything here. Add links to my site if you wish.

Do not copy anything to other sites without adding link to here.

All the contents of the site belong to Yariv Hammer.