Entries in .NET Framework (4)

Tuesday
Sep282010

Udi Dahan in Calgary

Calgary developers! Mark your calendars and call the babysitter because tomorrow night, Udi Dahanwill be delivering a presentation at the Calgary .NET User Group. Now, if you don’t know Udi, here’s the bio:

Udi Dahan is The Software Simplist, an internationally renowned expert on software architecture and design. A solutions architecture and connected systems MVP, 5 years in a row, Mr. Dahan is also one of 33 experts in Europe recognized by the International .NET Association, an author and trainer for the International Association of Software Architects, and an SOA, Web Services, and XML Guru recommended by Dr. Dobb’s magazine. When not consulting, speaking, or training, Udi leads the development ofNServiceBus, the most popular open-source service bus for .NET.

To describe Udi and his expertise, imagine gathering the top minds in the .NET community, shooting a “Bo Knows” commercial on his many talents. In short, he’s a pretty smart cookie. With that stated, tomorrow night, Udi will deliver an interesting presentation, covering command/query responsibility segregation, something he’s written about for while:

Command/Query Responsibility Segregation 
More and more developers are starting to use messaging patterns and domain models in their N-Tier architectures. Many are surprised by the added complexity and beginning to wonder - was it worth it? Join Udi as he describes the missing pattern which brings simplicity back to distributed systems architecture.

Why not drop by and join us? The venue is super-simple to find with lots of nearby parking:

Nexen Conference Centre, +15 Level (Bing Maps
801-7th Avenue SW, Calgary

And don’t forget to register! This helps out the event organizers a lot. So please take two minutes and sign up.

Registration is at 5 PM and Udi’s presentation kicks off at 5:30 PM. You’ll probably want to show up early since the room will likely be packed. Afterward, we’ll head to Ceili's Irish Pub & Restaurant for drinks with the man himself.

Saturday
Sep252010

Windows API Code Pack v1.1

Many years ago, as a .NET developer, I would often find myself frustrated whenever I wanted to target a particular aspect of Windows, only to discover that no corresponding API in managed code existed. (Yes, I leaned somewhat towards being one of the “.NET purists” that Scott Hanselman warned you about.) In cases like these, and after exhausting all possibilities to find a library in the .NET Framework that met my requirements, I would bite the bullet and resort toPlatform Invocation Services (P/Invoke) or COM Interop. Now, to be clear: Both of these technologies work very well. However, I struggled writing P/Invoke wrappers or failed to understand all the subtle nuances of COM. In short, I found P/Invoke and COM Interop a challenge to get exactly right, resulting in my application crashing in a blaze of glory at runtime.

Over the years, things got a little better as developers in the .NET community began to publish interop libraries. Then, in 2004, Adam Nathan announced the PINVOKE.NET wiki and life – as we knew it – got a whole lot better. Now, despite the goodness that this site brought to the .NET community, it would still take time for signatures to be updated or published. In some cases, the listing would be incomplete. In essence, for developers writing managed code, wanting to target specific aspects of Windows, a certain level of heavy-lifting was sometimes required.

Enter the Windows API Code Pack.

The Windows API Code Pack is designed to help managed code developers target key features of Windows without forced you to resort to P/Invoke or COM Interop. Moreover, it abstracts all of the heavy-lifting away from you, leaving you with a clean API for you to target. From the project’s description:

The Windows API Code Pack provides a source code library that can be used to access some features of Windows 7 and Windows Vista from managed code. These Windows features are not available to developers today in the .NET Framework.

Yochay describes it as-follows (with emphasis added by me):

You may think of the Windows API Code Pack as the closest thing to an “official” managed API for Windows on top of the .NET Framework. The Windows API Code Pack is a free, managed source code library provided by Microsoft as-is. You should consider this library as if you wrote that code. It is a great starting point and provides a really solid solution for managed code developers who create Windows application and looking to light up their applications. It covers a lot of the new Windows 7 features as well as some more fundamental core features from the Windows Vista timeframe.

I’d rank the Windows API Code Pack as a must have library for developers targeting Windows Vista or Windows 7 with managed code. In fact, it’s so awesome that it makes you feel like you’ve discovered the Konami Code for unlocking key features of Windows. (My apologies to younger readers who never had the pleasure of playing the supremely-challenging game, Contra on the NES.) The thing I love most about the Windows API Code Pack is the abstraction layer it provides. It hides the gnarly, low-level plumbing (e.g. P/Invoke or COM Interop integration with Windows) and instead, allows you to focus on integrating features that you care about (e.g. sensors in Windows 7) and in a manner that feels more intuitive to managed code developers (e.g. classes/methods/properties).

So, what exactly does the Windows API Code Pack support in terms of Windows features? In a word: Lots.

  • Windows 7 Taskbar
    • Jump Lists, Icon Overlay, Progress Bar, Tabbed Thumbnails, and Thumbnail Toolbars
  • Windows Shell
    • Windows 7 Libraries
    • Windows Shell Search API support
    • Explorer Browser Control
    • A hierarchy of Shell Namespace entities
    • Windows Shell property system
    • Drag and Drop for Shell Objects
    • Windows Vista and Windows 7 Common File Dialogs, including custom controls
    • Known Folders and non-file system containers
    • Shell Object Watcher
    • Shell Extensions API support
  • DirectX
    • Direct3D 11.0, Direct3D 10.1/10.0, DXGI 1.0/1.1, Direct2D 1.0, DirectWrite, Windows Imaging Component (WIC) APIs
  • Windows Vista and Windows 7 Task Dialogs
  • Sensor Platform APIs
  • Extended Linguistic Services APIs
  • Power Management APIs
  • Application Restart and Recovery APIs
  • Network List Manager APIs
  • Command Link control and System defined Shell icons

That’s a lot!

Recently, the team recently released the next version of the Windows API Code Pack. So, what are the major changes in v1.1? From the release notes:

  • Code clean-up
    • Addressed many FxCop violations and PREfast warnings
    • Various spot-fixes for improved stability
    • Added String localization preparation
  • Bug Fixes within the Code Pack and Samples
  • New Features
    • PropVariant (Re-designed)
    • Thumbnail Handlers
    • Preview Handlers
    • ShellObjectWatcher
  • New Demos and Sample Applications
  • Visual Studio 2010 Compliance
  • xUnit test coverage
  • Signed assemblies

Interested in seeing the Windows API Code Pack in-person? Register for TechDays! We have a session dedicated to the Windows API Code Pack occurring in the Developing for Three Screens and the Cloud track:

DEV336 - The Windows API Code Pack: Add Features of Windows 7 to Your Application 
Day 1 - 3:40pm - 4:45pm

Accessing new features of Windows 7 is a challenge from managed (.NET) code. The level of interoperability required is out of reach for many developers. The Windows API Code Pack for the Microsoft .NET Framework is a sample library you can use in your own projects today that provides access to new user interface features (taskbar jumplists, libraries, sensor platform, and more) as well as "behind the scenes" features that make your applications more aware and responsive (restart and recovery, power management, and more.) Discover a shortcut to development of Windows 7-based applications for programmers of Microsoft Visual Basic and Visual C# and get started today.

This session was prepared by the ultra-fantastic Kate Gregory and will be presented in all eight cities hosting TechDays!

If you’re building applications in managed code and you’re looking to target particular features of Windows then download the Windows API Code Pack and building your Next Big Thing today! Also, please send us your feedback or file a bug using the Discussions or Issue Tracker for the Windows API Code Pack!

Friday
Nov272009

My Interview with Dave Bost on The Thirsty Developer Podcast

Recently, I had the pleasure of chatting a fellow Developer Evangelist from Microsoft, Dave Bost to discuss some of the latest and greatest features of Visual Studio 2010 and the .NET Framework 4. You can check out a recording of this interview here.

I continually find new and exciting things in both Visual Studio 2010 and the .NET Framework 4. Combined with ASP.NET MVC 2 and Silverlight 4, there is a lot of great technology out in the community for developers to hand their hands on.

Tuesday
May242005

Ode to the SchemaImporterExtension Class

Back in May 2004, Daniel Cazzulino - XML wonk of the .NET blogosphere - published an article on MSDN entitled, "Code Generation in the .NET Framework Using XML Schema". In his article, he described how one could extend xsd.exe to support dynamic code generation when processing XML Schema. The purpose was to support the scenario where a developer could customize the code generated for schema type structures. In my opinion, this solution provided two benefits: (1) it provided control over the code generation process and, in turn, (2) resolved the "loose" type map inferred by the .NET Framework. Personally speaking, I can recall numerous occasions when the .NET Framework would infer type structure element as xsd:string, rather than its actual representation. System.Guid anyone? Not ideal, to say the least.

Fast forward to May 2005. In Visual Studio 2005, the name of the game is extensibility - this includes the tools that ship along side the SDK. In particular, the XML Schema Definition Tool (xsd.exe) has been updated to support schema importer extensions, a new feature of the .NET Framework. Let's take a closer look.

In the .NET Framework 2.0, a new namespace has been added; System.Xml.Serialization.Advanced. The purpose of this namespace is to contain types that support advanced XML serialization and schema generation scenarios. Currently, this namespace contains the following types: 

  • SchemaImporterExtension
  • SchemaImporterExtensionCollection 

The SchemaImporterExtension class allows developers to hook into the schema generation process of xsd.exe. By implementing a class that derives from this type, you can control the code generated for types found in schema.

Creating a schema importer extension is pretty easy. You simply derive from SchemaImporterExtension and override the appropriate methods. In most cases, you'll override the ImportSchemaType methods, which perform most of the heavy lifting. Here's an example:

// PersonSchemaImporterExtension.cs
 
using System;
using System.CodeDom;
using System.CodeDom.Compiler;
using System.Collections;
using System.Xml;
using System.Xml.Schema;
using System.Xml.Serialization;
using System.Xml.Serialization.Advanced;
 
namespace Bristowe.Examples.Xml.Serialization
{
 public sealed class PersonSchemaImporterExtension : SchemaImporterExtension
 {
  public override string ImportSchemaType(string name, string ns,
   XmlSchemaObject context, XmlSchemas schemas, XmlSchemaImporter importer,
   CodeCompileUnit compileUnit, CodeNamespace mainNamespace,
   CodeGenerationOptions options, CodeDomProvider codeProvider)
  {
   if (name.Equals("Person") && ns.Equals("http://bristowe.com/"))
   {
    CodeTypeDeclaration speaker = new CodeTypeDeclaration("Person");
    mainNamespace.Types.Add(speaker);
 
    CodeMemberField firstNameField = new CodeMemberField(
     new CodeTypeReference(typeof(string)), "_firstName");
    speaker.Members.Add(firstNameField);
 
    CodeMemberProperty firstNameProperty = new CodeMemberProperty();
    firstNameProperty.Attributes = MemberAttributes.Public | MemberAttributes.Final;
    firstNameProperty.GetStatements.Add(
     new CodeMethodReturnStatement(
      new CodeFieldReferenceExpression(
       new CodeThisReferenceExpression(), "_firstName")));
    firstNameProperty.Name = "FirstName";
    firstNameProperty.SetStatements.Add(
     new CodeAssignStatement(
      new CodeFieldReferenceExpression(
       new CodeThisReferenceExpression(), "_firstName"),
       new CodePropertySetValueReferenceExpression()));
    firstNameProperty.Type = new CodeTypeReference(typeof(string));
    speaker.Members.Add(firstNameProperty);
 
    CodeMemberField surnameField = new CodeMemberField(
     new CodeTypeReference(typeof(string)), "_surname");
    speaker.Members.Add(surnameField);
 
    CodeMemberProperty surnameProperty = new CodeMemberProperty();
    surnameProperty.Attributes = MemberAttributes.Public | MemberAttributes.Final;
    surnameProperty.GetStatements.Add(
     new CodeMethodReturnStatement(
      new CodeFieldReferenceExpression(
       new CodeThisReferenceExpression(), "_surname")));
    surnameProperty.Name = "Surname";
    surnameProperty.SetStatements.Add(new CodeAssignStatement(
     new CodeFieldReferenceExpression(new CodeThisReferenceExpression(), "_surname"),
     new CodePropertySetValueReferenceExpression()));
    surnameProperty.Type = new CodeTypeReference(typeof(string));
    speaker.Members.Add(surnameProperty);
 
    return "Person";
   }
 
   return null;
  }
 }
}

Here's how it works: Schema importer extensions form a pipeline of processing units. If/When an extension finds a type it is responsible for generating custom code, it does so via the Code DOM provided by the .NET Framework. In the code example above, you can see that the only type this extension can process is the element, Person that resides in the namespace, "http://bristowe.com/".

Here's how the process looks from a 50,000' view:

When the XML Schema Definition Tool (xsd.exe) is used to import a XML Schema, it uses the System.Xml.Serialization.XmlSchemaImporter class internally to process any/all schema elements found in the XML Schema document (foo.xsd). When created, the XmlSchemaImporter class loads any schema importer extensions defined in the underlying configuration. In this instance, leveraging machine.config is the default usage pattern. Here's how a schema importer extension is registered in machine.config:

<configuration>
 ...
 <system.xml.serialization>
  <schemaImporterExtensions>
   <add
    name="Bristowe.Examples.Xml.Serialization.PersonSchemaImporterExtension"
    type="Bristowe.Examples.Xml.Serialization.PersonSchemaImporterExtension,
     Bristowe.Examples.Xml,
     Version=1.0.0.0"
   />
  </schemaImporterExtensions>
 </system.xml.serialization>
</configuration>

With these extensions loaded, xsd.exe is ready to process the XML Schema. Now when the tool encounters your type (i.e. bristowe:Person), your custom code will be added to the generated class file. Neat, eh?