Showing posts with label Assemblies. Show all posts
Showing posts with label Assemblies. Show all posts

Native Image Generator in Microsoft dot net

The Native Image Generator utility (Ngen.exe) allows you to run the JIT compiler on your assembly'sMSIL and generate native machine code which is cached to disk.

After the image is created .NETruntime will use the image to run the code rather than from the hard disk. Running Ngen.exe onan assembly potentially allows the assembly to load and execute faster, because it restores codeand data structures from the native image cache rather than generating them dynamically.

Below are some points to be remembered for Native Image Generator:-

√Native images load faster than MSIL because JIT compilation and type-safety verificationis eliminated.

√If you are sharing code between process Ngen.exe improves the performancesignificantly. As Native image generated Windows PE file so a single DLL file can beshared across applications. By contrast JIT produced code are private to an assemblyand can not be shared.

√Native images enable code sharing between processes.√Native images require more storage space and more time to generate.

√Startup time performance improves lot. We can get considerable gains when applicationsshare component assemblies because after the first application has been started theshared components are already loaded for subsequent applications. If assemblies inan application must be loaded from the hard disk, does not benefit as much fromnative images because the hard disk access time shadows everything.

Assemblies in GAC do not benefit from Native image generator as the loader performsextra validation on the strong named assemblies thus shadowing the benefits of NativeImage Generator.

If any of the assemblies change then Native image should also be updated.

You should have administrative privilege for running Ngen.exe.

While this can fasten your application startup times as the code is statically compiledbut it can be somewhat slower than the code generated dynamically by the JIT compiler.So you need to compare how the whole application performance with Ngen.exe andwith out it.

To run Ngen.exe, use the following command line.ngen.exe install This will synchronously precompile the specified assembly and all of its dependencies.

The generatednative images are stored in the native image cache.In .NET Framework 2.0 there is a service (.NET Runtime Optimization Service) which canprecompile managed assemblies in the background.

You can schedule your assemblies to beprecompiled asynchronously by queueing them up with the NGEN Service. Use the followingcommand line. ngen.exe install /queue:Assemblies which are critical to your application's start up time should either be precompiledsynchronously or asynchronously with priority 1. Priority 1 and 2 assemblies are precompiledaggressively while Priority 3 assemblies are only precompiled during machine idle-time.

Synchronously precompiling your critical assemblies guarantees that the native images will beavailable prior to the first time your end user launches the application but increases the time takento run your application's set up program.

You can uninstall an assembly and its dependencies (if no other assemblies are dependent onthem) from the native image cache by running the following command. ngen.exe uninstall Native images created using Ngen.exe cannot be deployed; instead they need to be created on theend user's machine. These commands therefore need to be issued as part of the application'ssetup program. Visual Studio .NET can be used to implement this behavior by defining customactions in a Microsoft Installer (MSI) package.

54
RELATED POST


ERROR CHECK LIST FOR INSPECTIONS

WALK THROUGHS IN TESTING

TESTING FOR SPECIALIZED ENVIRONMENTS PART ONE

TESTING FOR SPECIALIZED ENVIRONMENTS PART TWO

VALIDATION TESTING

SYSTEM TESTING


DEBUGGING AND TESTING

DEFECT AMPLIFICATION AND REMOVAL

ITERATIVE SPIRAL MODEL

STANDARD WATER MODEL

CONFIGURATION MANAGEMENT


CONTROLLED TESTING ENVIRONMENT

RISK ANALYSIS PART ONE


RISK ANALYSIS PART TWO

BACK GROUND ISSUES

SOFTWARE REVIEWS PART ONE

SOFTWARE REVIEWS PART TWO

SOFTWARE RELIABILITY

SAFETY ASPECTS

MISTAKE PROOFING

SCRIPT ENVIRONMENT

V MODEL IN TESTING

Creating assemblies in asp.net

You can create a single-file assembly by using command-line compilers such as vbc and csc. A single-file assembly includes all the information about the component. You can use the following statement to create an assembly file with the .exe extension.

C#:

csc filename.cs

VB.NET:

vbc filename.vb

These statements create an assembly that has the same name as that of the VB or CS file and with an .exe extension. To create an assembly file with a different filename, you can use the /out: option of the compiler command, as follows:

C#:

csc /out:outputfile.exe sourcefile.cs

VB.NET:

vbc /out:outputfile.exe sourcefile.cs

In these examples, the source file must contain a single entry point, such as the Main() function. If you do not have an entry point, the compiler gives you an error message. If you do not want your source file to contain any entry point and want it to contain only other classes and methods, you must create a library assembly. A library assembly contains components that will be accessed by other assemblies. It is very similar to class libraries.

You can create a library assembly by typing the following command:

C#:

csc /t:library /out:outputfile.dll sourcefile.cs

VB.NET:

vbc /t:library /out:outputfile.dll sourcefile.cs

After you have created assemblies for all the files to be used in a project, you can create a deployment project.

Creating a multifile assembly

You might be required to create a multifile assembly if you want to use classes written in different languages. You might also be required to create a multifile assembly if you want to optimize the process of downloading components. For example, you might want to combine rarely used components into a single assembly.

When you create a multifile assembly, one of the files in the assembly must contain the assembly manifest. Let us look at the process of creating a multifile assembly with the help of an example. Consider the class ConnectDB:

'ConnectDB.VB
'-------------
Imports System.Data
Imports System.Data.SQL
Namespace DB
Public Class ConnectDB
:
Functions for establishing connection
:
End Class
End Namespace

Consider another class, Calculate, which uses the class ConnectDB to connect to the sales database and returns the price of a product after calculating the discount:

'Calculate.VB

'-------------
Imports System.Data
Imports System.Data.SQL
Imports DB
Public Class Calculate
:
Call functions from the ConnectDB class
Calculate the discounted price
:
End Class

To create an assembly with these two files, you need to follow these steps:

1. Compile all the classes that are created within a namespace, which is referenced by other modules. In this example, the class ConnectDB is created within the namespace DB. The DB namespace is accessed in the Calculate class. Therefore, you first need to build the ConnectDB
class into a module by using the following statement:

vbc /t:module ConnectDB.vb /r:system.dll /r:system.data.dll

When you want to create a module instead of a library assembly or an executable file, you need to specify the /t:module option, which instructs the compiler to create a standard DLL file that does not contain the assembly manifest. The /r option is used to specify references to other libraries. This statement creates a module called ConnectDB.mcm.

The default extension for a module is .mcm. You can change the default name of the output file generated by the compiler by using the /out: option of the compiler.

2. After compiling classes that are included inside namespaces, you need to compile classes that use other modules. In the example, the Calculate class references the ConnectDB module and makes calls to functions written in the ConnectDB class. Therefore, you must compile the Calculate class file by executing the following statement at the command prompt:

vbc /addmodule:ConnectDB.mcm /t:module Calculate.vb

In this statement, the /addmodule option is included to specify the name of the module, which is referenced by the file Calculate.vb. When you give this statement from the command prompt, the compiler creates a module called Calculate.mcm, which references another module, ConnectDB.mcm.

3. After compiling various classes into modules, you can create a multifile assembly by using the Al.exe utility. Type the following statement at the command prompt to create an assembly:
al /out:App.dll /t:lib ConnectDB.mcm Calculate.mcm

In this statement, the /out option specifies the name of the output file to be produced by Al.exe. The /t option specifies the file format of the output file.

You can set the option to lib (code library), exe (console application), or win (Windows-based application). Finally, you specify the names of the modules to be included in the assembly. This statement creates a library assembly called App.dll. This file contains the assembly manifest, which describes the types in other modules included in the assembly.

RELATED POST

CREATING ASSEMBLIES WITH ASP.NET

BUILDING HANDLERS IN ASP.NET

INTERFACES AND CLASSES CREATION IN ASP.NET

CACHING IN ASP.NET

CACHING IN ASP.NET PART TWO

WIRE LESS APPLICATION WITH ASP.NET

SECURITY IN ASP.NET PART ONE

SECURITY IN ASP.NET PART TWO

LOCALIZING ASP.NET APPLICATIONS

DEPLOYING ASP.NET APPLICATIONS

ASP.NET CONFIGURATION SYSTEM

WEB SERVICES IN ASP.NET

WEB SERVICES PART TWO

WEB SERVICE INFRASTRUCTURE

WEB SERVICE INFRASTRUCTURE PART TWO

EXCHANGING MESSAGES IN ASP.NET

MICROSOFT HAIL STORM

SOAP AND DOT NET PART ONE

SOAP AND DOT NET PART TWO


Assemblies

Assemblies are the fundamental building blocks of a .NET Framework application. They contain the types and resources that make up an application and describe those contained types to the common language runtime. Assemblies enable code reuse, version control, security, and deployment.

Put simply, an assembly is a project that compiles to an EXE or a DLL file. Although .NET EXE and DLL files resemble their predecessors externally, the internal structure of an assembly is quite different from that of an EXE or DLL created with earlier development tools. An assembly consists of four internal parts:

The assembly manifest, or metadata. This contains information about the assembly that the common language runtime uses to obtain information about the assembly.
The type metadata, which exposes information about the types contained within the assembly.

The intermediate language code for your assembly.

Resource files, which are nonexecutable bits of data, such as strings or images for a specific culture.

The assembly manifest contains the metadata that describes the assembly to the common language runtime. The common language runtime then uses the information in the assembly manifest to make decisions about the assembly's execution. An assembly manifest contains the following information:

Identity

It contains the name and version number of the assembly, and can contain optional information such as locale and signature information.
Types and resources. It contains a list of all the types that will be exposed to the common language runtime as well as information about how those types can be accessed.

Files

It contains a list of all files in the assembly as well as dependency information for those files.

Security permissions

The manifest describes the security permissions required by the assembly. If the permissions required conflict with the local security policy, the assembly will fail to execute.

For the most part, the developer does not have to be concerned with the contents of the assembly manifest. It is compiled and presented to the common language runtime automatically. The developer does, however, need to explicitly set the metadata that describes the identity of the assembly.

The identity of the assembly is contained in the AssemblyInfo.vb or .cs file for your project. You can set identity information for your assembly by right-clicking the AssemblyInfo icon and choosing View Code from the drop-down menu. The code window will open to the AssemblyInfo code page, which contains default null values for several assembly identity attributes. The following code example shows an excerpt from the AssemblyInfo file.

Creating Class Library Assemblies

You will frequently want to create class library assemblies. These represent sets of types that can be referenced and used in other assemblies. For example, you might have a custom control that you want to use in several applications or a component that exposes higher math functions.

Such an assembly is not executable itself, but rather must be referenced by an executable application to be used.

You can create class library assemblies and control library assemblies by using the templates provided by Microsoft Visual Studio .NET.

The class library template is designed to help you create an assembly of types that can be exposed to other applications, and the Microsoft Windows control library template is provided to assist you in building assemblies of custom controls.
  1. To create a class library assembly
  2. From the File menu, choose New, and then choose Project.
  3. The New Project window opens.
  4. In the New Project window, select Visual Basic Projects or Visual C# Projects as appropriate, and then choose Class Library or Windows Control Library as appropriate.
  5. Write the code for your class library or Windows control library.
  6. Set any required identity information in the AssemblyInfo file of your assembly.
  7. From the Build menu, choose Build to build your assembly.
Resources and Resource Assemblies

Most production-ready applications will use resources. Resources are nonexecutable data that is embedded in an application. Examples of resources might include strings that are displayed in the user interface based on the culture settings of the system or a set of images. Packaging such data into resource files allows you to change the data required by a program without recompiling the entire application.

In this section, you will learn to create resource files manually, how to embed these resource files into assemblies or create resource-only assemblies, and how to use code to retrieve code and objects stored in resource assemblies.

Creating Resource Files

The .NET Framework includes a sample application called ResEditor that can be used for creating text and image resource files. The ResEditor application is not integrated with Visual Studio .NET—it must be run separately. In fact, it is supplied as source code files and must be compiled before it can be used.

The ResEditor source files are located in the \FrameworkSDK\Samples\Tutorials\resourcesandlocalization\reseditor folder, which is in the folder that Visual Studio .NET is installed in. You can build the application using either the batch file supplied in that folder or by adding the source files to an empty Visual Studio project, configuring, and then building them.

To create a resource file with ResEditor

If necessary, compile the ResEditor source files. They can be found in the \FrameworkSDK\Samples\Tutorials\resourcesandlocalization\reseditor folder, which is in the folder that Visual Studio .NET is installed in.

Open ResEditor. If you want to edit a preexisting .resources or .resx file, you can open it by choosing File, Open, and then browsing to the appropriate file.

From the Add drop-down menu, choose the type of resource you want to add. In the Add text box, type the name that the resource will use. Click the Add button to add the resource to the file.

In the main pane, click the cell next to the resource name to specify the value. If the resource is a string, type the string into the box. If it is an image, browse to the correct file.

If you want to rename any of the resources, highlight the resource you want to rename by clicking it, type the new name in the rename text box, and click the Rename button to apply the new name. If you want to delete a resource, highlight the resource by clicking it and choose Delete from the Resources menu.

When you have finished editing your resource file, choose File and then Save As to save your file. You can save it as either a .resources file or a .resx file.

Once created, you can add .resources or .resx files to your project by selecting Project, Add Existing Item, and then browsing to the appropriate resource file.

Embedding Resources

Once you have created resource files, you can embed them in your assembly. This allows you to package resources into the same assembly as the code files, thus increasing the portability of your code and reducing its dependence on additional files. To embed an externally created resource into your assembly, all you have to do is add the file to your project. When the project is built, the resource file will be compiled into the assembly.

To embed resources in your assembly
  1. Create the resource file. This can be either a .resources file or a .resx file. You might find it useful to use a tool such as ResEditor.
  2. From the Project menu, choose Add Existing Item.
  3. The Add Existing Item window opens.
  4. Browse to the correct resource file, and click Open to select it.
  5. The file is added to your project.
  6. From the Build menu, choose Build to build your solution.
  7. The assembly is built with the resource file embedded.
Creating Resource Assemblies

You can create assemblies that only contain resources. You might find this useful in situations where you expect to have to update the data contained in resource files, but do not want to have to recompile your application to update it.

To create a resource assembly

From the File menu, choose New, and then choose Project.
The New Project window opens.

In the New Project window, choose Visual Basic Projects or Visual C# Projects as appropriate, and then choose Empty Project. Click OK to create the project.
A new empty project is created.

From the Project menu, use Add Existing Item to add your resource files to your project.
In Solution Explorer, right-click your project and choose Properties.
The Project Property Pages open.

In the Output Type drop-down menu, change the output type of your project to Class Library.
From the Build menu, choose Build .

Your resources are compiled in the assembly.

Creating Satellite Assemblies

When creating international applications, you might want to provide different sets of resources for different cultures. Satellite assemblies allow different sets of resources to automatically be loaded based on the CurrentUICulture setting of the thread. You learned how to automatically generate applications for localization in Chapter 8. In this section, you will learn how to create additional satellite assemblies and incorporate them into your application.

Visual Studio .NET allows you to effortlessly create satellite assemblies. All you need to do to create a satellite assembly is to incorporate alternate sets of resource files that are appropriately named into your application. Visual Studio .NET does the rest upon compilation.

To be incorporated into a satellite assembly, your resource file must follow a specific naming scheme based on the culture it is designed for. The name of a resource file for a specific culture is the same as the name of the resource file for the invariant culture, and the culture code is inserted between the base name and the extension. Thus, if we had a resource file named MyResources.resx, a resource file containing alternate resources for neutral German UIs would be named MyResources.de.resx. And a version of the file containing German resources specific for Luxembourg would be named MyResources.de-LU.resx.

Once these alternate versions of the file are added to your solution, they will automatically be compiled by Visual Studio .NET into satellite assemblies, and a directory structure for them will be created. At run time, the culture-specific resources contained in these files will automatically be located by the common language runtime.

To create satellite assemblies

Create alternate versions of your resource files specific to locales where your application will be run.

Name your resource files correctly for their specific culture. Names for resource files for satellite assemblies must contain the culture code separated by periods between the base name and the extension. The base name and extension must be the same as the resource file for the invariant culture.

From the Project menu, use Add Exiting Item to add these files to your application.
From the Build menu, choose Build Solution to build your solution.

Culture-specific resource files are automatically compiled into satellite assemblies and placed in an appropriate directory structure.

Retrieving Resources at Run Time

At run time, you can use the ResourceManager class to retrieve embedded resources. A ResourceManager, as the name implies, manages access and retrieval of resources embedded in assemblies. Each instance of a ResourceManager is associated with an assembly that contains resources.

You can create a ResourceManager by specifying two parameters: the base name of the embedded resource file and the assembly in which that file is found. The new ResourceManager will be dedicated to the embedded resource file that you specify. The base name of the file is the name of the namespace that contains the file and the file without any extensions.

For example, a resource file named myResources.de-DE.resx in a namespace called Namespace1 would have a base name of Namespace1.myResources.

The assembly parameter refers to the assembly that the resource file is located in. If the assembly that contains the resources is the same assembly that contains the object that is creating the ResourceManager, you can get a reference to the assembly from the type object of your object.

Understanding Private and Shared Assemblies

Most of the assemblies you create will be private assemblies. Private assemblies are the most trouble free for developers and are the kind of assembly created by default. A private assembly is an assembly that can be used by only one application. It is an integral part of the application, is packaged with the application, and is only available to that application. Because private assemblies are used by one application only, they do not have versioning or identity issues. Up to this point, you have only created private assemblies.

At this point, something might seem incongruous about that last paragraph. For example, you learned how to create custom controls in Chapter 7, and you can use the DLL that contains your custom controls in any number of projects. Yet, these are private assemblies. How can this be?

When you add a reference to a private assembly to your project, Visual Studio .NET creates a copy of the DLL containing that assembly and writes it to your project folder. Thus, multiple projects can reference the same DLL and use the types it contains, but each project has its own copy of the DLL and therefore has its own private assembly.

Only one copy of shared assemblies, on the other hand, is present per machine. Multiple applications can reference and use a shared assembly. You can share an assembly by installing it to the Global Assembly Cache. There are several reasons why you might want to install your assembly to the Global Assembly Cache. For example:

Shared location. If multiple applications need to access the same copy of an assembly, it should be shared.

Security. The Global Assembly Cache is located in the C:\WINNT (Microsoft Windows 2000) or WINDOWS (Microsoft Windows XP) folder, which is given the highest level of security by default.

Side-by-side versioning. You can install multiple versions of the same assembly to the Global Assembly Cache, and applications can locate and use the appropriate version.

For the most part, however, assemblies that you create should be private. You should only share an assembly when there is a valid reason to do so. Sharing an assembly and installing it to the Global Assembly Cache requires that your assembly be signed with a strong name.

Strong Naming

A strong name is a name that guarantees the identity of an assembly. It consists of information about the identity of the assembly, such as the name, version number, and culture information (if present), and the public key of a public/private key pair. This information is encrypted with the private key of the key pair and can be decrypted with the public key of the key pair. Because no one but the developer has access to the private key, the strong name cannot be replicated by anyone but the developer, thereby ensuring the identity of the assembly.

RELATED POST

DAY 11 OOPS INTRODUCTION

DAY 12 POLYMORPHISM

DAY 13 INHERITANCE AND POLYMORPHISM

DAY 14 EBUGGING TOOLS IN DOT NET

DAY 15 DEBUG AND TRACE IN CLASSES

DAY 16 UNIT TEST PLAN

DAY 17 EXCEPTIONS IN VISUAL STUDIO

DAY 19 ADO.NET INTRODUCTION

DAY 20 DATA ACCESSING IN DOT NET

DAY 21 DATA BASE OBJECTS