Common Language Runtime: Manages Running Code
Common Language Runtime: Manages Running Code
Page 1
ADO.NET and XML Base Class Library Common Language Runtime Windows COM+ Services
Exception handling
The CLR supports a standard exception- handling mechanism that works across all languages.
n Filters (lowest level) exceptions: [C#] try { //Some code that access a file }
Garbage Collector
catch(FileNotFoundException e) { Console.WriteLine("[Data File Missing] {0}", e); } n Prepare state for exception
ABB
Security support Debugging support
Page 2
Interoperation Support
What is Assemblies
n
Collection of functionality that is built versioned and deployed as a single implementation unit. The manifest: ( use MSIL disassembler Ildasm.exe to view)
n Establishes the identity, version, culture and digital signature. n Defines what files are included in the assembly n Specifies the types and resources that make up the assembly,
properly. n
ABB
Single vs Multifile Private Assemblies
n Used by a single application n Stored in the same application directory as the application n Name does only need to be unique within the application scope n Guaranteed that the application will run with the bits it was built
Page 3
with
Shared Assemblies
Signing An Assembly
Calculus.dll
n Used by many applications at the same time n Must have a cryptographically unique strong name n Deployed in the global assembly cache %windir%\Assembly IL Metadata Manifest Assembly files (self & RUT.mod) Public assembly types (self & RUT.mod) Public Key Embedded in PE File Hash PE File Hash Value Signed with private key RSA Digital Signature
Embedded in PE File
Public Key
ABB
Satellite Assemblies How the Runtime Locates Assemblies
Page 4
en
sv
.NET PE File
Zh-CN n One core assembly with the code n Different resource assemblies for each culture JIT Compilers CLRs Virtual Execution Engine
n n
1.
n Administrator policy or machine.config <dependentAssembly> <assemblyIdentityname="myasm" publicKeyToken="e9b4c4996039ede8" /> <bindingRedirect oldVersion="1.0.0.0" newVersion="2.0.0.0"/> </dependentAssembly>
assembly.
1. May be overridden with <publisherPolicy apply=yes|no> in the
in the config subdirectory of the run-time installation. The version presented here is final and cannot be overridden. 2.
If the assembly has been bound to before, use the previously loaded assembly.
JIT
en-US
Main code
Class Loader
Verifier
ABB
How Assembly References are Resolved part 2
Execution in the CLR
CLRs Virtual Execution Engine
Page 5
3. 4.
Checks the GAC for the assembly, if found, use it. Probes for the assembly in the following steps:
1. If configuration and publisher policy do not affect the original JIT Compiler Verifier Class Loader
reference and the request was made using Assembly.LoadFrom(), then use this information
2. If a <codebase> is found in the application configuration file
then use it, if not found at that specified location the request fails.
3. Further probing using application base, culture, name and
private binpath. 5.
Execution
If not found ask windows installer to install it. It acts as an install on demand feature.
System.Drawing Drawing2D Imaging Printing Text System.Xml XSLT XPath System Security ServiceProcess Text Threading Serialization
ADO.NET and XML Base Class Library Common Language Runtime Windows COM+ Services
ABB
Language Interoperability A Unified Programming Model
Consistent API availability regardless of language and programming model
Page 6
n The CLR specifying and enforcing a common type system n Inherit implementation from other types implemented in other
languages
n Debuggers, profilers and other tools requires only to understand one
.NET Framework
RAD, Composition, Delegation Subclassing, Power, Expressiveness Stateless, Code embedded in HTML pages
VB Forms
MFC/ATL
ASP
Windows API
Windows Forms
n n n
Uses virtual methods to override message responses No message loop Component model that supports events
MyButton.Click += new EventHandler (OnButtonClicked); private void OnButtonClicked (object sender, EventArgs e) { MessageBox.Show ("Click!");}
n n
Event is using delegates as function pointers Main form is started and then it is up to the components to fire events
ABB
Programming model
n
Page 7
Organization
n Organized in hierarchical namespaces
Component Oriented
n Properties, methods & events are 1st class n Design-time functionality
.NET Framework
Form form = new Form(); form.Text = "Main Window"; form.Show();
Everything Is An Object
n
Everything Is An Object
n
Boxing
n Allocates box, copies value into it
Unboxing
n Checks type of box, copies value out
Dim i As Integer = 123 Dim o As Object = i Dim j As Integer = CInt(o) i o j 123 123 123
System.Int32
ABB
From COM to .NET From COM to .NET
Page 8
COM Registration GUIDs .IDL files HRESULTs IUnknown AddRef /Release CoCreateInstance QueryInterface
.NET self describing components hierarchical namespaces source code to metadata structured exceptions Root object class Garbage collector new operator Type.GetInterface
n No registration required
n Code is completely self- describing n Simply xcopy components to application directory
n Zero-impact install
n Installing one application will not affect another
Evolution of COM
COM
COM+
MTS
ABB
Overview of COM+ Services Object Pooling
Page 9
Pools are:
n n
CanBePooled(), lets you tell COM+ whether your object can be pooled when this method is called. Activate( ) and Deactivate( ) to perform initialization and cleanup.
Transactions
n n
Attribute-Based Programming
Collections of operations that form a single logical unit of work are called transactions. Transaction must be atomic (i.e., either all or none of the operations in the transaction are completed).
n n
By taking advantage of COM+ Services transactions are easily implemented in .NET (as we shall see).
Interceptor programs look for special attributes and provide additional service if they are present. Used for accessing COM+ Services from within the .NET framework. The programmer does not need to know the details of how this is implemented to use COM+ Services. The .NET Reflection API can be used to implement
n n
ABB
Example: Transactions (C#)
Example adapted from OReillys .NET Framework Essentials
Page 10
using System; using System.EnterpriseServices; // Installation options omitted [Transaction(TransactionOption.Required] Public class Customer : ServicedComponent { public void Add(...) { try { // Add new customer to the system and update dbs ContextUtil.SetComplete(); // Commit } catch(Exception e) { ContextUtil.SetAbort(); } } }
using System; using System.EnterpriseServices; // Installation options omitted [Transaction(TransactionOption.Required] Public class Customer : ServicedComponent { [AutoComplete] public void Add(...) { // Add new customer to the system and update dbs } }
Exercise 1
1 2 3 Windows Application
n
Private Assembly
Goal:
n Get to know WinForms and how to create applications in .NET
using C# n
Shared Assembly
Requirements
n Must have Visual Studio.NET and the framework
1 2 3 Web Application
Prerequisites
n Know C++/Java or C#
n
Web Service
Deliverable
n A simple window based application
ABB
Exercise 1
n
Page 11
Exercise 2
Goal:
n Implement an interface that will be used in the following labs n Add a strong name to the interface and put it in the GAC n Get to know private assemblies
Requirements
n Lab 1 finished
Deliverable
n An assembly that provide a common interface for the successive
exercises
n A private assembly that use the common interface
Useful tools
n COM Interoperability
n TlbImp.exe Imports a COM type library into a proxy .NET assembly n RegAsm.exe Enters a .NET component into the registry n TlbExp.exe Exports a .NET components type information n AxImp.exe Creates a .NET proxy for an ActiveX control so that the control
Useful Tools
n Assembly management
n ildasm.exe examines assemblies and shows the manifest n Gacutil.exe adds and removes assemblies to the Global Assembly Cache n sn.exe generates and manages private and public keys XML n Al.exe Assembly generation utility is used to generate assemblies with
n .NET Configuration
n Mscorcfg.msc manages publication polices and assemblies
ABB
Additional Resources
n n n n n n n n n
Page 12
msdn.microsoft.com/net/ www.gotdotnet.com www.asp.net www.microsoft.com/trainingandservices www.msjogren.net/dotnet/swe/ msdn.microsoft.com/msdnmag/netindex.asp www.dotnetwire.com/ www.devx.com/dotnet/ NET Framework Essentials
n THuan Thai & Hoang Q. Lam, OREILLY 2001
www.msdn.microsoft.com
n Source for many papers/tutorials/tools etc.