VS2010: Inside .NET 4.0
By Tim Anderson
Visual Studio 2010 was released at the same time as a new version of the .NET Framework.
HardCopy Issue: 52 | Found In: Visual Studio 2010 | Published: 19/05/2011 | Last Revision: 19/05/2011
The release of Visual Studio 2010 also saw the release of version 4.0 of the .NET Framework. This is a major update. Versions 3.0 and 3.5 extended rather than replaced version 2.0 and continued to use the 2.0 runtime, whereas 4.0 introduced a new runtime.
Visual Studio 2010 can also target older versions of .NET if required, and in general Microsoft made a big effort to preserve compatibility. One impressive new feature is that multiple versions of the .NET Framework can run side-by-side in the same process, for example when managed code calls a COM component that is itself written in managed code, but with an older version of .NET. In the past this scenario might fail, but now both assemblies will use the version with which they were built.
Note that some features of .NET 4.0, such as changes in ASP.NET and in specific languages, are covered elsewhere.
Parallel Programming
It is widely accepted that multiple CPU cores rather than faster CPUs are the way forward for improving software performance. The implication for software developers is that concurrent programming is vital when performance is critical. The .NET Framework has always supported multi-threading, but coding threads is complex and can lead to subtle and unpredictable errors. These risks have not been eliminated, but new features in .NET 4.0 do reduce the burden on developers who no longer have to deal directly with threads.
The heart of the new Parallel extensions is the Task Parallel Library (TPL). As its name implies, the Task Parallel Library deals with tasks rather than threads. It has its own scheduler, and will use one or many threads to perform a task depending on how many processors are available. The new Parallel class has methods Parallel.For, Parallel.ForEach and Parallel.Invoke which use the TPL. Instead of looping consecutively over the assigned tasks, they are performed in parallel to the extent that is appropriate for the machine:
Parallel.Invoke( () => {Task1();},
() => {Task2();}
);
In this example, two tasks are passed to Parallel.Invoke using lambda expressions. The time taken will normally be little more than that of the single slowest task, on a machine with two or more cores.
The TPL is used by other .NET libraries to improve performance. The most prominent example is Parallel LINQ (PLINQ), a parallel implementation of LINQ to Objects. This lets you use Language Integrated Query statements against any enumerable object and have it execute in parallel.
Windows Presentation Foundation 4.0 has richer graphics, among numerous other changes.
Parallel programming is also supported by new collection classes and primitive types that handle synchronisation without the need to deal directly with locking code. Examples include BlockingCollection(T) and System.Threading.Barrier.
The Visual Studio 2010 debugger lets you inspect Parallel Stacks and Parallel Tasks with new tools, while the profiling tools let you visualise multi-threaded performance.
Overall, the concurrent programming support in .NET Framework 4.0 and Visual Studio 2010 is among the most significant new features. The main caveat is that developers still need to be aware of the kinds of bugs that concurrency can introduce, and incorporate that awareness into the test and development process.
Security
When .NET was launched ten years ago, it introduced a new approach to application permissions called Code Access Security, in which the execution rights for .NET assemblies were controlled by policies set through the .NET Configuration Tool. Policies could be applied based on conditions, and execution was permitted at various levels from full trust to completely disallowed.
Microsoft has moved away from Code Access Security to the extent that some of its features are no longer supported, and it is now turned off by default. This is because Code Access Security was too complex, many of its settings were ineffective, and the fact that it only applied to .NET code limited its value.
Its replacement for administrators is Windows Software Restriction Policies, which apply to both .NET and native code applications, and which is simpler to manage. Within .NET itself, there is also the concept of separating security-critical code, that can potentially do damage, from security- transparent code that is safe. This model is called Transparency and it is used in sandboxed environments. Code can be marked as transparent via attributes. This model was actually introduced in .NET 2.0 mainly for internal use, but now in .NET 4.0 has been enhanced and made more widely available.
Coding for Windows
There are two frameworks for Windows desktop applications in .NET. Windows Forms is the original, and remains supported though development has slowed. Windows Presentation Foundation (WPF) is the newer framework, based on XAML mark-up and compatible as far as possible with Silverlight.
WPF 4.0 is the version that makes it mature and fully usable. One factor is that Visual Studio 2010 is itself built with WPF, and the interaction between the two teams resulted in substantial improvements in areas including native code Interop and text handling. Graphic effects are faster and more powerful. Cached composition lets you cache content in video memory, and there are new Pixel Shader and Animation Easing features. Layout rounding overcomes rendering artefacts on object edges.
Another key element is Windows 7 support. The launch of Windows 7 has reinvigorated the upgrade cycle, and users of these machines will expect applications that take advantage of its new features. Microsoft created these APIs with native code and COM, but there are two routes to incorporating them into .NET applications.
One is the Windows API Code Pack which works with .NET 3.5 or higher and adds support for the Taskbar, Windows 7 libraries and common file dialogs, task dialogs, the sensor platform, and application restart and recovery. But if you use WPF 4.0 you might not need the code pack. Support for the Windows 7 taskbar is built-in, including adding items to Jump Lists, progress bars within taskbar icons, and adding controls to thumbnail previews. There is also support for Windows 7 multi-touch, with events like TouchDown and TouchMove, and Manipulation classes that let you respond to gestures.
Developers of business applications will find three new controls which match their counterparts in Silverlight. These are the DataGrid, the Calendar and the DatePicker.
Windows developers should think twice before starting a new Windows Forms application. There are still reasons to do so, but WPF is much better at layout and scaling. Another factor is Silverlight, which is essentially cut-down WPF and shares the same design tool in Expression Blend. If there is a possibility of a transition to Silverlight, then starting with WPF rather than Windows Forms makes sense. Although Microsoft is backing away from Silverlight as a cross-platform runtime, it is central to its plans for Windows Phone and may well figure largely in the next version of Windows.
Dynamic language support
Included in .NET Framework 4.0 is the Dynamic Language Runtime (DLR), and dynamic features have been added to .NET languages including Visual Basic and C#. The DLR supports dynamic languages such as Python and Ruby, implemented for .NET as IronPython and IronRuby, and enables interoperability between dynamic and static languages.
Dynamic typing is also useful for COM Interop, where types may not be known until runtime. It simplifies working with COM automation in Microsoft Office, for example.
The DLR is supported by a new System.Dynamic namespace. This includes two classes, DynamicObject and ExpandoObject. DynamicObject is a base class for creating your own dynamic classes, which can include classes that have both static and dynamic members. ExpandoObject is a class whose members can be added and removed at runtime. It can also be passed to other dynamic languages:
dynamic myobj = new ExpandoObject();
myobj.forename = “William”;
myobj.lastname = “Smith”;
myobj.id = 34;
The addition of dynamic features to the core .NET Framework is important not only for interoperability, but also because it defuses the argument about the relative merits of static versus dynamic typing; developers can use whichever approach they prefer or which is best suited to the task.
Workflow and Communication
Microsoft is placing a big emphasis on Windows Workflow (WF) in .NET 4.0, hoping to move it more into the mainstream of .NET development. In addition, WF is becoming tightly integrated with Windows Communication Foundation (WCF), because of the obvious synergy between workflow and messaging.
WF is essentially a runtime that knows how to process activities, where activities are sequences that you define either in code or in the WF Activity Designer. The runtime is able to persist its state, so that even long-running workflows are handled correctly. Workflow applications can be deployed in the IIS Web server, as Windows services, or in SharePoint, or hosted by custom applications.
Workflow Foundation has new visual designers in Visual Studio 2010.
There are numerous changes to WF in this release, starting with a brand new designer. You can also host the WF designer in your own applications, enabling users to customise workflows. Performance is another focus. The activity library has been largely rewritten. There is still a Sequence workflow, but a Flowchart workflow replaces the old StateMachineWorkflow.
New built-in flow control activities include DoWhile, Pick and ParallelForEach. A number of built-in activities support WCF messaging, and message activities can be gathered into transactional scopes. There are also more options when hosting a WF application, the most significant being Windows Server AppFabric (codenamed ‘Dublin’). This provides dedicated services for hosting both WF and WCF applications, including database and persistence support, monitoring, messaging and the ability to manage your application with either the IIS manager or PowerShell. There are also Visual Studio templates to enable easy packaging of applications for deployment in AppFabric.
Aside from WF integration, there are not so many changes in WCF, although it does get support for WS-Discovery for dynamically discovering service addresses. There is also simplified configuration, new framework support for routing services, and better support for REST (URL-based) services. WCF Web APIs, a Microsoft-supported open source project currently in beta, remakes the WCF stack to fit naturally with HTTP and JavaScript or Silverlight clients.
A common question is how the combination of WF, WCF and AppFabric compares to Microsoft’s high-end application integration server BizTalk. The normal response is that AppFabric applications are intended primarily for Microsoft platforms, whereas BizTalk handles interaction between Microsoft and non-Microsoft platforms. Nevertheless, there is some overlap and AppFabric is likely to handle some tasks that were previously in BizTalk’s realm with greater ease of development and lower cost deployment.
According to consultant David Chappell, “Microsoft is making the workflow way more attractive for a broader range of scenarios.... Windows Workflow is on its way to centre-stage.”