You can find Part One HERE
The Windows Phone 7 Application Steps
Firstly, make sure all the Windows Phone 7 development tools are installed and configured correctly on your system. You can go to the following Microsoft site for more information: http://create.msdn.com/en-us/home/getting_started
Using a WCF service on a shared hosting provider turned out to be fairly straightforward except for two things: you have to use basicHttpBinding (do not use wsHttpBinding) and the service call must be implemented with an asynchronous event handler (sounds hard, but it isn’t too bad).
Create a new Windows Phone Application, I called it NumbersApp:
On the default MainPage.xaml add two text boxes (tbNumber and tbResult) and a button control (btnProcess). You can change any other properties of the page as desired:
Now, add a service reference to the previously deployed WCF service from Part 1 (right click on Service References and choose Add Service Reference, enter the URL and click Go. You should see the Process operation available on myservice:
Click on the Advanced… button and make sure the “Generate asynchronous operations” checkbox is checked. Silverlight applications require asynchronous web service operations.
Check the Output and Error List windows after adding the service reference…occasionally there will be errors. I’ve found that EXITING Visual Studio, starting back up, deleting, and re-adding the reference usually fixes these errors.
On the XAML design screen, double click the Process Number button to open the default click handler. We’ll be adding a client reference to the WCF service and an Event Handler for the Process operation. Don’t forget to add a using statement to the service also:
using NumbersApp.myNumberService; namespace NumbersApp { public partial class MainPage : PhoneApplicationPage { public MainPage() { InitializeComponent(); } private void btnProcess_Click(object sender, RoutedEventArgs e) { myNumberService.Service1Client client = new myNumberService.Service1Client(); client.ProcessCompleted += new EventHandler(client_ProcessCompleted); client.ProcessAsync(System.Convert.ToInt16(tbNumber.Text)); } void client_ProcessCompleted(object sender, ProcessCompletedEventArgs e) { if (e.Result != null) { Numbers results = e.Result; tbNumber.Text = "Square is " + results.squaredk__BackingField + " and root is " + results.rootk__BackingField; } } }
This code calls the Process service and sets up an Event Handler which is called when the results are ready from the service. The result is a Numbers object that has properties to access the square and square root of the value. Run the application by pressing F5, enter a number, and verify the hopefully correct results:
Using this technique, you can call your custom WCF service and return any type of serializable object needed for your application. Try it out, and publish any interesting results or tips you discover!
One thought on “WCF Services, Shared Hosting and Windows Phone 7, Part Two”
Comments are closed.