Silverlight 2.0 and WCF

Having had some time to digest all the news from MIX08 I am working on converting a Silverlight 1.1 project to Silverlight 2.0. One of the major changes is to move from ASMX services to WCF services. Now, that cross-domain access is possible and with WCF inherently being more complex there are a lot of failure points.

So, having spend probably a total of 8 hours on updating the structure of the new project and deploying it to a production environment I thought I'd share this with you so you don't have to spend that much time. Since most of the time spent had to do with deployment and WCF configurations I will focus on that.

The 3 main challenges I needed to figure out were...

  1. Windows authentication versus Anonymous authentication. This is solely WCF and doesn't have to do with Silverlight directly.
  2. Cross-domain access. This is important when troubleshooting your service, which has been deployed to production, and referencing it within your local Visual Studio Silverlight project.
  3. Compatibility. Again, WCF only configuration.

1. Windows vs. Anonymous authentication.

By default, WCF requires you to have your IIS virtual directly configured to be accessible anonymously. However, if you do some kind of domain related stuff you need your IIS virtual directly to be configured to be Windows Authentication. Here is the code to update your web.config file of the WCF web application.




                <binding name="MyBinding">

                    <security mode="TransportCredentialOnly">

                        <transport clientCredentialType="Windows" />







                <behavior name="DesignDBServiceBehavior">

                    <serviceMetadata httpGetEnabled="true"/>

                    <serviceDebug includeExceptionDetailInFaults="false"/>





            <service behaviorConfiguration="DesignDBServiceBehavior" name="DesignDBService">









        <serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>


2. Cross-domain access.

I mainly just needed to get this working because I tried to debug my local SL project having referenced and external cross-domain WCF service. In order to get this working you need to put a "clienataccesspolicy.xml" in the root of your IIS server. This is usually at C:\inetpub\wwwroot. If the file is there and granted the file allows your local domain of your SL project access, Silverlight will automatically acknowledge the file and give access. So, there is nothing you need to do within Silverlight. HOWEVER, MAKE SURE TO RESTART IIS. THE FILE IS NOT RECOGNIZED RIGHT AWAY.

<?xml version="1.0" encoding="utf-8"?>





        <domain uri="*"/>



        <resource path="/" include-subpaths="true"/>





3. Compatibility

This involves two steps

1. Change to the web.config file of the WCF web application.

<serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>

   add this line into the root of <system.serviceModel>.

2. Adding of a class property of the service class, not the service interface.

using System;
using System.ServiceModel;
using System.Configuration;
using System.Web;
using System.Web.Configuration;
using System.ServiceModel.Configuration;
using System.Text;
using System.Security.Principal;
using System.ServiceModel.Activation;
using System.Collections.Generic;

using System.Linq;

[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class DesignDBService : IDesignDBService

Published Tuesday, March 18, 2008 11:01 PM by kemaltolga


No Comments