MSDN Magazine Launch Issue - February 15, 2008 - (Page 43) Figure 9 Generating the Proxy Class C:\demos\MsdnCFServiceSample\NetCFClient>”\Program Files (x86)\Microsoft .NET\SDK\CompactFramework\v3.5\bin\NetCFSvcUtil.exe” http://localhost:53222/Service/Service.svc?wsdl Microsoft (R) .NET Compact Framework Service Model Metadata Tool [Microsoft (R) Windows (R) Communication Foundation, Version 3.5.0.0] Copyright (c) Microsoft Corporation. All rights reserved. Attempting to download metadata from ‘http://localhost:53222/Service/ Service.svc?wsdl’ using WS-Metadata Exchange or DISCO. Generating files C:\demos\MsdnCFServiceSample\NetCFClient\Service.cs C:\demos\MsdnCFServiceSample\NetCFClient\CFClientBase.cs C:\demos\MsdnCFServiceSample\NetCFClient> One additional limitation remains in your service beyond just using BasicHttpBinding: custom headers are not supported by NetCFSvcUtil.exe. If your public static System.ServiceModel.EndpointAddress service requires custom headers to be included in EndpointAddress = your messages, you will need to either modify the new System.ServiceModel.EndpointAddress( code emitted by the tool or write your own proxy “http://mycomputername:53222/Service/Service.svc/ basic”); class to add support for these custom headers. Also, notice the /basic suffix to the URL. This is Now add a.NET Compact Framework applicahow the client indicates to the service which of the tion to your solution. Create a new project using three offered endpoints it wants to use. When you the Smart Device Project template and name it have the generated proxy class source files, simply NetCFClient. Add the Compact WCF assemblies add them to your device project in Visual Studio. to your project references: System.ServiceModel.dll and System.Runtime.Serialization.dll. Next, add a Using the WCF Service single button onto your Form1.cs design surface Earlier you created an event handler for the sinand set its caption to “Call Service”. Double-click Figure 10 Running the App gle Call Service button. Let’s implement that now. it to create an event handler. Now you just need a against the WCF Service The code should look just like calling a WCF serproxy class to invoke the service. vice from a WCF desktop application: Note that the URL you pass to the tool on the command line will be the URL that the device uses to contact your service, so http:// localhost may work to generate your proxy, but your device app will try to talk to itself rather than contact your desktop PC. Also note that this tool did not generate an output.config file the way the desktop svcutil.exe tool would. The .NET Compact Framework 3.5 does not support configuring WCF through configuration files, so all the endpoint information for a service needs to be in code. The ASP.NET development server will not respond to device requests unless the device or emulator is cradled. What I have done here is use localhost to generate the proxy, then modify the generated Service.cs file by finding the EndpointAddress field defined in the ServiceClient class and changing the URL to use my hostname (the service address) rather than localhost. The code should look something like this: Creating a Proxy Class Run the NetCFSvcUtil.exe tool against the service to generate the proxy class to include in the device project (see Figure 9). You’ll want the source files generated to go into the device application’s source directory, so run the tool from there. The tool itself (once installed) should be found in %PROGRAMFILES%\Microsoft .NET\SDK\ CompactFramework\v3.5\bin, although if you’re running on a 64-bit machine, check the Program Files (x86) directory. To run the tool, you only need to pass in the URL to the service metadata (the service must be running). You can add /language:vb if you want the tool to emit the proxy class for a Visual Basic® project. void callServiceButton_Click(object sender, EventArgs e) { ServiceClient client = new ServiceClient(); MessageBox.Show(client.GetData(5)); } .NET Compact Framework Resources .NET Compact Framework Team blog blogs.msdn.com/netcfteam .NET Compact Framework 3.5 Redistributable and Power Toys msdn2.microsoft.com/aa497280 Some individual blogs related to .NET Compact Framework development blogs.msdn.com/andrewarnottms; blogs.msdn.com/markprenticems; blogs.msdn.com/davidklinems; jmpinline.nerdbank.net Deploy and run your device app. Click the Call Service button. After a moment you should see a message box pop up with “You entered: 5” (see Figure 10). If you get an error that your host could not be reached, try cradling your device. If your device is an emulator, you can cradle the emulator via the Device Emulator Manager in Visual Studio. The proxy class generated by the tool translates method calls into WCF messages and sends them using the .NET Compact Framework messaging layer. Response messages are then deserialized and passed back to your app via the method’s return value. The code generated by the NetCFSvcUtil tool will run equally well under the full version of WCF, in which case the proxy classes derive from a WCF class called ClientBase . Since the .NET Compact Framework 3.5 does not ship with ClientBase , the NetCFSvcUtil tool emits code for a CFClientBase that must be included with your application. Although this class performs a similar function to the ClientBase class found in WCF on the desktop, it is not exactly the same and may change in future versions. ■ Mobile WCF launch2008 43 http://blogs.msdn.com/netcfteam http://msdn2.microsoft.com/aa497280 http://blogs.msdn.com/andrewarnottms http://blogs.msdn.com/markprenticems http://blogs.msdn.com/davidklinems http://jmpinline.nerdbank.net
For optimal viewing of this digital publication, please enable JavaScript and then refresh the page. If you would like to try to load the digital publication without using Flash Player detection, please click here.