Wednesday, September 9, 2009

What's New for WCF in Visual Studio 2008

Code download available at: Foundations2008_02.exe (329 KB)
Browse the Code Online
Visual Studio® 2008 and the underlying Microsoft® .NET Framework 3.5 offer new tools and support for Windows® Communication Foundation (WCF). They don't change the basic capabilities of WCF 1.0 (released with the .NET Framework 3.0); rather, they extend and complete it.
Visual Studio 2008 automates manual WCF tasks, which includes updating proxy references, and eliminates repetitive tasks such as creating simple host projects. Visual Studio also addresses some tough problems such as cross-targeting and data contract type sharing. In this column I'll walk through the new features, point out their advantages, and explain any pitfalls and workarounds. Although I'll use C# project settings here, everything applies to Visual Basic® as well unless I state otherwise.

.NET Framework Cross-Targeting
Previous Visual Studio releases always targeted the version of the .NET Framework they shipped with. For example, Visual Studio 2005 can only generate assemblies that target the .NET Framework 2.0. This practice did not reflect the reality most developers face. Typically, developers need to maintain old versions of their applications written for previous versions of .NET while at the same time using the new version of Visual Studio on the application's next version.
Moreover, this practice meant that developers who were maintaining applications written for previous .NET Framework versions could not benefit from productivity enhancements, such as the code refactoring support introduced in Visual Studio 2005.
The problem was that there was no cross-targeting of .NET Framework versions. You either had to have multiple versions of Visual Studio installed or compensate with separate testing and deployment builds. Visual Studio 2008 tries to address this by providing adequate (albeit imperfect) support for targeting multiple versions of the .NET Framework. Since the .NET Framework 3.0 and the .NET Framework 3.5 actually use the same CLR version as the .NET Framework 2.0, and the only differences are in new referenced assemblies, Visual Studio can still target the same runtime yet provide cross-targeting for the .NET Framework versions 2.0, 3.0, and 3.5 (where the .NET Framework version number corresponds to release numbers, not runtime versions—that is still the CLR 2.0).

In Visual Studio 2008, the Application pane of the project Properties contains a new combobox called Target Framework that allows you to target the .NET Framework versions 2.0, 3.0, and 3.5 (see Figure 1).
Figure 1 Target Framework Property in Visual Studio 2008 (Click the image for a larger view)

The Target Framework value is only in effect at development time; it has no effect at run time (your assembly still targets the .NET 2.0 CLR). The value you choose represents the oldest version of the .NET Framework your assembly can be built against. New projects are configured by default to target .NET Framework 3.5. It gets a bit more complicated when adding references; if you downgrade the Target Framework version while referencing higher-version assemblies, Visual Studio 2008 will prompt you, fault the reference, and fail the build. Visual Studio 2008 will not allow you to add a reference to a .NET Framework assembly that requires a higher version of the Framework than your project. If you add a reference to another project in the same solution that is of a higher version, Visual Studio 2008 will alert you as to the possible conflict. If you add a reference by browsing to an assembly, Visual Studio 2008 will not intervene to stop you.
With respect to languages and cross-targeting, note that in C# (but not Visual Basic), you can restrict use of features such as anonymous types and extension methods in a .NET Framework 2.0 or 3.0 project by restricting the compiler version. You can do this by going to the Build pane, clicking the Advanced button, and selecting ISO-2 (C# 2.0) for the language version (instead of the default, which has not been standardized as of yet).
When opening a Visual Studio 2005 WCF project in Visual Studio 2008, the upgrade process will keep the framework version at 2.0. While this will actually work (remember, the underlying runtime version is unchanged), I recommend manually setting it to version 3.0 or 3.5, as required.
The Target Framework version matters the most when taking advantage of new project templates. WCF workflow and syndication projects must be built targeting the .NET Framework 3.5; the Service Library project requires targeting the .NET Framework 3.0 or 3.5. The Add Service Reference feature (described later in this column) is only available when selecting Framework version 3.0 or 3.5 for the project.

WCF-Provided Host
Visual Studio 2008 ships with a ready-made, general-purpose service host called WcfSvcHost.exe. It's found under C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE. For ease of use, I recommend adding that location to the system's Path variable. WcfSvcHost is a simple command-line utility, accepting two parameters: the file path to a .NET assembly containing the service class or classes, and a file path to the host .config file. For example:

See full details:

No comments: