I’ve had multiple meetings recently with customers and press where the topic of .NET development has come up, particularly as it relates to the cloud and server.  They’ve heard about the extensive work we’ve done with Visual Studio 11 to enable the client-side development of Metro style apps for Windows 8 using C#, Visual Basic, C++, and JavaScript, and they’re curious to learn what improvements have been made for server-side development using .NET

From my perspective, .NET is already the richest and most productive way for developers to create server-side applications that run in the cloud and on premises, and to do so with symmetry across both.  With .NET 4 available today on Windows Server and in Windows Azure, developers have in place the languages, libraries, frameworks, and capabilities necessary to create next-generation solutions, whether for the enterprise or for a consumer application.

And things only get better with .NET 4.5. The coming release of .NET is targeted to provide great capabilities for developers working on mobile apps, web apps, and cloud services, while at the same time enabling rapid scalability, fast time to market, and support that spans a gamut of PCs, browsers, and mobile devices. Work in .NET 4.5 has been done at all levels of the stack, from the internals of the Common Language Runtime (CLR), to the Base Class Libraries (BCL), to Entity Framework (EF), to Windows Communication Foundation (WCF), to Windows Workflow Foundation (WF), to ASP.NET. 

Given the aforementioned questions I’ve received about server-side development with .NET 4.5, I thought I’d share some highlights for what’s coming in this very exciting release.

Web

For a broad view of what’s new for web development, I recommend reading What’s New for ASP.NET 4.5 and Web Development in Visual Studio 11. For the purposes of this post, I’ll focus on a few highlights.

In our modern world of connected devices and continuous services, a very important aspect of a web site is being able to provide great results for mobile devices.  ASP.NET in .NET 4.5 enables just that, with mobile support built into ASP.NET MVC 4 that enables optimized experiences to be delivered for tablets and phones.  ASP.NET Web Pages also includes support for Mobile Display Modes, which enable you to create device-specific pages that are rendered conditionally based on the device making the request.

ASP.NET Web Forms has also seen a lot of love in .NET 4.5.  Web Forms now supports model binding, which allows data controls to directly bind query parameters to form fields, query strings, cookies, session state, and view state.  These data controls can also use strongly-typed binding expressions, rather than using the customary dynamic binding methods where type information is only available at run time.  Additional support for HTML5 is included as well, such as support for new HTML5 form type elements like ‘email’, ‘tel’, ‘url’, and ‘search’.

ASP.NET in .NET 4.5 now has support for WebSockets, a protocol that provides bidirectional communication over ports 80 and 443 but still with performance similar to that of TCP, enabling a good alternative to long-polling.  It has much improved support for writing asynchronous code.  It has support for bundling and minification of JavaScript, which results in less data to be served to clients and faster load times for applications.  And it has inherent performance improvements, such as a ~30% reduction in base level memory consumption for an ASP.NET application.

Of course, the improvements go beyond just programming model capabilities and performance.  The code editor in Visual Studio has been enhanced with new ASP.NET Smart Tasks, with IntelliSense improvements, and with refactoring support.

As I’ve discussed previously on this blog, JavaScript support in Visual Studio 11 has also been enhanced significantly, and the code editor in Visual Studio 11 has some very handy HTML5 and CSS3 code editor improvements.  The new Page Inspector tool in Visual Studio 11 also enables developers to better connect the browser on the client with code on the server, letting developers easily map HTML elements to the source that resulted in their rendering.

Data

The ability to query for data is an integral aspect of many web applications and services, and .NET 4.5 delivers some significant updates in the data space, particularly around the Entity Framework.

Entity Framework 5 (included in .NET 4.5) now supports multiple development workflows, catering to whichever methodology the developer prefers.  Some developers prefer to first create their database and then consume that into their application; this is enabled with the “database first” approach.  Others prefer a more “model first” approach, where a designer or an XML editor can be used to define entities in an application domain, highlighting how these relate to each other and to concrete representations which can then be used to generate a database.  Still other developers prefer a more “code first” approach, where they define their model objects using “plain old classes,” never having to open a designer or define a mapping in XML.  With the .NET Framework 4.5, developers don’t have to choose, as all three approaches are supported (with “code first”, Entity Framework now also supports migrations, enabling developers to easily modify and version their database schema without dropping any data).

Entity Framework in .NET 4.5 includes a variety of new features focused on making the developer more productive.  For example, .NET enum types can now be mapped to the underlying Entity Data Model (EDM). Table-valued functions (TVF) are now supported, making it possible to use LINQ queries and Entity SQL against them.  Spatial data is now supported, with two new primitive EDM types called Geometry and Geography, and with their associated .NET types DbGeometry and DbGeography.

The Entity Framework has also seen some significant performance improvements in .NET 4.5.  For example, it now supports automatically compiled LINQ queries.  Queries written with the Entity Framework are manifested as expression trees which need to be translated at run time into SQL queries.  That transformation can take some measurable time, in particular for very complex queries.  .NET 4.5 supports the automatic caching of query compilations, alleviating the need for the developer to do so manually to achieve the same performance benefits.  The impact of these performance improvements when combined with others in the system have resulted in throughput improvements on some queries by as much as 600%.

For more information on what’s new in Entity Framework in .NET 4.5, the Data Developer Center on MSDN is a good place to start.

Services

One of the significant new features to surface in .NET 4.5 is the new Web API framework.  ASP.NET Web API enables developers to build modern HTTP services that can be consumed by a broad range of clients, including browsers and mobile devices.  It supports content negotiation, such that clients and servers can work together to determine the right format for data to be returned.  It supports automatic serialization of data to JSON, XML, and Form URL-encoded data.  It enables easily exposing REST APIs that map incoming requests to business logic using built-in routing and validation support.  It supports query composition, such that the framework can automatically layer on top of IQueryable<T>-returning APIs support for paging and sorting and other capabilities.  It supports testing with mocking frameworks, and much more.

It is also highly productive.  The ASP.NET Web API tutorial has a good example of how easy it is to build a simple API; this API just returns a list of available products, which for the purposes of the example has been hardcoded:

public IEnumerable<Product> GetAllProducts()
{
    return new List<Product>
    {
        new Product() { Id=1, Name="Gizmo 1", Price=1.99M },
        new Product() { Id=2, Name="Gizmo 2", Price=2.99M },
        new Product() { Id=3, Name="Gizmo 3", Price=3.99M }
    };
}

This API can then easily be consumed from any client, such as one using JavaScript in a browser.  For more information on ASP.NET Web API, I recommend the ASP.NET Web API site.

Windows Communication Foundation (WCF) has also been significantly enhanced for .NET 4.5, making it much easier to develop robust web services.  As with ASP.NET, one of the major WCF features in .NET 4.5 is support for WebSockets, exposed via the NetHttpBinding and NetHttpsBinding bindings.  UDP support has also been added to WCF in .NET 4.5.

WCF has also been augmented with productivity features.  WCF in .NET 4.5 supports contract-first development, such that you can generate service and data contracts from a WSDL document and then provide the backing implementation.  The configuration files used by WCF have been greatly simplified, such that the defaults used are much more likely to be appropriate for production use and thus don’t need to be specified.

The WCF support in .NET 4.5 also ties in with the greatly overhauled asynchronous story in .NET 4.5, which I’ve previously written about and which I’ll mention again later in this post.  WCF service methods can now be written to return Tasks, and can thus be implemented using the new async and await keywords in C# and Visual Basic.  WCF also now has support for true asynchronous streaming, which can lead to important scalability benefits.

The What’s New in Windows Communication Foundation 4.5 page provides a good overview on the breadth of what’s new.

Workflow

Windows Workflow Foundation (WF) is a key technology for implementing long-running processes, the kind that often show up in server and cloud workloads.  It includes a programming model, an in-process workflow engine, and a designer, all of which have been improved for .NET 4.5.

Several of the more prominent improvements for WF are visible in this simple screenshot I took after experimenting with some of the features:

This image highlights the state machine workflows that are now possible to create using .NET 4.5.  Further, it shows that C# expressions are now supported in the workflow designer (this is in addition to the support for Visual Basic expressions that were already supported in the previous release).  The designer showcased here has also been improved significantly, with support for new features like search, click-and-drag support to pan around the workflow, an outline view to make it easier to navigate hierarchical workflows, support for multi-select, and more.

These are just some of the improvements available for WF in .NET 4.5.  Others include being able to run different versions of the same workflow side-by-side, better control of how child activities persist in the workflow, and improved debugging support and build-time validation.

For more on what’s new in Windows Workflow in .NET 4.5, I recommend the What's New in Windows Workflow Foundation in .NET 4.5 overview material in the MSDN documentation.

Identity

Windows Identity Foundation (WIF) is a set of classes that support implementing claims-based identity, something particularly relevant to web applications and services.  Prior to the .NET Framework 4.5, WIF shipped as a standalone entity, but as of .NET 4.5 its types are integrated into the heart of the Framework.  In fact, some types at the center of .NET’s security model now inherit from WIF types; for example, GenericPrincipal (which has been in .NET since v1) now derives from the WIF type ClaimsIdentity.  This deep integration makes it much easier to use claims.

Other improvements are also available in WIF as part of .NET 4.5.  Session management has been improved for web farms, such that custom code is no longer required.  Developers can simulate any incoming claim types and values at development time using Visual Studio 11 support for WIF.  You can also use Visual Studio support for Windows Azure Access Control Service (ACS) to easily enable application login using Active Directory, Facebook, Windows Live ID, Google, Yahoo!, or any other OpenID provider.

For more information on WIF, the Windows Identity Foundation 4.5 Overview is a good place to get started.

Scalability

The fewer resources on average a server system uses to process a request, the more requests the system can process concurrently, and the more you can do with less.  One key way server and cloud systems enable this kind of scalability is via asynchrony, such that thread-related resources are only consumed when they’re needed for executing code; while waiting for I/O to complete that will yield the data necessary for a computation to continue, asynchronous solutions allow the system’s threading resources to be used for other means.

As has already been alluded to in discussions of ASP.NET, WCF, and WF, up and down the stack improvements have been made for .NET 4.5 around asynchrony.  Both C# and Visual Basic have been extended with new “async” and “await” keywords that allow developers to write asynchronous code with the same control flow constructs they use in their synchronous code, avoiding the need for complicated, explicit callback mechanisms (F# already had a similar capability in Visual Studio 2010).  Then throughout the Framework, types have been augmented with Task-returning methods that make functionality immediately consumable using the new keywords, and hooks have been added that allow developers to write Task-returning methods and have them hosted by the system in an asynchronous manner.  These improvements make it possible for developers to build asynchronous systems much more productively.

Consider an asynchronous action method written with ASP.NET MVC.  In this method, I want to first download the contents of http://bing.com, and then once that’s completed, I want to download the contents of http://msdn.com.  I also want to report any errors that occur.  Doing this prior to .NET 4.5 would have resulted in a page’s worth of code, with nested callbacks, complicated error handling, and manual management for tracking outstanding operations.  With .NET 4.5, I can instead simply write a straightforward action method like the following:

public async Task<ActionResult> IndexAsync() {
    var client = new HttpClient();
    try {
        ViewBag.BingContent = await client.GetStringAsync("http://bing.com/");
        ViewBag.MsdnContent = await client.GetStringAsync("http://msdn.com/");
    }
    catch(Exception exc) {
        ViewBag.Error = exc.Message;
    }
    return View();
}

This example uses some of the new async support in .NET 4.5, including the new keywords, new support in ASP.NET MVC that supports writing such methods, and the new HttpClient class which exposes a wealth of asynchronous methods.

As mentioned in my previous post on Visual Studio 11 Programming Language Advances, this support for asynchrony expands further into Visual Studio 11’s tooling.  For example, the Visual Studio 11 debugger has first-class knowledge of async methods (allowing you to step through the method as if it were synchronous), the code editor provides tooltips that show how to consume async methods using the new language keywords, the “Add Service Reference…” dialog automatically generates Task-based endpoints that can be consumed in async methods, the Concurrency Visualizer can highlight suspension points in async methods, and the MSTest unit test framework in Visual Studio supports async unit tests.

You can learn more about this async support on the Visual Studio Async page on MSDN.

Runtime Performance

Improvements for .NET 4.5 extend all the way down into the core of the engine, into the CLR itself, with multiple improvements made specifically with server scenarios in mind.

One key area of investment has been in garbage collection.  In .NET 4, we introduced “background GC” for the workstation flavor of the garbage collector.  This background mode, which is enabled by default, allows for Generation 2 collections to be performed concurrently with collections of Generation 0 and Generation 1.  What this means to developers is that application pause time is reduced significantly, which for client applications with rich UI can be a big deal, minimizing the number of hiccups that occur in the application’s responsiveness.  This need for reduced pause time is also relevant to servers, however, as servers need to remain responsive to incoming requests and often provide consistency on the latency with which requests are processed.  As such, .NET 4.5 sees the introduction of background GC for the server flavor of the garbage collector, as well.  As with the workstation GC, the server GC has this new capability enabled by default, so you don’t need to do anything special in your applications to reap the benefits.

The GC changes were made in recognition of the importance of memory management for various kinds of server workloads.  Another such change has to do with array sizes.  In .NET 4 and earlier, no object could be larger than 2GB in size, even in 64-bit processes.  For certain workloads that use large arrays, however, this size limit can be constraining.  As such, .NET 4.5 supports lifting the limit for arrays in 64-bit processes, such that arrays may be larger than 2GB.  This means, for example, that you could allocate an array of Int32.MaxValue Int32s, even though such an array will consume 8GB.  Large objects in .NET are allocated on a special heap, known not coincidentally as the Large Object Heap (LOH);  .NET 4.5 has also seen significant performance improvements to the LOH, including usage of better algorithms for managing free memory in the heap.

Another performance-focused improvement in the runtime is the new multi-core JIT compilation support.  In .NET 4 and earlier, just-in-time (JIT) compilation results in methods being compiled as they’re needed.  With .NET 4.5 and the new multi-core JIT capability, applications can make a few calls to methods on the ProfileOptimization class so as to highlight regions of the app where improved JIT compilation times are important.  The system will track what methods are used in that region, and on subsequent runs, those methods may be compiled on additional threads concurrent with execution of the program.  ASP.NET itself makes use of these APIs in .NET 4.5, so server apps written with ASP.NET benefit automatically.  This can be particularly beneficial for web app startup time, where some apps should see as much as a 30% improvement.

You can learn more about such performance improvements in the April 2012 issue of MSDN Magazine.

Base Class Libraries

The support for asynchrony through the .NET 4.5 libraries is quite extensive, with hundreds of new async methods exposed, and with quite a few performance improvements made for existing types in the Framework.  These new async methods span mscorlib.dll, System.Xml.dll, System.Data.dll, System.Web.dll, System.Net.Http.dll, and beyond.

Outside of asynchrony, though, there are a plethora of additions made to the core libraries that will positively impact server and cloud scenarios.  For example, .NET 4.5 includes the new ZipArchive and ZipFile classes for manipulating .zip files, as well as improvements to the existing compression support in the framework to improve compression ratios and speeds.

ZipFile.CreateFromDirectory(@"C:\MyBlog", @"Somasegar.zip");

The Managed Extensibility Framework (MEF) now includes support for generic types, support for multiple scopes, and a convention-based programming model.  The new System.Net.Http namespace (which contains the previously mentioned HttpClient type) provides components that allow for consumption of modern web services over HTTP, components that can be used in both clients and servers.  And much more.

The .NET team blog is a good place to learn more about what’s new in the BCL.

Conclusion

I’ve said for a long time now that .NET is a great environment in which to develop server and cloud applications, spanning from the private cloud (datacenter) to the public cloud, and I see this role getting even stronger with .NET 4.5.

.NET 4.5 is a modern enterprise framework. ASP.NET enables developers to build highly interactive server applications that use modern standards like HTML5 and CSS3.  Such applications are often built in terms of APIs and services that are also exposed to other consumers; ASP.NET Web API and WCF both provide solid frameworks with which to build these.  These applications and services also often need to support credentials, and the integration of Windows Identity Foundation into .NET 4.5 gives developers the tools they need to be successful with authentication and authorization.  ADO.NET and the Entity Framework enable developers to easily incorporate data into their server applications, with multiple work styles enabled, and with clean integration of validation and business rules.  With Windows Workflow Foundation, developers can create powerful workflow services for authoring distributed and asynchronous applications, and with WCF, these systems can communicate and integrate with any number of others enterprise solutions, from SAP to Oracle to SharePoint to Dynamics. Scalability is achieved with an asynchronous solution that spans the .NET runtime, libraries, and languages, and all of this runs on top of the highly robust, reliable, and efficient engine that is the CLR.

As you can see, there are a lot of new enhancements coming in .NET 4.5 that makes .NET a compelling programming environment for building applications for the cloud and server environments.

Namaste!