Click here to Skip to main content
15,867,860 members
Articles / Mobile Apps / Windows Phone 7
Article

Consume Console Host WCF Service with Windows Phone 7

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
18 Apr 2011CPL4 min read 27.1K   525   10   2
Consume WCF service with Windows Phone 7
Article.gif

Introduction

I recently created a simple test app/demo using Windows Phone 7 to consume WCF Services which hosts in a console. There are a few good tutorials/documentation out on the internet for this topic.

All of these tutorials are all very good to explain what and how to consume WCF service from the Windows Phone 7 platform. The common technique is that when consuming these WCF services, we have to make a 'Service reference' to the actual WCF service which can be hosting in IIS or expose wsdl, i.e., the service can be seen by the browser.

But what about console host WCF service? Ouch, it won't work then, since we can't directly add to the 'Service reference' as there is no way we can get to these services because they are simply not accessible. Then, maybe we can think there is a way similar to Silverlight project consuming the service via ChannelFactory and create the proxy that way, so I had a go and the result is 'NotSupportException' is thrown. Ahh.. hit the rock again, as stubborn as all developers in the world, I think there must be a way but just don't know how, so the search is on ...

So I searched the interweb again up and down with no luck, there is a document saying you need to implement Clientbase<TChannel> interface and IChannel, etc. But since I am not that keen on doing all this dirty work, I put my thinking cap on and had a coffee of course.

Then, with the help from the caffeine, if I can't add 'Service Reference' to these services why don't I add 'Service Reference to a newly created WCF service, I am sure I can do that nice and easy and then with this newly created service pointing to the existing WCF service that is hosted in the console application, viola it works!

So to save my fellow developer's time, I think I have to share it and here it is.

Background

System requirements: .NET Framework 4.0 VS 2010 and Windows Phone 7 SDK.

To demonstrate this, I have created the following.

The Projects

There are a total of 9 projects within the solutions:

  1. ActualService
  2. ActualServiceForPhone
  3. IPerson
  4. IPersonSL
  5. IPersonSLPhone
  6. ServiceHost
  7. SLClient
  8. SLClient.Web
  9. SLPhoneClient

Before I explain what each of the projects does, please bear in mind that this article is not about coding but rather the trick or technique to get round consuming existing WCF services via a bridge in the sense of new service.

ActualService

This is the actual service which contains the real implementation of the interface for the Person objects:

C#
public class Service1 : IService1, IServicePolicy
{
    ...
}

ActualServiceForPhone

This is the actual bridge service from the Windows Phone 7 making call to the underlying WCF service "ActualService".

IPerson

The interface class is used in both the service and client, i.e., this interface is used in the implementation on the service level and used as type definition in the client respectively.

C#
[ServiceContract]
public interface IService1 { ... }

[ServiceContract(Name = "IService1")]
public interface IService1Async { ... }
    
[DataContract]
public class Person {
	[DataMember] ...
}

IPersonSL and IPersonSLPhone

Both of these 2 class libraries are identical to IPerson, as per request each of the project type needs its own class library meaning Silverlight project requires Silverlight class library, Windows Phone 7 will then require its own class library type and hence the requirement of these 2 types. Notice the library file is an actual link to the Person.cs rather than an exact copy as this will save some time during update/maintenance. SL-silverlight, SLPhone-silverlight Windows Phone 7.

ServiceHost

This is the console host for my WCF service:

C#
using (ServiceHost host = new ServiceHost(typeof(Service1), 
 	new Uri[]{ new Uri("http://localhost:60011", UriKind.Absolute) }))
{
    ...
    host.Open();
    Console.ReadLine();
    host.Close();
    ...
}

SLClient and SLClient.Web

These two projects are a default template project for Silverlight 4.0 with SLClient containing just a single button and a grid to reflect the data coming from the ActualService WCF service.

SLPhoneClient

This is the Windows Phone 7 Silverlight Project. Select the standard template for Silverlight application creation. This project also contains a single button and a list box to display data coming from ActualServiceForPhone.

To run the project:

  • Download the attached solution zip file.
  • Fire VS 2010 and open the solution file and compile all projects.
  • Start servicehost in a console with admin rights (Start cmd in admin mode).
  • Set ActualServiceForPhone as 'Start up project' and hit F5 (To ensure the service is running, remember to check the port number as well).
  • To run the SLClient (silver switch 'Start up project' to this project and hit F5).
  • Similarly to test the Windows Phone 7, select the SLPhoneClient as 'Start up project' and hit F5.

Hope everyone enjoyed this article. Happy programming.

History

  • 18th April, 2011: Initial post

License

This article, along with any associated source code and files, is licensed under The Common Public License Version 1.0 (CPL)


Written By
United Kingdom United Kingdom
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralThoughts Pin
PIEBALDconsult19-Apr-11 2:53
mvePIEBALDconsult19-Apr-11 2:53 
GeneralRe: Thoughts [modified] Pin
D Y Chan19-Apr-11 4:02
D Y Chan19-Apr-11 4:02 

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

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