Application Domains and Contexts and Threads, Oh My!



Similar documents
C#5.0 IN A NUTSHELL. Joseph O'REILLY. Albahari and Ben Albahari. Fifth Edition. Tokyo. Sebastopol. Beijing. Cambridge. Koln.

The Microsoft Way: COM, OLE/ActiveX, COM+ and.net CLR. Chapter 15

Chapter 1: General Introduction What is IIS (Internet Information Server)? IIS Manager: Default Website IIS Website & Application

Single Sign On via Qlikview IIS Server

Monitoring.NET Framework with Verax NMS

CA Unified Infrastructure Management

Performance Testing. Configuration Parameters for Performance Testing

Microsoft Corporation. Project Server 2010 Installation Guide

Web Services with ASP.NET. Asst. Prof. Dr. Kanda Saikaew Department of Computer Engineering Khon Kaen University

Installation Instruction STATISTICA Enterprise Server

Como configurar o IIS Server para ACTi NVR Enterprise

Enhancing your Web Experiences with ASP.NET Ajax and IIS 7

OutSystems Platform 9.0 SEO Friendly URLs

Click Studios. Passwordstate. Installation Instructions

Click Studios. Passwordstate. Installation Instructions

Chapter 1 - Web Server Management and Cluster Topology

User Management Tool 1.6

Quick Start for Network Agent. 5-Step Quick Start. What is Network Agent?

How to Install and Setup IIS Server

Server Manager Performance Monitor. Server Manager Diagnostics Page. . Information. . Audit Success. . Audit Failure

Monitoring and Maintaining Cisco Unified MeetingPlace Web Conferencing

Building Scalable Applications Using Microsoft Technologies

Chapter 3 Application Monitors

Moldplus Server Installation Guide Version 3.10 Revision date: February 05, 2006

Chapter 2: OS Overview

Developing, Deploying, and Debugging Applications on Windows Embedded Standard 7

Demo: Controlling.NET Windows Forms from a Java Application. Version 7.3

XIA Configuration Server

Connecting Software Connect Bridge - Exchange Server Sync User manual

Basics of VTune Performance Analyzer. Intel Software College. Objectives. VTune Performance Analyzer. Agenda

Java Garbage Collection Basics

NTP Software File Auditor for NAS, EMC Edition

Useful metrics for Interpreting.NET performance. UKCMG Free Forum 2011 Tuesday 22nd May Session C3a. Introduction

Quick Start for Network Agent. 5-Step Quick Start. What is Network Agent?

MS Enterprise Library 5.0 (Logging Application Block)

Configuring Load Balancing

Live Maps. for System Center Operations Manager 2007 R2 v Installation Guide

How to Setup SQL Server Replication

Enterprise Knowledge Platform

Performance in the Infragistics WebDataGrid for Microsoft ASP.NET AJAX. Contents. Performance and User Experience... 2

Server Installation Manual 4.4.1

Load Testing and Monitoring Web Applications in a Windows Environment

CafePilot has 3 components: the Client, Server and Service Request Monitor (or SRM for short).

Kepware Technologies Optimizing KEPServerEX V5 Projects

Event processing in Java: what happens when you click?

q for Gods Whitepaper Series (Edition 7) Common Design Principles for kdb+ Gateways

Sage 300 ERP Sage CRM 7.1 Integration Guide

Click Studios. Passwordstate. Upgrade Instructions to V7 from V5.xx

Password Reset PRO. Quick Setup Guide for Single Server or Two-Tier Installation

Configuring.NET based Applications in Internet Information Server to use Virtual Clocks from Time Machine

Best Practices for Application Management in Introscope. Abhijit Sawant

ADMINISTRATION & USAGE GUIDE SHORETEL CALL RECORDER ShoreTel Professional Services

STATISTICA VERSION 10 STATISTICA ENTERPRISE SERVER INSTALLATION INSTRUCTIONS

StoreGrid Backup Server With MySQL As Backend Database:

HTG XROADS NETWORKS. Network Appliance How To Guide: EdgeDNS. How To Guide

GRAVITYZONE UNIFIED SECURITY MANAGEMENT. Use Cases for Beta Testers

Using WhatsUp IP Address Manager 1.0

JMC Next Generation Web-based Server Install and Setup

TIBCO Spotfire Web Player 6.0. Installation and Configuration Manual

Crystal Reports Licensing Evolution

PART IV Performance oriented design, Performance testing, Performance tuning & Performance solutions. Outline. Performance oriented design

Parallelization: Binary Tree Traversal

Cache Configuration Reference

XyLoc Security Server w/ AD Integration (XSS-AD 5.x.x) Administrator's Guide

Polycom RealPresence Resource Manager System Getting Started Guide

SAN Conceptual and Design Basics

TARGETPROCESS INSTALLATION GUIDE

RoomWizard Synchronization Software Manual Installation Instructions

Backup Server DOC-OEMSPP-S/6-BUS-EN

Gladinet Cloud Backup V3.0 User Guide

IBM Tivoli Composite Application Manager for Microsoft Applications: Microsoft Internet Information Services Agent Version Fix Pack 2.

Modeling Workflow Patterns

Migrating IIS 6.0 Web Application to New Version Guidance

Freshservice Discovery Probe User Guide

JBoss Seam Performance and Scalability on Dell PowerEdge 1855 Blade Servers

User's Guide - Beta 1 Draft

Sage 300 ERP Sage CRM 7.2 Integration Guide

SETTING UP ACTIVE DIRECTORY (AD) ON WINDOWS 2008 FOR EROOM

Monitoring Oracle Enterprise Performance Management System Release Deployments from Oracle Enterprise Manager 12c

How to Configure the Windows DNS Server

Table of Contents. This whitepaper outlines how to configure the operating environment for MailEnable s implementation of Exchange ActiveSync.

How To Install An Aneka Cloud On A Windows 7 Computer (For Free)

SSC - Concurrency and Multi-threading Java multithreading programming - Synchronisation (I)

Web Server (Step 1) Processes request and sends query to SQL server via ADO/OLEDB. Web Server (Step 2) Creates HTML page dynamically from record set

Kaspersky Lab Mobile Device Management Deployment Guide

Infrastructure that supports (distributed) componentbased application development

WatchGuard SSL v3.2 Update 1 Release Notes. Introduction. Windows 8 and 64-bit Internet Explorer Support. Supported Devices SSL 100 and 560

Chapter 2 TOPOLOGY SELECTION. SYS-ED/ Computer Education Techniques, Inc.

Table of Contents. Chapter 1: Installing Endpoint Application Control. Chapter 2: Getting Support. Index

Introduction CORBA Distributed COM. Sections 9.1 & 9.2. Corba & DCOM. John P. Daigle. Department of Computer Science Georgia State University

Citrix EdgeSight for NetScaler Rapid Deployment Guide

Nota Tecnica UBIQUITY 4 TN0016. The document describes the latest updates introduced in Ubiquity 4.

EXAM PRO:Design & Develop Windows Apps Using MS.NET Frmwk 4. Buy Full Product.

How to Scale out SharePoint Server 2007 from a single server farm to a 3 server farm with Microsoft Network Load Balancing on the Web servers.

POA 2.8 Hotfix 02 Release Notes

Server & Workstation Installation of Client Profiles for Windows

Transcription:

Application Domains and Contexts and Threads, Oh My! Michael Stiefel co-author Application Development Using C# and.net

Why Understand App Domains? By understanding Application Domains you will have an opportunity to unify and tie together various concepts and mechanisms with.net. You will meet them sooner or later. But when will I meet them?

When you least expect to... Dorothy: Do you suppose we'll meet any strange concepts when programming with.net?" Tinman: "We might." Scarecrow: Concepts that...that eat straw?" Tinman: "Some. But mostly application domains, and contexts, and threads." All: Application Domains and Contexts and Threads, oh my! Application Domains and Contexts and Threads, oh my!"

Scalability The more applications that can run simultaneously, the more scalable the solution. For applications to run together, they must be isolated from each other. If one application crashes, the others must continue to run. One application cannot directly access the data, or directly call methods, in another application.

Win 32 Application Isolation Applications are isolated in separate processes. Each process has its own address space. A process context switch is usually implemented in the processor. Processes are inefficient for really scalable solutions (tens to hundreds of thousands). Process switches are slow. Interprocess communication is much slower than intraprocess communication.

.NET Application Isolation The Common Language Runtime uses Application Domains to provide isolation in software. A type-safe assembly s types and methods can only be accessed in well defined ways. Hence, the CLR can prevent direct access to types from one application domain to another. Application Domains can contain multiple assemblies.

Application Domains Security Evidence Configuration Information Loaded Assemblies Code Isolation Think of ASP.NET as an example.

Code Access Security Policy Assemblies are assigned a certain level of trust. For a given level of trust certain permissions are allowed, others are denied. Examples of permissions are the right to access a file, use the clipboard, or make or accept Web connections. A particular level of trust is defined by a Code Group. A code group is defined by various characteristics such as whether the assembly is running on the local computer or on a particular web site.

Security Evidence Evidence is the information associated with an assembly that allows the CLR to determine to which Code Group the assembly belongs. Examples of evidence: System.Security.Policy.Zone : MyComputer System.Security.Policy.Url : file://e:/appdomainsecurity.exe The Evidence class represents the collection of evidence. Classes such as Zone and Url model the individual pieces of evidence.

Configuration Files If an assembly has a strong name, you can specify the version policy. You can also specify where to locate assemblies that have not yet been loaded. The directory in which the application runs is known as the application base. The AppDomainSetup class models this information.

AppDomainSetup Class Properties ApplicationBase root application directory ConfigurationFile Gets or sets the name of the configuration file for an application domain. LoaderOptimization Specifies the optimization policy used to load an executable. PrivateBinPath Gets or sets the list of directories that is combined with the ApplicationBase directory to probe for private assemblies. PrivateBinPathProbe Gets or sets the private binary directory path used to locate an application. Private assemblies allow for building isolated apps.

AppDomain Class Represents an application domain. To create an application domain instance: public static AppDomain CreateDomain( string appdomainname, Evidence appdomainevidence, AppDomainSetup appdomainsetupinformation) Application domain creator can specify security evidence, and configuration. Allows for building applications that cannot interfere with one another.

Executing Code Once an assembly has been created you can invoke methods to start executing code. ExecuteAssembly Load Execute code in assembly starting with entry method. Loads an assembly. CreateInstance Create a type, invoke methods through reflection. Can modify the evidence and culture.

AppDomain Example Evidence ev = AppDomain.CurrentDomain.Evidence; Evidence evidence = new Evidence(ev); evidence.addhost(new Url(@ file://f:/ ); evidence.addhost(new Test()); AppDomainSetup setupinfo = new AppDomainSetup(); setupinfo.configurationfile = "foo.config"; AppDomain appdomain = AppDomain.CreateDomain("NewAppDomain", evidence, setupinfo); appdomain.executeassembly("targetassembly.exe");

Unloading Application Domains When an application finishes executing the AppDomain can be unloaded. Individual assemblies in an application domain cannot be unloaded. The default (initial) application domain of a process cannot be unloaded.

TypeResolve Event If an assembly load fails, the type resolve event is raised. If handled, the application domain can provide the necessary assembly using any rules it wants, including building it on the fly.

Threads A process can have one or more threads of execution. Threads are scheduled by the system. A thread s context includes the machine registers and the stack.

Thread Class The currently executing thread is found from the static property Thread.CurrentThread..NET threads run as delegates define by the ThreadStart class: public delegate void ThreadStart(); A thread instance is represented by the Thread class, the Start method causes the thread to run. The Join method causes a thread to wait on another thread.

Thread Synchronization Using threads can cause race conditions. Does i = i + 1 represent a single statement? What could go wrong?

Monitor Class The Monitor class allows you to set up a critical section where only one thread can execute at a time. Monitor.Enter(object o), Monitor.Exit(object o) You can wait and signal objects. Monitor.Wait, Monitor.Pulse, Monitor.PulseAll You do not have to block on Monitor.Enter. Monitor.TryEnter

Interlocked Class The Interlocked class has methods for insuring the increments, decrements, and exchanges for single values update correctly. Interlocked.Increment(ref OutputCount);

Synchronization Attributes Deriving your class from ContextBoundObject, you can use the SynchronizationAttribute class to let the system handle the synchronization for you for all instance methods on the object The SynchronizationAttribute class has 4 values: REQUIRED, REQUIRES_NEW, SUPPORTED, NOT_SUPPORTED [Synchronization(SynchronizationAttribute.REQUIRED)] class Counter : ContextBoundObject {...

Threads and Application Domains A process can have multiple application domains and multiple threads: what is the relation between a thread and an application domain? Application domains run on the thread that created them.

Code Sample ThreadStart x = new ThreadStart(RunCode); Thread t = new Thread(x); t.start();... static void RunCode() {... AppDomain appdomain = } AppDomain.CreateDomain("NewAppDomain", evidence, setupinfo); appdomain.executeassembly("targetassembly.exe");

CLR Hosting If you need to use.net from within a legacy application, you can write a CLR Host. An CLR Host must load the CLR, setup the application domains, and then execute their code. ASP.NET uses an ISAPI filter to start the CLR and load the Web services apps. Yukon will use CLR Hosting so that you can write stored procedures in.net languages.

Starting up the CLR Since side-by-side versions of the CLR can co-exist, you can specify which version to startup. If unspecified, the latest version is used. In Version 1, only one CLR version runs at a time in a process. Specify which CLR execution engine you want: workstation or server. Uniprocessor machines use workstation version. Specify which garbage collector you want. Request ICorRuntimeHost interface to start creating application domains.

CorBindToRuntimeEx CComBSTR bstrver(l"v0.0.0000"); CComBSTR bstrclrtype(l"wks"); CComPtr<ICorRuntimeHost> phost; HRESULT hr = CorBindToRuntimeEx(NULL, bstrclrtype, STARTUP_LOADER_OPTIMIZATION_MULTI_DOMAIN STARTUP_CONCURRENT_GC, CLSID_CorRuntimeHost, IID_ICorRuntimeHost, (void **)&phost); if (!SUCCEEDED(hr)) {... }

Server and Workstation Engines Server version takes advantage of multiple processors. Garbage collection can take place on each processor in parallel. Single processor machines always get workstation version.

Garbage Collection Settings Concurrent Garbage Collection occurs when garbage collection is done on background threads. More responsive UI, but slower overall performance. Nonconcurrent Garbage Collection (default) is done on the user code thread. Always better for server applications, better performance

ICorRuntimeHost Can stop and start CLR. Create the default domain. phost->start(); CComPtr<IUnknown> punkdefaultdomain; hr = phost->getdefaultdomain(&punkdefaultdomain); CComPtr<_AppDomain> pdefaultdomain; hr = punkdefaultdomain->queryinterface( uuidof(_appdomain), (void**) &pdefaultdomain);... phost->stop();

Managed and UnManaged Hosts The hosting code is divided into a unmanaged and managed parts. It is far easier to manage application domains from managed code. Avoids transitions from managed to unmanaged code. The next step is to start up the managed host and invoke the startup method in the process default domain.

Code Sample CComBSTR bstrassembly(l"apphost"); CComBSTR bstrtype(l"managedhost"); hr = pdefaultdomain->createinstance(bstrassembly, bstrtype, &pobjecthandle);... hr = v.pdispval->queryinterface( uuidof(_object), (void**) &pobj);... _Type* ptype; hr = pobj->gettype(&ptype);... CComBSTR bstrargument(l"startmanagedhost"); _MethodInfo* mi; hr = ptype->getmethod_6(bstrargument, &mi);... CComVariant vreturnvalue; hr = mi->invoke_3(vhandle, NULL, &vreturnvalue);

Managed Host The managed host then manages a thread pool to run the application domains it manages. The appropriate security, configuration files and location are used as appropriate.

Code Isolation How can you use data or call methods that live in another application domain? Marshal by Value (the data gets copied) Marshal by Reference (you get a reference to the data) This applies to function arguments or data access.

Marshal By Value Serialization makes a copy of the object. Use Serialization attribute or implement ISerializable Unfeasible for large objects (performance) Duplicates object, no network hit to access object [Serializable] class Counter {...

Marshal By Reference Derive object from MarshalByRefObject and you will get a reference to the data in the other application domain. Uses proxies to access object Use if state must stay in one app domain or object is too large to copy class Counter : MarshalByRefObject {...

Context Contexts are used inside of application domains to control access to objects that require a special execution environment. Derive the object from ContextBoundObject which derives from MarshalByRefObject. Proxies are used even with an application domain so the infrastructure can provide the necessary services. I.E.: different threading or transaction requirements. Objects in the same context do not use proxies.

Summary Application Domains provide application isolation in software. Threads can run across application domains providing independent execution paths in an application. Contexts provide a means for the infrastructure to provide services to make different application objects work together.