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:
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.
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.
In addition you must specify your custom factory class in the SVC file:
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:
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:
- create an instance of a BasicHttpBinding class
- create an instance of, an EndPointAddress class passing in the Uri to the Svc file
- create an instance of your Service Client Proxy passing in the binding and end point objects
- add an event handler for the service callback
- call the method of interest on the end point
To implement the callback, create a method that takes an object and the generated EventArgs class for this callback:
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.
Comments
Anonymous
April 04, 2008
Rich Internet Applications Rich Internet Applications (RIA’s) combine the maturity, stability andAnonymous
April 04, 2008
Rich Internet Applications Rich Internet Applications (RIA’s) combine the maturity, stability and scalabilityAnonymous
April 04, 2008
Abstract This series of articles takes a look at the current state of RIA development leveraging SilverlightAnonymous
April 07, 2008
Looking for a great, concise, just-get-to-the-point, intro to programming RIA's using Silverlight 2.0?Anonymous
May 15, 2008
I there my WCF service dosen't acept factory as parameter... is it because it dosen't have codebehind? thanks.. RuiAnonymous
May 22, 2008
Not sure. Send me an email with a sample of what you are trying to do. Thanks! -bobAnonymous
May 19, 2009
Thanks so much for the tutorial! I've been using WPF (for some museum kiosks in the Library of Congress, and recently a year of dev on a biz app) and have been looking for a really good example of a RIA / Silverlight app using WCF. This was a sweet find. Cheers Jim