If you create a standard WCF service it does not work with Silverlight, it needs a few tweaks to get it to work. First is that Silverlight only supports connecting to a basicHTTP service and not a wsHTTP service, so you need to enable a second service or change your primary to basicHTTP, you can find out the reasons why by reading: Accessing SOAP Services
Second you need to attribute your class with: [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]. For example:
1: namespace SilverlightApplication1Web
2: {
3: [ServiceContract(Namespace = "")]
4: [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
5: public class CustomerService
6: {
7: [OperationContract]
8: public int CountUsers()
9: {
10: return 2;
11: }
The third tweak is the biggest: Normally your WCF service and Silverlight application do not sit in the web application (either in Visual Studio or on the server) and due to the security put in place to prevent cross site attacks your service calls will fail. Obviously creating a web application with everything in is a solution, but if you haven’t you must add a security file to the WCF service web application. There are two files you can create in the root of your website, the first one is a crossdomain.xml. Crossdomain.xml is a format created by Macromedia. I do not recommend this one for Silverlight scenarios as Silverlight only supports a subset of the functionality but if you need to deal with Flash based clients then this is the route you must follow. Your other option is the Microsoft way, which is to create a file called clientaccesspolicy.xml. A clientaccesspolicy.xml to allow all methods, from all clients, to all URLS looks like this:
<?xml version="1.0" encoding="utf-8"?>
<access-policy>
<cross-domain-access>
<policy>
<allow-from http-request-headers="*">
<domain uri="*"/>
</allow-from>
<grant-to>
<resource path="/" include-subpaths="true"/>
</grant-to>
</policy>
</cross-domain-access>
</access-policy>
That is not normally what you want to do in production because of the security issues, but for early development it can help. Note: You can use BOTH files to get Silverlight and Flash clients to have the best experience. More details on both those files can be found at: http://msdn.microsoft.com/en-us/library/cc197955(VS.95).aspx
The last tip is the usage of WCF in Silverlight. It is still WCF so you are required to open and close your service connection. However since Silverlight makes web calls using async methods you need to chain up commands using events. i.e. you would create a event handler for when the connection is open and then open the connection. In the open event handler you would do the work, and once that completes you close the connection.
Chaining event handlers for getting Silverlight + WCF in code and program flow.
Special thanks to Herman (the delegator) and Willy for prompting me to write this post… especially since Herman won’t blog on this.