Nick's .NET Travels

Continually looking for the yellow brick road so I can catch me a wizard....

WCF on Windows Mobile and .NET Compact Framework

I was just listening to Don, Dave and James on the second of the Jumpstart series for the Codemasons’ Guild and the topic of communicating via WCF came up.  Now typically when I build mobile apps I don’t go through all the pain of using WCF, I simply use a regular asmx web service and then use Add Web Reference to add it to my mobile project.  To secure it, I just communicate over SSL. If you do want/need to use WCF on the server side, there are a couple of options to do this.

Before we jump into how you use WCF, let me point out a couple of useful powertoys:

The Power Toys for .NET CF include NetCFSvcUtil.exe which is a device equivalent of SvcUtil.exe and is needed in order generate the WCF proxy.

Firstly, you need to be aware that the .NET CF has some severe limitations when it comes to WCF.  Unfortunately the only binding that is supported (excluding the much over-hyped WCF via Exchange) is basicHttpBinding. For the WCF service you want to consume you need to change it from using the default wsHttpBinding.  This can be done by launching the Tools > WCF Service Configuration Editor from Visual Studio. Open the web.config file for the WCF Service project.  Under Endpoints, adjust the Binding to basicHttpBinding.

image

Save this change and run the WCF Service.

Now to the options…..

1) The first option is to use Add Web Reference.  This is by far the simplest approach as you can click Browse to: Web services in this solution.  Select your service and click Add Reference. 

image

Once you have added the reference you can call your service method the same way you would from a regular desktop application:

localhost.Service1 service = new localhost.Service1();
service.Url = service.Url.Replace("localhost", "192.168.1.2");
return service.GetData(5, true);

Note: You have to change the “localhost” host name to something that can be resolved by the device.  I typically just use the ip address of the development machine.  Clearly for production you will want to specify this in a configuration file or make it a configurable setting within your application.

2) The second option is to use NetCFSvcUtil.exe to generate the appropriate WCF proxy information. Start by opening up the Visual Studio command prompt (Start > Microsoft Visual Studio 2008 > Visual Studio Tools > Visual Studio 2008 Command Prompt) and adding the path to the Compact Framework power toys:

>> set path=%path%;C:\Program Files\Microsoft.NET\SDK\CompactFramework\v3.5\bin

Navigate to the folder where you want the proxy files to be created and then use NetCFSvcUtil.  I figured this would be quite simple but it appears that somewhere between Vista SP1 and SP2 (and there are reports of this problem on Windows 7 too) a bug in NETCFSvcUtil surfaced preventing it from working.

image

As you can see the error message is really helpful:

Error: An error occurred in the tool.
Error: Error in the application.

Currently, there doesn’t seem to be a workaround for this.  Some people have had varied success by changing the parameters and return types of the service methods.  The one strategy I used that appears to work is to use a combination of SvcUtil and NetCFSvcUtil.

>> svcutil.exe c:\temp\WindowsMobileServices\MyDataServices\bin\MyDataServices.dll

Microsoft (R) Service Model Metadata Tool
[Microsoft (R) Windows (R) Communication Foundation, Version 3.0.4506.2152]
Copyright (c) Microsoft Corporation.  All rights reserved.

Generating metadata files...
C:\temp\WindowsMobileServices\WindowsMobileServices\tempuri.org.wsdl
C:\temp\WindowsMobileServices\WindowsMobileServices\tempuri.org.xsd
C:\temp\WindowsMobileServices\WindowsMobileServices\schemas.microsoft.com.2003.1
0.Serialization.xsd

>> netcfsvcutil.exe tempuri.org.wsd tempuri.org.xsd
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.

Generating files...
C:\temp\WindowsMobileServices\WindowsMobileServices\tempuri.org.cs
C:\temp\WindowsMobileServices\WindowsMobileServices\CFClientBase.cs

Now, include all the generated file into your mobile project.  You will also need to add references to System.ServiceModel and System.Runtime.Serialization.

In order to call your service method you can now write the following:

var binding = System.ServiceModel.BasicHttpBinding();
var endpoint = System.ServiceModel.EndpointAddress("http://192.168.1.2:6323/Service1.svc");
Service1Client client = new Service1Client(new binding, new endpoint
);
return client.GetData(5);

So, the question is why would you go the second option?  Well if you actually look at the generated code, adding the WCF service using Add Web Reference adds a bunch of unnecessary fields.  When calling the method GetData there is a second parameter called “valueSpecified” which is little more than a flag to indicate if the first parameter was specified or not.  This is not required if you use the second option to generate the proxy information.

Comments (6) -

  • Liem Le

    1/07/2010 8:47:08 PM |

    thanks for this article! it helped!

    in your article you mention "Error: An error occurred in the tool.
    Error: Error in the application.

    Currently, there doesn’t seem to be a workaround for this.  Some people have had varied success by changing the parameters and return types of the service methods."

    so i attempted to remove these lines of code from my wcf.
            //[OperationContract]
            //CompositeType GetDataUsingDataContract(CompositeType composite);
    and

            //public CompositeType GetDataUsingDataContract(CompositeType composite)
            //{
            //    if (composite.BoolValue)
            //    {
            //        composite.StringValue += "Suffix";
            //    }
            //    return composite;
            //}


    then it all worked. hope that helps someone.

  • Liem Le

    1/07/2010 8:47:28 PM |

    thanks for this article! it helped!

    in your article you mention "Error: An error occurred in the tool.
    Error: Error in the application.

    Currently, there doesn’t seem to be a workaround for this.  Some people have had varied success by changing the parameters and return types of the service methods."

    so i attempted to remove these lines of code from my wcf.
            //[OperationContract]
            //CompositeType GetDataUsingDataContract(CompositeType composite);
    and

            //public CompositeType GetDataUsingDataContract(CompositeType composite)
            //{
            //    if (composite.BoolValue)
            //    {
            //        composite.StringValue += "Suffix";
            //    }
            //    return composite;
            //}


    then it all worked. hope that helps someone.

  • Liem Le

    1/07/2010 8:47:37 PM |

    thanks for this article! it helped!

    in your article you mention "Error: An error occurred in the tool.
    Error: Error in the application.

    Currently, there doesn’t seem to be a workaround for this.  Some people have had varied success by changing the parameters and return types of the service methods."

    so i attempted to remove these lines of code from my wcf.
            //[OperationContract]
            //CompositeType GetDataUsingDataContract(CompositeType composite);
    and

            //public CompositeType GetDataUsingDataContract(CompositeType composite)
            //{
            //    if (composite.BoolValue)
            //    {
            //        composite.StringValue += "Suffix";
            //    }
            //    return composite;
            //}


    then it all worked. hope that helps someone.

  • Nike Shox NZ

    5/08/2010 7:26:32 PM |

    Very good site, excellent content, I will recommend to my group of readers in college too, I will leave an input that the key to success is to choose a product that's in demand.

  • iPhone Software Development

    30/09/2010 3:56:38 AM |

    I have read that by using WCF, you can send data as asynchronous messages from one service endpoint to another. A service endpoint can be part of a continuously available service hosted by IIS, or it can be a service hosted in an application. WCF is a flexible platform. Because of this extreme flexibility, WCF is also used in several other Microsoft products. By understanding the basics of WCF, you have an immediate advantage if you also use any of these products.WCF is designed in accordance with service oriented architecture principles to support distributed computing where services are consumed by consumers. Clients can consume multiple services and services can be consumed by multiple clients.

  • new nike

    28/10/2010 11:47:36 PM |

    Great Ifo. Great People. Great Blog. Thank you for all the great sharing that is being done here. And I know a great shoe website<a href="http://www.nikeshoesdance.com">关键词</new nike>

  • lahore web hosting

    3/12/2010 9:14:40 AM |

    Web Design and Web function are everything when it comes to online traffic and Web sales. If your website is lacking in any way, you are probably losing potential sales through your website. According to recent research by web and online business experts; customers will decide in just 7 seconds whether or not to stay on your website once they get there! This doesn’t leave much room for error does it? As soon as your website is accessed customers are looking for any reason (no matter how small) to click away from your web, you have to have a website that invites users to participate; that’s where we come in. We provide full service web design and web development, all you need to do is provide us with your vision and we’ll make it a reality. Our long list of satisfied web design, website develop and web hosting customers speaks for itself. They have catapulted us into becoming one of the preeminent Lahore web design companies… Respect, Dependability, Capability, and Opportunity have made us one of the most Renowned companies in web design, website development and web hosting feilds; it’s our pleasure to serve our wonderful clients, let us show you what they already know…. Successful websites all share the same basic traits.

  • 3 x mobile bingo

    5/12/2010 10:49:45 PM |

    Interesting article. You make some good points. Thank you again.

  • UFC 125 online

    21/12/2010 3:52:08 AM |

    i love reading your blog its really interesting....

    http://www.paslit.com/aggressive-personal-injury-lawyers-battling-for-your-rights
    http://hubpages.com/hub/Strong-Cut-throat-Personal-Injury-Lawyers-Fighting-for-Your-Rights
    http://www.bukisa.com/articles/422379_commanding-aggressive-personal-injury-lawyers-fighting-for-your-rights
    http://forums.tigsource.com/index.php?action=profile;u=43190
    http://www.earthswoop.com/forums/member.php?u=10331
    http://community.wreg.com/service/displayKickPlace.kickAction?u=29487245&as=96304
    http://www.sencha.com/forum/member.php?221053-mikeyhouse82

  • Capodanno roma

    30/12/2010 12:05:21 AM |

    Great work dude, u gave nice post to us. Thanks for spending the time to discuss this, I feel strongly about it and love learning more on this topic.

  • Capodanno roma

    30/12/2010 12:09:36 AM |

    Great work dude, u gave nice post to us. Thanks for spending the time to discuss this, I feel strongly about it and love learning more on this topic.

  • event planning software

    8/02/2011 9:08:50 PM |

    Thanks for such knowledge providing and the unknown technical facts.

  • Nike Shox Clearance

    22/02/2011 4:52:00 PM |

    Experience is the child of thought , and thought is the child of action. We cannot learn men from books.

  • Coach Bags Clearance

    23/02/2011 3:52:04 PM |

    Expericence is a hard teacher because she gives the test first, the lesson afterwards. http://www.popcoachbags.com/

  • free sms

    24/02/2011 2:51:47 AM |

    Thanks for sharing these useful information! Hope that you will continue doing nice article like this.

  • pacquiao vs mosley

    13/03/2011 11:47:24 PM |

    your blog is very useful

    http://my.opera.com/kickmoko/blog/2011/03/12/a-barbie-dress-up-game-is-an-online-game-created-more-than-just-to-entertain
    http://kickmoko.yolasite.com/blog/start-a-new-mood-of-styles-with-today-s-online-game-a-barbie-dress-up-game
    http://kickmoko.wikidot.com/a-barbie-dress-up-game-is-a-wonderful-online-game
    http://kickmoko.webnode.com/news/a-barbie-dress-up-game-is-an-awesome-online-game/

  • lifebook

    5/05/2011 5:38:51 PM |

    Real  nice design  and  superb   content material ,  absolutely nothing  else we  require : D.

Comments are closed