Click here to Skip to main content
Licence CPOL
First Posted 14 Jun 2009
Views 21,924
Downloads 670
Bookmarked 29 times

Using WCF in .NET Compact Framework

By asprodotru | 14 Jun 2009
This article will show you how to create a proxy client to the WCF web service from PDA device.

1

2
1 vote, 9.1%
3

4
10 votes, 90.9%
5
5.00/5 - 11 votes
1 removed
μ 4.69, σa 1.06 [?]
screen1.jpg

Introduction

Creating various web services, we can very easily also create client proxies for the PC in various ways: meaning of the tool Add service reference in Visual Studio or simply setting parameters in a command line svcutil.exe, receiving on exit a code and configuration file. But what do we do if the business concerns portable devices like Pocket PC or Smartphone? Fortunately, there is a solution to this problem which I am going to describe in given article.

Solution to the Problem

For example, we are going to create the new WCF web service in Visual Studio 2008 - File -> New -> Project -> Web -> WCF Service Application with name BooksService. Then, having created the necessary types of the data and function, we start our web service. It will be possibly in IIS 6.0/7.0 or built-in VS2008 ASP.NET Developer Server. Well, now it is possible to ask a fair question - how do we connect from the pocket computer to our service? Fortunately, instead of writing your own proxy class, there is a simpler way which consists of using the utility netcfsvcutil.exe, which you can download from this link. Then, let's set in parameters of the given utility in the next line: "http://192.168.134.10:8888/BooksService.svc". On exit, we will receive two files: CFClientBase.cs and BooksServie.cs. It is just what is necessary for us. Now I will, in detail, tell about the creation of service and the convenient mobile client for it.

WCF Service

After creation of this web service, we declare its interface as follows:

[ServiceContract]
    public interface IBooksService
    {
        [OperationContract]
        string[] GetBookNames();
        [OperationContract]
        Book[] GetBooks(GetBookType type, string input);
        [OperationContract]
        Book[] GetAllBooks();
        // TODO: Add your service operations here
    }

As you can see, it is the Book class used in this piece of code. Its implementation looks as follows:

[DataContract]
    public class Book
    {
        [DataMember]
        public string Name { get; set; }
        [DataMember]
        public string Author { get; set; }
        [DataMember]
        public string ISBN { get; set; }
        [DataMember]
        public decimal Price { get; set; }
        [DataMember]
        public string Description { get; set; }
    }

    public enum GetBookType
    {
        ISBN, Name, Author
    }

But when does the service takes this information about books? Not to complicate an example working with such databases as SqlServer or Oracle, we will store our data in an XML file data.xml which is built in folder App_Data.

<Books>
  <Book>
    <Name>Data Access and Web Services for Rich Internet Applications</Name>
    <Author>John Papa</Author>
    <ISBN>0-596-52309-2</ISBN>
    <Price>44,99</Price>
    <Description>This comprehensive bookl teaches you how to build
	data-rich business applications with Silverlight 2 that draw on
	multiple sources of data. Packed with reusable examples,
	Data-Driven Services with Silverlight 2 covers all of the data access
	and web service tools you need, including data binding, the LINQ
	data querying component, RESTful and SOAP web service support,
	cross-domain web service calls, and Microsoft's new ADO.NET Data Services
	and the ADO.NET Entity Framework.</Description>
  </Book>
...
<Books>

The implementation of IBooksService interface is as follows:

public class BooksService : IBooksService
    {
        XDocument document = XDocument.Load(String.Format("{0}\\{1}\\{2}",
	HostingEnvironment.ApplicationPhysicalPath, "App_Data",
	"data.xml")); //getting path to the web service location and data file

        public string[] GetBookNames()
        {
            var q = from dest in document.Root.Elements("Book")
                    select dest.Element("Name").Value; //return only name
            return q.ToArray();
        }

        public Book[] GetBooks(GetBookType type, string input)
        {
            switch (type)
            {
                case GetBookType.ISBN: //if user select "ISBN" filter
                    {
                        var q = from dest in document.Root.Elements("Book")
                                where dest.Element("ISBN").Value ==
				input //Strict comparison of the "ISBN" data
                                select new Book()
                                {
                                    Author = dest.Element("Author").Value,
                                    Description = dest.Element("Description").Value,
                                    ISBN = dest.Element("ISBN").Value,
                                    Name = dest.Element("Name").Value,
                                    Price = Convert.ToDecimal(dest.Element("Price").Value)
                                };
                        return q.ToArray();
                    }
                case GetBookType.Author: //if user select "Author" filter
                    {
                        var q = from dest in document.Root.Elements("Book")
                                where dest.Element("Author").Value.ToLower().
				IndexOf(input.ToLower()) != -1 //Change of the
					//register on lower and search of
					//occurrence of a line "input"
					//in an initial line
                                select new Book()
                                {
                                    Author = dest.Element("Author").Value,
                                    Description = dest.Element("Description").Value,
                                    ISBN = dest.Element("ISBN").Value,
                                    Name = dest.Element("Name").Value,
                                    Price = Convert.ToDecimal(dest.Element("Price").Value)
                                };
                        return q.ToArray();
                    }
                case GetBookType.Name: //if user select "Author" filter
                    {
                        var q = from dest in document.Root.Elements("Book")
                                where dest.Element("Name").Value.ToLower().
				IndexOf(input.ToLower()) != -1 //Change of the
				//register on lower and search of
				//occurrence of a line "input" in an initial line
                                select new Book()
                                {
                                    Author = dest.Element("Author").Value,
                                    Description = dest.Element("Description").Value,
                                    ISBN = dest.Element("ISBN").Value,
                                    Name = dest.Element("Name").Value,
                                    Price = Convert.ToDecimal(dest.Element("Price").Value)
                                };
                        return q.ToArray();
                    }
                default:
                    {
                        return new List<book />().ToArray();
                    }
            }
        }

        public Book[] GetAllBooks()
        {
            var q = from dest in document.Root.Elements("Book") //return all books
                    select new Book()
                    {
                        Author = dest.Element("Author").Value,
                        Description = dest.Element("Description").Value,
                        ISBN = dest.Element("ISBN").Value,
                        Name = dest.Element("Name").Value,
                        Price = Convert.ToDecimal(dest.Element("Price").Value)
                    };
            return q.ToArray();
        }
    }

As you can see, I work with XML data using LINQ to XML because it is the easiest way for me. Now, when we have a ready service, we can place it on any hosting and be connected to the set address. So, we will start the most interesting part of work - the creation of the pocket client for it.

PDA Client

To begin with, you should create the new project Smart Device Project. After that, you will have a new pure form and automatically created code of initialization for it. On it, meanwhile it is possible to stop. As I said in "Solution to the Problem" section, you should run netcfsvcutil.exe and start with the necessary parameters for you. You should attach CFClientBase.cs and BooksService.cs files to your current project. You now have a ready proxy class for work. Let's look at how to correctly initialize the connection to our service:

private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                string remoteAddress = HelpClass.BuildUrl(); //get address
                EndpointAddress endpoint = new EndpointAddress(remoteAddress);
                client = new BooksServiceClient(new BasicHttpBinding(),
				endpoint); //create new client proxy
                comboBox1.Items.Clear();
                foreach (string s in client.GetBookNames())
                {
                    comboBox1.Items.Add(s);
                }
                books.Clear();
                books.AddRange(client.GetAllBooks()); //get all names of the books
                seed = 0;
                if (books.Count > 1)
                    button1.Enabled = true;
                comboBox1.SelectedIndex = 0;
            }
            catch (Exception ex) { MessageBox.Show(string.Format
				("Connection Error: {0}", ex.Message)); }
        }

The HelpClass class has been used:

public static class HelpClass
    {
        static XDocument doc = XDocument.Load
	(Assembly.GetExecutingAssembly().GetName().CodeBase.Substring(0,
	Assembly.GetExecutingAssembly().GetName().CodeBase.LastIndexOf('\\') + 1) +
	"settings.xml");

        public static string BuildUrl()
        {
            return string.Format("http:/ /a{0}:{1}/BooksService.svc",
		doc.Root.Element("Host").Value, doc.Root.Element("Port").Value);
        }
    }

So, after the connection to a final point, you can high-grade use all functions which the web service provides.

Conclusion

All in all, as you can be convinced, the creation of the elementary client to service does not require special work. All basic work on proxy creation is incurred by the utility netcfsvcutil.exe, on your responsibility in instructions of the service address and initialization of the client through basicHttp binding. Good luck!

screen.JPG

History

  • 14th June, 2009: Initial post

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

asprodotru

Software Developer

Russian Federation Russian Federation

Member
I have started programming in 13 years on Delphi.Then it was Pascal,Php,C++,some JavaScript.Then it were C# and .NET Framework, such tehnologies as WCF, WPF, ADO.NET, ASP.NET, LINQ, ASP.NET MVC Framework, Entity Framework, ASP.NET Ajax and another.
 
Now it is at all Java EE/SE technologies.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralGetting the parent directory of a file PinmemberWesley Harris20:44 22 Nov '10  
GeneralCF to WCF Pinmembermaxnis6:11 12 Nov '10  
GeneralRe: CF to WCF PinmemberWesley Harris20:40 22 Nov '10  
Generalclosing a wcf service PinmemberJustin Carasick10:20 6 Sep '10  
GeneralShared Types Compact Framework PinmemberCarl Howarth4:33 29 Jun '09  
Generalnb - Binding PinmemberCashels20:48 16 Jun '09  
GeneralRe: nb - Binding Pinmembermartyn_mcfly22:02 16 Jun '09  
GeneralRe: nb - Binding PinmemberCashels22:23 16 Jun '09  
GeneralGreat PinmemberMember 41795951:44 16 Jun '09  
GeneralRe: Great Pinmembermartyn_mcfly2:13 16 Jun '09  
Thank you. Yes, this example is easily expanded under your needs. Big Grin | :-D

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web04 | 2.5.120210.1 | Last Updated 14 Jun 2009
Article Copyright 2009 by asprodotru
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid