j-nunn.com

Problems getting Silverlight to communicate with WCF services under IIS7

I have been developing a new application allowing clinicians and nursing staff to record the condition of a patients skin. As part of this they needed a way to mark on a diagram, where any wounds were. This was a perfect chance to use Silverlight. I created the control and then hooked it up to an existing WCF service I have for obtaining patient details. There were a few modifications needed to allow the Silverlight application to access the service, but all went smoothly. The problems started when I moved from hosting the Service in Visual Studio’s Development Server to IIS7.

I kept getting communications errors, after using Fiddler to see what was going on, the application was looking for a file it couldn't find clientaccesspolicy.xml. Searching on the web and on the Silverlight.netforums I found that this is a file needed to allow cross domain access to services. This is a simple little XML file that sits in the root of your web site.

The contents of the file should be:

<?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>

This will allow  a request from all domains to access all services running under this site. To restrict the allowed domains modify the <allow-from> settings as follows:

<allow-from http-request-headers="*">
   <domain uri="*.j-nunn.com"/>
   <domain uri="*.tortrix.net"/>
</allow-from>

This will allow all requests from the j-nunn.com and tortrix.net domains. To restrict which services can be accessed the <grant-to> section can be modified:

<grant-to>
  <resource path="/Services" include-subpaths="true"/>
  <resource path="/PatientData/Service" include-subpaths="false"/>
</grant-to>

This allows all services in the Services directory and and sub directory, and any services in the PatientData/Service directory to be accessed.

The changes needed to allow Silverlight to access the WCF services were to add the following element to the web.config:

<system.serviceModel>
        <serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>

and then to decorate the actual service implementation classes with the following attribute:

[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]

Despite the pain trying to figure out why the solution worked in Visual Studio but not when hosted in IIS, this was a very worthwhile exercise, the application is very easy to use and the users love the interactivity provided via Silverlight.

Post a comment

  1. Formatting options
       
     
     
     
     
       
Copyright © 2010 John Nunn
This site is powered by