venerdì 13 novembre 2009

Calling a WCF Service from a .NET CF application

I'm currently developing a distributed C# application, named Hippocrates. It is a prototype for a Hospital distributed information system: every Unit runs its own database and a WCF service which is the database only access point. Such services will cooperate among themselves, sharing informations and forwarding requestes issued by computer terminals and mobile devices running Windows Mobile and the .NET 3.5 CF.

Well, I realized that developing applications for the .NET CF is not always straightforward: a lot of things are missing in the compact framework implementation, with respect to the legacy .NET framework and there are also a lot of things to keep in mind while programming mobile devices which makes use of WCF service references.

On the server side, you cannot make use of Async or Duplex Contracts. Second, your service must expose an EndPoint that uses the BasicHttpBinding, otherwise the mobile client won't be able to invoke any method.

Let's now see how to code the client part. Working with Visual Studio 2008 mobile project template, you won't be able to add a reference to a running WCF service through the "Add Service Reference" entry of the Smart Device Application Project properties: there's not such an option! I managed to solve such an issue downloading and installing the Power Toys for the Microsoft .NET Compact Framework. Then, using the NECTCFSvcUtil utility I generated the proxy code:

C:\Program Files\Microsoft.NET\SDK\CompactFramework\v3.5\bin\NETCFSvcUtil.exe
http://server:port/service/

This will generate two files (Service.cs and ServiceClient.cs) that I had to add to the mobile device project in order to invoke WCF methods. Once you've got this work done, you can invoke the service methods. For example, assuming you've got a WCF service contract that exposes a "Request" method:

ServiceClient client = new ServiceClient();
client.Request();

That's all, folks!

Nessun commento:

Posta un commento