Friday, April 16, 2010

Simple WCF service for Win Phone 7 / Silverlight testing

This blog post is meant to briefly illustrate a simple WCF client configuration which returns a CustomerDTO, which you can call and try out from your Win Phone 7, Silverlight, or other client.

The client calls to a simple WCF service on my website, gaddzeit.com, that returns a list of (random-name-generated) customers. The number of rows returned is based on the parameter int howMany (eg. 50 returns 50 CustomerDTO rows). There is a upper limit of 1000 rows returned before a warning exception is thrown.

Do the following:
  • Create a client app (a unit test library, console app, a repository, etc.)
  • Right-click the project and select "Add A Service Reference..."
  • enter the WCF service URL shown below
  • click the Go button
  • once the service is loaded, provide a meaningful name for the namespace (eg. FakeCustomerService)


http://www.gaddzeit.com/FakeCustomerService/


Open the config file (ServiceReferences.ClientConfig | web.config | app.config). The <system.serviceModel> element will have been generated for the WCF service:


<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <system.serviceModel>
        <bindings>
            <basicHttpBinding>
                <binding name="BasicHttpBinding_IGaddzeitServices" maxBufferSize="2147483647"
                    maxReceivedMessageSize="2147483647">
                    <security mode="None" />
                </binding>
            </basicHttpBinding>
        </bindings>
        <client>
            <endpoint address="http://www.gaddzeit.com/FakeCustomerService/FCService.svc"
                binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IGaddzeitServices"
                contract="GaddzeitServices.IGaddzeitServices" name="BasicHttpBinding_IGaddzeitServices" />
        </client>
    </system.serviceModel>
</configuration>


Next, enter a using statement for the web service namespace into:
  • the Repository or Command class (if using MVVM)
  • the code-behind (if responding directly from a control event handler in the presentation layer)

Finally, enter the following code into the above class. This code instantiates the web service, and attaches the GetRandomCustomerNamesCompleted (asynchronous) event to a callback method.
   NOTE Win Phone 7 (and Silverlight) require asynchronous web service calls.


const int howManyCustomersToReturn = 40;
GaddzeitServicesClient webService = new GaddzeitServicesClient();
webService.GetRandomCustomerNamesAsync(howManyCustomersToReturn);
webService.GetRandomCustomerNamesCompleted += new EventHandler(webService_GetRandomCustomerNamesCompleted);


Below is a rough sample of the callback method (in this sample, I am concatenating the customer names to an instance string _customerNames.)

void webService_GetRandomCustomerNamesCompleted(object sender, GetRandomCustomerNamesCompletedEventArgs e)
{
   var customers = new List<CustomerDTO>(e.Result);
    foreach (var customer in customers)
    {
        if (customer == null)
        {
            continue;
        }
        _customerNames += string.Format("{0} {1}\r\n", customer.FirstName, customer.LastName);
    }
}


NOTE You could also add code to track the time on the response by comparing DateTime.Now.Ticks for before and after the web service call.

NOTE If you want to build a complete Win Phone 7 client using TDD, mocks and MVVM, this earlier blog post provides a walk through. In this blog post, the only piece NOT included is a WCF client call (the walkthrough places a comment recommending a WCF client call inside the CustomerRepository.Save() method.)

8 comments:

Raffay said...

this is an amazing post!thanks for sharing!i hope you’ll give more information about! Undoubtedly the perfect write-up! I’ve truly book-marked it and emailed this out to every one of my close friends as I know they’ll be curious, thank you very much!

reseller hosting |reseller web hosting

Nikolai said...

Interesting information, I will look forward for any updates.

Jerico at web hosting reviews.

norman said...

Thanks for shareing! I agree with you. The artical improve me so much! I will come here frequently.
stop aging now
hydrolyze

Marco said...

HI, nice article. Any idea if there is a code example of doing someting like http://channel9.msdn.com/Series/The-Full-Stack/The-Full-Stack-Part-13-Synchronizing-the-Windows-Phone-Client-with-the-WCF-Service

Of could you expand on that?

norman said...

I recently came across your blog and have been reading along. I am so excited that I have found this, this post is really the sweetest on this notable topic.
omaha steaks

jennifer said...

i really like your post .. i will like to bookmark your site for my future needs :)
dissertation writing services
UK dissertation

dustin said...

Your post is very informative. You have written it very well. I will keep on coming here to check new updates. Thanks for sharing this article.
dissertation writing services in uk

Jacob Hubbard said...

This is a nice article. I admire your efforts. I have bookmarked your website. Thanks for sharing.
writing dissertation