this post doesn’t assume that u have any background on what the services is , so let’s start with a simple definitions
Service-oriented architecture :
simply it’s when your architecture is divided into separated components of code each one have it’s own functionality and they communicate together through a standard protocol of communication , so these services or components can communicate interoperably also each component can serve multiple clients so it can be reused from different domains
Main benefits from SOA
- interoperability : for example a Java based project could use a .net based web service , because the use same communication standard
- reuse : let’s assume a translator web service written once and could serve millions of applications ( phone , desktop , web applications )
- scalability and agility : the same translator web service when i change in it’s functions or add new dictionaries or languages i don’t need to contact all the clients that use this service to rechange their code . i can just change the service and it’s changed in all clients because they don’t copy the code they only use the service . as long as i keep the same contract
WEB services :
it’s a standard way for communication between web based applications in two devices over a network (internet ) using XML , SOAP , WSDL . XML is used to tag data and SOAP is the protocol of sending data and WSDL is the web services describing language used to describe the available webservice . unlike normal web applications WEBservices don’t need a browser also it doesn’t offer a GUI
web services examples :
- bing search service
- Microsoft translator service
- IMDB service
- AMAZON services
- Twitter / Facebook services
- Google API services
some drawbacks of WEBservices :
- internet connection is required
- transfer of large data across the internet may affects the performance
WCF services :
a part of the .NET Framework that provides a unified programming model for rapidly building service-oriented applications that communicate across the web and the enterprise.
WCF ABC :
- address : it’s the uri of the service or where you can find the service on the web
- binding : Specifies how a service is accessible. In other words: how the two parties will communicate in terms of transport (HTTP , TCP ,NamedPipe , Peer2Peer ,MSMQ)
- contract : it’s simply what the service can do ( the data and methods that the service can provide )
DEMO1 : Building a WCF service:

DEMO2 : Consuming a WCF service from Windows phone 7 :
this will be a sample demo on how to consume a webservice in windows phone 7
the only difference between normal consumption of webservices in WP7 and in any normal application
is that in normal application the communication is synchronous ( application calls web services and services responds in the same time to the application ) but in WP7 it’s asynchronous communication ( in which when windows phone calls the webservice the response in not in the same time but when the web service is done with the response it fires an event in the windows phone and we need to write in the event handler the code that needed to utilize the response ) don’t get confused with the above comparison , it will be elaborated in the code below
to consume a webservice , let’s take for an example microsoft translator web service which we can find on this link
http://msdn.microsoft.com/en-us/library/ff512435.aspx
the service uri (adress) is : http://api.microsofttranslator.com/V2/Soap.svc
now open a new windows phone project
from the project explorer right click the project icon and add service reference
copy paste the service URL : http://api.microsofttranslator.com/V2/Soap.svc in the address field and click Go
then change the namespace to TranslatorService for example in which all the Generated code will be under this namespace
once you added a reference to the web service now you can create new object of the service client class this object you will use it to call all the methods of the service
so open the mainpage.xaml.cs file and let’s write some code
let’s assume that the program that we are writing will translate the sentence in the textbox1 in french and show it in a text Block called textblock1 when we click a button called button1
from the toolbox drag a textbox textblock and a button , it’s supposed to look like this after changing the page title and subtitle and labels of the components
double click the button to handle the on click event
in the on click event , initialize a new object from the service by writing the following code :
private void button1_Click(object sender, RoutedEventArgs e) { TranslatorService.LanguageServiceClient myclient = new TranslatorService.LanguageServiceClient(); }
after this myclient object is the object that i’ll use to call all the service methods
now we are going to call the translateasync function to translate it takes appid , text , from , to , contenttype , category
you can create your own appid or just feel free to use my temp one i’d generated
you can see the function documentation in the service website
private void button1_Click(object sender, RoutedEventArgs e) { TranslatorService.LanguageServiceClient myclient = new TranslatorService.LanguageServiceClient(); myclient.TranslateAsync("5F5CBEB15E4D9847AEB2C93B525EAC9B2A89465", textBox1.Text, "en", "fr", "text/plain", "general"); }
we know that when the service is done with the response a translatecompleted event will be fired so we will add to this event a new event handler with arguments inside the <> and the function name to be executed between the ()
private void button1_Click(object sender, RoutedEventArgs e) { TranslatorService.LanguageServiceClient myclient = new TranslatorService.LanguageServiceClient(); myclient.TranslateAsync("5F5CBEB15E4D9847AEB2C93B525EAC9B2A89465", textBox1.Text, "en", "fr", "text/plain", "general"); myclient.TranslateCompleted += new EventHandler<TranslatorService.TranslateCompletedEventArgs>(translatecompleted); }
- <TranslatorService.TranslateCompletedEventArgs> is the event args to be passed to the invoked function when executed
- tanslatecompleted is then name of the function to be executed of the new event handler
and we will add the function implementation in a separate function
private void translatecompleted(object obj , TranslatorService.TranslateCompletedEventArgs e ) { textBlock1.Text = e.Result.ToString(); }
the object ” e ” of type TranslatorService.TranslateCompletedEventArgs
contains the event arguments and it’s attribute Result contains the results of the invoked method in the webservice
which will be the translated text which then we will convert it to string and display it in the textblock1
the whole file would look like this :
now we are done let’s compile Run and try to see if there will be any bugs
now you are supposed to know:
- what’s the SOA , and webservices , WCF services
- how to build a WCF services
- how to consume a web service in windows phone 7 applcations