Developing, Deploying and Invoking WCF Services from Silverlight 2 Applications

In order for your WCF Service to be invoked from Silverlight you need only change the default Web.Config EndPoint Binding setting from wsHttpBinding to basicHttpBinding:

image

Optionally you can add the AspNetCompatibilityRequirements() attribute to your service class. This allows your WCF service to appear just like an ASMX service. This is useful for your ‘legacy’ AJAX applications.

image

I ran into an issue when deploying my WCF Service to my shared web hosting service provider. I received this error when trying to browse to the SVC file:

This collection already contains an address with scheme http. There can be at most one address per scheme in this collection.
Parameter name: item

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.ArgumentException: This collection already contains an address with scheme http.  There can be at most one address per scheme in this collection.
Parameter name: item

I got the solution from Rob Zelt’s blog. You simply replace the default ServiceHostFactory and ServiceHost classes with a custom implementation. The key line of code is the instantiation of your custom ServiceHost class which takes an argument which is an array of base addresses. By default the 0’th element is passed in. When in a shared hosting environment your domain is listed in the 1’th position.

image

In addition you must specify your custom factory class in the SVC file:

image

Now that our service is implemented and deployed, we are ready to invoke it from Silverlight. Right click on your Silverlight project in Visual Studio 2008 and select ‘Add Service Reference…’. This will bring up the Add Service Reference dialog box:

image

You can enter the Uri to the Svc file or you can instruct the tool to find all the services within your solution. By adding a service reference to your project, you will have a WCF Service proxy generated into your project and your Silverlight project App.Config file will be updated with the necessary WCF configuration settings.

To invoke your service:

  1. create an instance of a BasicHttpBinding class
  2. create an instance of, an EndPointAddress class passing in the Uri to the Svc file
  3. create an instance of your Service Client Proxy passing in the binding and end point objects
  4. add an event handler for the service callback
  5. call the method of interest on the end point

image

To implement the callback, create a method that takes an object and the generated EventArgs class for this callback:

image

The generated EventArgs class will contain the data returned from the service call. In this case what is returned is an Array of type CD.

Now that we have the results from our service call, we can use Silverlight Controls and Data Binding to display the information. That is the topic of Part 3 in this series.