Professional Documents
Culture Documents
Ch6 NET
Ch6 NET
NET Components
Objectives of Chapter 6 .NET Components Introduce .NET framework Introduce the concepts of .NET components Discuss the types of .NET components, connections of components, and their deployments Distinguish local and distributed components Distinguish aggregation and containment compositions Distinguish synchronous and asynchronous method invocations Provide step by step tutorials on building, deploying, and using .NET components
Overview of .NET Framework The .NET framework is introduced by Microsoft in 2000. The .NET framework is a platform for rapid and easier building, deploying, and running secured .NET software components to be integrated in applications as well as for rapid developing XML web services and applications. It provides a highly productive, component-based, multi-languages environment for integrating existing applications with internet to meet the challenges of new applications for deployment and operation of internet-scale applications.
Overview of .NET Framework (cont.) The .NET framework encompasses a virtual machine that provides a new platform for software development. The core of the .NET framework includes XML and Simple Object Access Protocol (SOAP) to provide web services over Internet. The concept of the .NET framework is to facilitate the developments of desktop, window, and webbased application services on Windows platform and make them available and accessible not only on Windows platform but also on other platforms through common protocols such as SOAP and HTTP.
Foundation of .NET framework CLR (cont.) The IL code is like Java byte code. Regardless of the types of source code programming languages IL codes can communicate with each other by supports of the CLR. The IL code can be either in the format of executable (.EXE) or Dynamic Link Library (.DLL). If these IL codes are generated by .NET compiler, they are called managed code. The managed code can be executed only on .NET aware platform. Some DLL or EXE generated by non .NET compilers (such as early version of VC++) are called un-managed code.
CLR In summary, The CLR is a high performance execution engine. It provides a code-execution environment which manages the code targeting the .NET framework. The code management includes management of memory, thread, security, code verification and IL compilation.
The .NET Framework Class Library (cont.) The root namespace in the class library is System namespace, which contains many basic classes such as Object, Console, and may contain subNamespaces such as IO, Net, Data, Remoting, etc. For example, XML is a sub-Namespace of System namespace which is deployed as System.XML.dll, ADO.NET is available in System.Data.dll which corresponds to System.Data namespace, and Form based UI classes are available in System.Windows.Forms.dll corresponding to namespace System.Windows.Forms.
The .NET Framework Class Library (cont.) Developers can create custom namespace and organize related classes in a custom namespace. A namespace can be deployed as an assembly of binary components. Same name classes can be placed in different namespaces because they are referenced by different namespace prefix. In order to use classes in a namespace a directive using <namespace> in C# or import <namespace> in VB must be included at the beginning of code. The system built-in basic class library is deployed in mscorlib.dll file.
The Component Model of .NET MSIL DLL components are replacing COM Components, the MSIL Remoting Channels EXE components are replacing DCOM Component, and the Web Service components are new SOAP components intended to be cross-platform, and cross-language web based components. Also, the .NET component technology is unified language oriented. Any .NET component is in the format of pre-compiled MSIL, which can be binary plugged in by any other MSIL components or any other .NET compatible clients.
The Component Model of .NET (cont.) A .NET component is a single pre-compiled and self described MSIL module built from one or more classes or multiple modules deployed in a DLL assembly file. An assembly consists of up to four parts:
1) Manifest (table of info records): name of assembly, key info version, strong name, culture info, files that make up assembly, reference depended assemblies exported info. 2) Metadata of modules 3) IL code of module 4) Resources such as image files.
The Component Model of .NET (cont.) There are many different types of components in the .NET framework. We can classify them into visual or non-visual component categories. A visual component is a control which can be deployed in a toolbox as an icon for drag and drop in a window form container. We focus on non-visual component, which is known as .NET component.
The Component Model of .NET (cont.) A .NET component can be a local component (.dll), which can only be accessed locally (within same application domain), in same machine or a remote (distributed) component (.exe), which can be accessed remotely (across application domains) in same machine or different machines. A .NET DLL component can be deployed as a private component, which knows the target client or can be deployed as a shared public component, which does not know the target client. A DLL component can be plugged-in to Windows form, Web form of another DLL or application.
Using Lib.dll
NameSpace Lib
Lib.dll
Lib.dll
CLR Host
The Component Model of .NET (cont.) In TempConvCSClient.cs the client loads TempConv namespace by using TempConv directive and instantiates an instance of the TempConvComp component and invokes fToC() and cToF() methods provided by this component. The Client application in C# can be built by the following command: >csc /t:exe /r:TempConvTemp.dll TempConvCSClient.cs p TempConvCSClient.exe
#include "stdafx.h" #include <iostream> #using <mscorlib.dll> #using "TempConv.dll" using namespace System; using namespace std; // This is the entry point for this application #ifdef _UNICODE int wmain(void) #else int main(void) #endif { int choice; double input; double output; bool next = true; TempConv::TempConvComp * myCSConvComp = new TempConv::TempConvComp ();
.NET containment compositions In containment compositions, if a request to the outer component needs helps from an inner component the request is forwarded to that inner component. The outer component does not expose the interface of the inner component. The containment is transparent to the client of an outer component. The client is blind of the handler of the request. The outerM2() delegates a request to the innerM() method of inner component
outerM2()
innerM()
inner outer
.NET mixed component compositions A .NET component can also be composed by mixed aggregations and containments in a flat structure or nested compositions in multiple levels in depth. Here we use an example to explain the concepts of combined containment and aggregation compositions.
Component A Call
Component B Method
Application Domain
The communication of Components by Event and Delegate The .NET Delegate is a method type (a reference to a method) which is similar to function pointer in C++, but it is type-safe and secure. A delegate will delegate a flow control to its registered event handler when the event is raised. It works in the pattern of observer which is kind of event listener in Java. An instance of a Delegate can hold a static method of a class or a method of a component or a method of object itself. There are two types of Delegates: SingleCast or MultiCast.
Remoting Connectors for .NET Distributed Components A component or a client cannot directly access a remote component running in different application domain in same or different processes unless using Remoting channel connection. The marshaling makes it possible to invoke a remote method of a distributed component. There are two ways to marshal an object: in MBV (Marshal by Value) server passes a copy of object to client or in Marshal by Reference (MBR) client creates a proxy of a remote object. When a remote component must run at a remote site, MBR is the only choice.
Remoting Connectors for .NET Distributed Components (cont.) A channel is the way to implement communications between clients and remote components. We use TCP channel in our example. The channel is similar to the socket communication in Java. Each channel must bind with a port. A client channel binds a client port and the server channel binds a port on the server. We create TCP channel on port 4000 and register the channel with the remote class and URI name, which will be used by client to get an object of remote component. We also need to create a TCP channel and register it on the client site.
Remoting Asynchronous Callback Invocation Between Distributed .NET Components The Remoting asynchronous callback is based on Remoting Delegate. It will not block out the client while waiting for notification from remote components. For example, someone wants to be notified once the stock prices reaches a specified level. Instead of pooling the stock price all the time, why not let the server notify you and you can do whatever you want to do. In some other cases, the jobs on server will take very long to complete, why not let the server notify you when the job is done. When client makes a synchronous call to remote method of remote component, it passes a callback method to server to be called back late through Remoting Delegate.
Remoting Asynchronous Callback Invocation Between Distributed .NET Components (cont.) Two Delegates: one is Mydelegate pointing to the remote method cToF of remote component named TempConvDotNET. The other asynchronous Delegate is AsynchCallback which is passed to BeginInvoke method of MyDelegate. using System; using System.Threading; using System.Runtime.Remoting; using System.Runtime.Remoting.Messaging; using System.Runtime.Remoting.Channels; using System.Runtime.Remoting.Channels.Tcp;
Remoting Asynchronous Callback Invocation Between Distributed .NET Components (cont.) System.Concole.WriteLine(Hit <enter> to exit ); System.console.ReadLine(); return 0; } public static void MyCallBack(IAsyncResult ar) { MyDelegate d = (MyDelegate)((AsyncResult)ar).AsyncDelegate; Coinsole.WriteLine(d.EndInvoke(ar)); Coinsole.WriteLine(See you next time); } }
We can find two Delegates here. One is MyDelegate pointing to the remote method cToF of distributed component, the other is AsyncCallback pointing callback method MyCallBack. The first parameter of BeginInvoke is a 32 degree in Celsius for cToF remote method and the second parameter is a callback Delegate. The callback will not block client program. When the distributed component completes the conversion work the callback method is called and IAsyncResult is returned back to client.
NET Component Deployments A .NET component can be deployed as private component or public shared component in an assembly file. The assembly is an atomic deployment (distribution) unit in .NET Framework. A private component knows the component where it will be plugged-in. A public shared component does not know which component will use itself. It must be published (registered) in a centralized repository Global Assembly Cache (GAC). A shared component supports side-by-side multiple version component execution.
Private Deployment A Private component must be deployed in a same directory of its client or sub-directory of where the client is. It is the simplest deployment done by copying all components to where the client is. The undeployment is done by simply removing all related .dll components from the directory. A private component does not support versioning control and for in-house development within a company. The following shows an example of private deployment
dir
client.exe.config
Client.exe
Client.cs
dir1
dir2
Comp1.dll
Comp2.dll
Private Deployment (cont.) >csc /t:library/out:dir1\comp1.dll comp1.cs >csc /t:library/out:dir2\comp2.dll comp2.cs >csc /r:dir1\comp1.dll, dir2\comp2.dll /out:client.exe client.cs
A XML configuration description file is required if components are not located in the same directory with their client. The private path must be specified in the probing sub-tag of assembly building tag in the application configuration file with an extension of config Here is a sample of configuration file: <configuration> <runtime> <assemblyBinding> <probing privatePath = dir1; dir2 /> </assemblyBinding> </runtime> </configuration> The CLR will use private path to look for required component to load if the CLR cannot find required component in current directory.
The most popular reusable component deployment is to deploy a component with a strong name, to register it with GAC. A shared component with strong name can make itself unique by public/private key pair. A shared component registered with GAC can make itself to be shared anywhere.
sn utility
Client manifest
token
The steps needed to create a shared .NET component are as follows: Step 1: Create a pair of public key/private key by sn.exe utility. > sn k mykey.snk The public key is for verification against private key which is signed as a cryptographic signature stored in component assembly. The public key is stored in a manifest of the assembly. When a client references the component the public key token is stored in the clients assembly.
Public shared deployment (cont.) Step2: Embed following lines into source code of component using System.Reflection: [assembly:AssemblyKeyFile (mykey.snk)] [assembly:AssemblyDelaySign (false)] [assembly:AssemblyVersion (1.2.3.4)]
The version number of a shared component is marked by four discrete number separated by dots in the format of major.minor.build.revision. A side-by-side execution is implemented by .NET versioning. The .NET framework allows different versioned components with same name running side-byside in different applications. The CLR checks the major and minor numbers first. By default, only exact match on major.minor is allowed. The build number is backward compatible by default. In other words, Version 1.2.3.0 is compatible with 1.2.0.0 but 1.2.3.0 is not compatible with 1.2.4.0. If the version number in components manifest cant match any component in GAC it will load a versioned component that is different in revision part.
The default version policy can be customized by overriding the assembly specification in configuration file. For example: <assembleyIdentity> <binding Redirect oldVersion = 1.2.3.4 newVersion = 2.0.0.0/> </assemblyIdentity> This setting indicates the component in version 2.0.0.0 will be loaded instead of 1.2.3.4.
Public shared deployment (cont.) Step3: Register the shared component in GAC >gacutil /i mycomponent.dll Step4: Using shared component The client must make a reference to the shared component to be reused. >csc /t:exe /r:mycomponent.dll /out:myapp.exe myapp.cs