Calling WCF services from NetCF 3.5 using Compact WCF and NetCFSvcUtil.exe

Published 13 September 07 12:29 AM | andarno 

The .NET Compact Framework 3.5 adds a subset of the Windows Communication Foundation (WCF or "Indigo") to smart devices, allowing them to communicate with desktop WCF components with all the flexibility of multiple, interchangeable service bindings and endpoints.  Although it ships out of the box only with support for message-level communication (no service calls, per se), we have a code generation tool that will allow you to call WCF or other WSDL-publishing services.

Desktop has an svcutil.exe tool that generates client proxy code, but the source code it generates cannot compile against NetCF libraries because NetCF does not include all the classes referenced from that source code.  While you can modify the code by hand to eventually get it to work with NetCF, we offer an easier way.

The NetCFSvcUtil.exe tool that ships as part of the Power Toys for .NET Compact Framework 3.5 supports many of the same command-line switches that svcutil.exe does, but performs a few additional/modified steps:

  1. Verifies that the service being consumed offers endpoints compatible with the feature subset included in NetCF 3.5.
  2. Generates the client proxy class in C# or VB that compiles against NetCF or desktop WCF.
  3. Generates code for a CFClientBase class that fills in where NetCF doesn't support calling WCF services.
  4. Does not generate a .config file to store the endpoint information.  This information is stored in the client proxy code.  If an X509 certificate required by the endpoint, that must be passed into the proxy class' constructor by your code.

Assuming that you have a service running at http://localhost/service/service.svc on your desktop computer and a device application in your C:\deviceapp directory, you could generate the code to call the service from NetCF using the following command line.  Be sure NetCFSvcUtil.exe is in your path.  Also be sure to use a machine name that can be reached from your device.  If you use "http://localhost/..." as your argument to the tool, your device will look for localhost (which is the device itself!) and not find the service that is running on your Windows box.

C:\deviceapp>netcfsvcutil.exe http://<machinename>/service/service.svc

The tool will generate and list two source files.  The source file named after your service will be the one that contains the proxy class you'll use from your app.  You can use it just like you would the svcutil.exe-generated code, except for the provision of secure endpoints made in my numbered list above.

To use these source files in your application, add them to your Visual Studio project and be sure you have System.ServiceModel.dll and System.Runtime.Serialization.dll in your project's Referenced Assemblies list in Solution Explorer.  Calling your service from your device application can be as simple as a couple of lines of code:

CalculatorServiceClient client = new CalculatorServiceClient();
MessageBox.Show(client.Add(3, 5));

Some features that WSDLs and the desktop .NET WCF supports are not supported by the .NET Compact Framework 3.5.  Although NetCFSvcUtil.exe generates code to enable some of these features, here is a list of features that remain unavailable for our first version of Compact WCF when calling a WCF service on desktop:

  • Fault contracts (they are not automatically deserialized for you, but you get the raw xml that you can parse manually)
  • Custom message headers (including peer hop counts)
  • Callback contracts
  • Sessions
  • Mandatory transactions

And the only desktop binding NetCF 3.5 supports is basicHttpBinding, with optional WS-Security (X.509 only) and no support for WS-ReliableMessaging.  NetCF 3.5 adds the new mail transport as well, but NetCFSvcUtil does not support generating proxies for services exposed only on the mail transport.

It's worthwhile to note that any service or client that supports WSDL contracts should be able to intercommunicate, regardless of underlying implementation.  This means that if you have a desktop WCF service that you want to call from NetCF, you may also use the already-existing Add Web Reference in Visual Studio to generate code for your client proxy class (the command line equivalent is wsdl.exe).  The resulting proxy class may not have as friendly of a public API for you to use, and it won't use the WCF stack, but it is a valid alternative that will work in many cases.

The reverse is also true: you can use WCF to call into old-style .asmx web services.  It is not necessary to even know which web service implementation is used on a server.  So long as it publishes a WSDL, you're in luck.  If it doesn't publish a WSDL, you may still be able to generate a client proxy for it as long as it uses WCF service model and you have access to the managed service assemblies.  But that's another topic.

Filed under: , , ,

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

# David Kline said on September 13, 2007 1:59 PM:

The .NET Compact Framework team made an exciting CTP available this week.&#xA0; The .NET Compact Framework

# Noticias externas said on September 13, 2007 2:13 PM:

The .NET Compact Framework team made an exciting CTP available this week.&amp;#xA0; The .NET Compact

# Christian Bøgh Jensen's Blog said on October 11, 2007 11:56 AM:

Andrew Arnott has an interesting post on WCF support in NetCF 3.5. Looks like Fault Contracts are not

# Noticias externas said on October 11, 2007 12:03 PM:

Andrew Arnott has an interesting post on WCF support in NetCF 3.5. Looks like Fault Contracts are not

# Jo Vermeulen said on October 15, 2007 11:38 AM:

Is it possible to use NetCFSvcutil.exe to generate .NET 2.0-compatible code? Or does it require .NETCF 3.5?

I also get an "operation contract not found" error for one of the web services I tried.

Thanks!

# Compact Framework said on November 29, 2007 4:11 PM:

An Introduction to WCF for Device Developers Chris Tacke OpenNETCF Consulting, LLC November, 2007 Download

# AlexanderZeitler said on December 6, 2007 8:13 AM:

Will the Power Toys CTP work with CF 3.5 Final?

# andarno said on December 22, 2007 12:41 PM:

Jo,

NetCFSvcUtil generates code that requires WCF libraries, which are only available in CF 3.5, so the proxies will not work on CF 2.0. It would be nice to get a version of wsdl.exe working for CF 2.0, wouldn't it?

When you get operation contract not found errors, you might want to check the validity of the WSDL.  Try using svcutil.exe on it and see if that gives you a different result.

# andarno said on December 22, 2007 12:42 PM:

Hi Alex,

The RTW version of the Power Tows is available now.  You should install that for use with CF 3.5 RTM to avoid some failures, although NetCFSvcUtil proxies generated from the CTP version will work on 3.5 RTM just fine.

# Eric T. said on August 14, 2008 9:32 AM:

Any idea when Asynchronous web service calls will be supported by the .NET CF?

# John Waters said on October 14, 2008 6:24 PM:

I have been googling for a few days trying to find out how to implement username/password authentication from a .NET CF 3.5 client against a WCF service.

I understand that is not buiilt in, but wondered if anyone has some pointers to how I might implement it using custom headers?

I am using Transport security over the basicHTTPBinding.

Any help would be much appreciated!

# lakshmi said on May 6, 2009 10:39 AM:

Hi i am getting

TargetInvocation Exception

Please help me.

# Ben said on June 25, 2009 5:05 PM:

Is it possible to have a mobile device actuallt HOST a WCF service?  That probably seems like an odd request, but WCF will be an ideal base upon which for us to build only if the host computer can contact the clients as well as simply having the clients contact the host.  The standard issue CF libraries don't seem to supposrt ServiceHost, but is there a way to get ServiceHost to run on a program built with the CF libraries anyway?  Thanks in advance for any pointers.

# Carl Howarth said on June 29, 2009 4:41 AM:

I am trying to generate proxies using netcfsvcutil with the /r switch pointing to a shared dll. The shared dll is not used within the proxies, can anyone help please? The shared dll does work in a non-CF client.

# Carl Howarth said on July 1, 2009 4:19 AM:

Hi there,

I'm struggling to get netcfsvcutil to use a shared class library... i add the switch '/r:c:\common.dll' for example, but this seems to be ignored and proxies are generated instead of reusing the common elements of code. If I use the desktop equivalent the shared library is used as I would expect. Are there limitations on the types of libraries that may be used at all?

Any help would be greatly appreciated.

Thanks and best regards, Carl

# Frank Silva said on September 16, 2009 7:05 PM:

Andrew we are working with this with great results as well as scratches. But we have now an issue. We just exposed another service we also consume from the PDA, I generated the proxy classes, the CFCBase one is identical, but the other has some re-implementations, compared to the first calss we generated.. is there a way around this issue?

Leave a Comment

(required) 
(optional)
(required) 

  
Enter Code Here: Required

Search

This Blog

Interesting blogs

Related sites

Syndication

Page view tracker