Upcoming Courses

Essential .NET
March 3rd-7th
Los Angeles
March 17th-21st
New York


NEW-Essential Windows Communication Foundation
February 26th-29th
Boston
March 17th-20th
Chicago


NEW-Essential Windows WorkFlow Foundation
March 10th-14th
Boston


Essential ASP.NET
February 11th-15th
Boston


.NET Architecture &
Design
February 11th-15th
Boston

March 17th-21st
New York


Essential SharePoint 2007 for Developers
March 10th-14th
Boston


NEW-Essential AJAX for ASP.NET Developers
March 17th-20th
Orlando


Essential SQL Server for Developers
March 3rd-7th
Boston


Essential C#
February 18th-22nd
Los Angeles

 










Using AppDomains to Build Reliable Systems

by: Pinku Surana

Building reliable systems is very hard because it is an all or nothing game. If there exists even one
unhandled exception, the entire system is considered unreliable. This is an impossible standard to meet
for a large system. In fact, the first step to building reliable systems is to accept that it is impossible.
Instead, we will attempt to build a more reliable system from a collection of smaller unreliable
components. The idea is to manage failure rather than pursue an impossible perfection.

The motivation for this article is a programming language called Erlang (http://www.erlang.org). It was
originally designed to build soft real-time systems at Ericsson, a telecommunication company. Erlang is a
dynamically-typed functional concurrent programming language. The primary feature of Erlang is oneway
message passing between local and/or remote lightweight processes. A lightweight process in
Erlang is a very efficient way to construct small, isolated, concurrent programs. Communication is
handled by sending discrete messages asynchronously over a channel to another Erlang process. The
runtime will move the messages very efficiently if the receiver process is on the same machine;
otherwise, it will serialize and send the message over the network to a remote process. By forcing
concurrent programs into this restrictive pattern, it reduces concurrency errors and promotes
parallelism.

Erlang makes use of these lightweight processes to build reliable programs. These “processes” have
similar isolation properties as normal OS processes: protected memory, concurrency, and failure in one
process will not bring down the whole system. That last property can be used to build “supervisor”
processes, which are responsible for launching and monitoring one or more worker processes. If a
worker process fails, the supervisor will restart the worker. Therefore, the entire system will continue to
operate even if there are intermittent failures in some components. If the component fails repeatedly,
the supervisor can launch a simpler process instead to hopefully keep the system up and running.

To implement this idea in .NET, we can use .NET AppDomains to simulate Erlang processes. AppDomains
in .NET are like cheap managed processes that run on the same instance of the CLR. In .NET v1 and v1.1,
an unhandled exception in an AppDomain would fail quietly without bringing down the entire
application. Unfortunately, this behavior was removed in .NET v2; therefore, an unhandled exception in
any AppDomain (really, any thread) will bring down the entire application. We no longer have process
isolation and the entire application is vulnerable to a flaw in any part of a program. In addition to
unhandled exceptions in the normal control flow of a program, one must be careful about asynchronous
exceptions like StackOverflowException and OutOfMemoryException that can occur at any time.

For each worker process that one wishes to load into a separate AppDomain, the main supervisor
program creates an instance of a Channel which supports reliable messaging between AppDomains. Like
Erlang, messages are enqueued concurrently but processed sequentially. The Channel loads the assembly into a new AppDomain, creates an instance of the receiver type, and offers an Enqueue
method so other processes can send messages to this process.



The Receiver1 class inherits from MarshalByRefObject because we use remoting to cross AppDomain
boundaries. Remoting between AppDomains on the same CLR is very fast. This class will be in a different
assembly. The class Receiver1 has a simple method to get an input message. It also has a method that
initializes the worker and gets a mapping of process names to delegates, which send messages to the
corresponding process. So if Receiver1 wants to send messages to Receiver7 or 8, it grabs those
delegates from the dictionary.

The implementation of channels in .NET is an ad-hoc message queue within the same process.
Essentially, it is a queue that is safe for concurrent writes, because many processes may write to the
same queue concurrently. To make this a little easier to use, the Enqueue method on the queue class
will be wrapped in a Send delegate.



The channel creates another thread to monitor the queue for new messages. The code is elided here,
but it basically blocks until new messages arrive on the channel. Then it dequeues a message and calls
ReceiveMessage. Finally, here’s the code that makes the system more reliable. If ReceiveMessage fails
with an unhandled exception, the Dequeue method catches the exception, unloads the “broken”
AppDomain and reloads a fresh copy of it. Unfortunately, this code matches the semantics of Erlang: if
the process fails while processing a message, the process is restarted but that particular message is lost.

The purpose of this design is to build a system that roughly emulates the reliability feature in Erlang. The
main program (aka supervisor) creates a channel for every worker process, allocating a new AppDomain
and connecting to a Receiver object via remoting. Worker processes do not contact each other directly;
instead, they are loosely connected by a “reliable” communication channel, similar to a message queue.
If an AppDomain should fail catastrophically for any reason, the channel catches the error, unloads the
errant code and reloads the AppDomain. Though this design will not magically make your application
100% reliable, it will help your application survive the inevitable errant, uncaught exceptions that plague
all systems.

Please check out our Guerrilla Enterprise .NET and .NET Architecture and Design courses.

To contact Pinku please send email to: surana@develop.com

 

For more information email: onsites@develop.com or call 800.699.1932
 
Featured New Course


NEW- Guerrilla Enterprise .NET
June 9th-13th in Boston register online or call 800.699.19322


Learn to build robust distributed systems using the Windows Communication Foundation (WCF), Windows Workflow Foundation (WF), and BizTalk. Guerrilla Enterprise .NET shows you how to take these technologies and build systems that use them in combination, playing on their strengths and mitigating their weaknesses using service-oriented principles.

You’ll get answers to these questions:

• What does service orientation really mean?
• How do I create services that are highly interoperable?
• How can I combine the functionality of different “building block” services to create higher level functionality?
• How does session state work in WCF? When should I use it?
• How can I model business processes that run for hours, days, or even weeks?

Course Highlights:

• Understand the architecture of WCF
• Write services that support internet and intranet scenarios
• Create workflows that are robust over long-running execution
• Create BizTalk applications that provide highly scalable messaging backbones
• Understand where to use WF and where to use BizTalk in a solution
• Appreciate the pros and cons of flowing transactions around your service layer
• Create asynchronous messaging applications
• Integrate WCF and WF using the new functionality introduced in .NET 3.5


Want to get a detailed course outline?
Click here
and put "Guerrilla Enterprise" in the subject line.


 
NEW- .NET: MyTeam Custom Editions

We've developed 4 customizable .NET developer courses
based on the experience level of your team!

Start customizing your course today with a few simple answers. We'll recommend a custom edition that is designed for the experience level of your team. Then you can tailor the course to best support the application you're building.
Customize now: www.develop.com/myteam
 

Upcoming Guerrilla Events

What You Can Expect:

- Multiple instructors (2-5) per event

- Extended course hours
- A team of industry leading experts
- Leading-edge technologies
- Innovative course materials, including slides, labs, books, demos
- Your own PC and development platform
- Peer-to-peer collaboration and competition
- An exciting, multi-media learning environment


All Inclusive Price includes:
5-NIGHTS HOTEL, ALL MEALS AND BEVERAGES

ABOUT GUERRILLA EVENTS

To get a detailed course outline for any of the Guerrilla Events listed below, please send an email to requests@develop.com and put the course name in the subject line.

Featured Guerrilla Event

Guerrilla .NET
- Updated with 3.0 & 3.5 content
May 5th-9th in Boston register online or call 800.699.1932

What you'll learn..

Guerrilla .NET provides a rapid exploration of .NET design philosophy and practical advice. You learn myriad patterns and best practices, and you get hands-on experience developing applications using Visual Studio 2008. Learn to write code using new .NET class libraries like LINQ and Silverlight.

You’ll get answers to these questions:

• What’s new in C# 3.0?
• How can transactions improve reliability?
• How do I debug difficult problems like exceptions, deadlocks, and memory leaks?
• How do I develop network services with Windows Communication Foundation?
• How do I develop cutting-edge UIs with Windows Presentation Foundation and Silverlight?

Course Highlights

• Use C# 3.0 idioms like iterator methods, the lambda operator, and extension methods
• Write code that works well with the garbage collector
• Handle and throw exceptions properly
• Work with Windows Presentation Foundation (WPF) and XAML
• Write browser applications using Silverlight
• Manage long-running processes with Windows Workflow Foundation (WF)
• Threading and Concurrency
• Scale your application to multicore machines using threads
• Debug difficult problems using WinDBG, SOS, and ADPLUS
• Understand how the .NET class libraries and the C# language are evolving in .NET 3.5



Want to get a detailed course outline?
CLICK HERE
and put "Guerrilla .NET" in the subject line.

 

Upcoming Courses

To get a detailed course outline for any of the courses listed below, please send an email to requests@develop.com and put the course name in the subject line.

NEW- Updated With .NET 3.0 Content -
Essential .NET
register online or call 800.699.1932
April 21st-25th in Atlanta
May 5th-9th in Los Angeles

What you’ll learn…
Essential .NET is the best introduction to the .NET platform available today. Experienced C++, VB, or Java developers will find a rapid and enjoyable combination of .NET design philosophy and practical advice. You learn a myriad of patterns and best practices. You get hands-on experience with the major .NET 2.0 and .NET 3.0 class libraries, including Windows Forms, Windows Communication Foundation (WCF), Windows Presentation Foundation (WPF), and ASP.NET.

NEW-Code Smarter with Design Patterns in .NET register online or call 800.699.1932
April 8th-11th in Los Angeles

What you’ll learn…
Reuse solutions, not just code. Code Smarter with Design Patterns in .NET helps you identify problems that occur repeatedly in your code, and solve those problems in a standardized way. Wrap your head around the concept of the design pattern—a programming solution or template that can be used in many different situations—and discover new tools for creating successful software.


NEW- Updated With .NET 3.5 Content -
Essential Windows Communication Foundation
register online or call 800.699.1932
April 15th-18th in Los Angeles
May 5th-8th in Bellevue, WA

WCF has been updated to reflect the changes and new features in .NET 3.5 including the new bindings and the Web Programming Model

Also:
- A new module concentrating solely on the design of service, data, message and fault contracts.
- A new module exploring the pure message based programming model including useful techniques like writing a SOAP router, more flexible version via custom dispatching and commonly used XML programming techniques like XmlReaders, XPath and Linq to XML.


What you’ll learn…
Learn to build secure and reliable WCF applications. Upon completion you will know how to define and access services using channels, bindings, messages, data contracts, operation contracts, and faults and know the built-in functionality of WCF, including instance management, concurrency, reliability, queues, transactions and security.

Essential ASP.NET register online or call 800.699.1932
April 7th-11th in Los Angeles