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.)