Compartir a través de


WCF in .NET Compact Framework 3.5

Many developers have questions around Windows Communication Foundation and Windows phone (formerly Windows Mobile:-)).  The WCF implementation in .NET Compact Framework 3.5 is a subset of the desktop implementation.  This blog post lists what’s supported and what’s not supported.

Tips for WCF in .NET CF

The first gotcha is that by default the WCF project template generates a wsHttpBinding for the endpoint, which is not supported.  You can switch the endpoint to basicHttpBinding to get past that issue. .NET CF supports the SoapFormatter but not that BinaryFormatter.  Also, config file driven WCF configuration is not supported.  There are are other features not supported as well so check the blog post first before trying to implement a service for mobile.

To generate a WCF proxy in a Smart Device project, you should not use “Add service Reference” within the project or it can result in errors.  Instead download the Power Toys for .NET Compact Framework 3.5 and use the netcfsvcutil.exe tool to generate client proxy files.

There is an issue when using the tool on Windows 7, Windows Vista SP2 or Windows Server SP2 as detailed here.  You can download an updated version of netcfsvcutil.exe available here and save it to the C:\Program Files (x86)\Microsoft.NET\SDK\CompactFramework\v3.5\bin directory on an x64 installation.

Here is an example command line:

image

The command-line is similar to svcutil.exe, using the /namespace and /out command-line switch with the URL to the service.  Make sure the service is running if not hosted in IIS.  Also notice that the URL passed in includes the IP address instead of localhost.  You cannot call a service using localhost from the device emulator so the service is running using the IP address instead of localhost.  See the whitepaper referenced in the section below for information on how to edit your environment to use a friendly hostname instead of IP address.

Note: You will need to manually add a reference to System.ServiceModel and System.Runtime.Serialization manually for the generated proxy class to compile.

More information on using the tool can be found in the dv_fxnetcfpt_enu.chm located in C:\Program Files (x86)\Microsoft.NET\SDK\CompactFramework\v3.5\bin.

Programming WCF in .NET CF

An excellent reference on using WCF in .NET Compact Framework 3.5 that you should read cover to cover is the whitepaper posted on CodePlex called WCF Guidance for Mobile Applications located here. This paper (sample code also available) has a brief mobile development overview if needed but the meat of the paper takes you through the steps to program WCF in Windows phone and .NET Compact Framework 3.5.  This white paper goes into detail on what’s supported, as well as provide options to extend the built in functionality for key scenarios such as adding support for SOAP faults, custom message headers, and mutual authentication with certificates.