Sunday, July 4, 2010

How to configure WCF Service for your Silverlight application

Hi,

During my work at ECI I've developed a monitoring application for end-users, using Microsoft Silverlight technology.

As you probably know, a Silverlight application or a Silverlight DLL can't access a database using some of the known .NET providers (for example: System.Data.SqlClient, System.Data.OracleClient etc.). In addition, it's not possible to add an ADO.NET Entity Data object. To sum up - it can only use components that are a part of Silverlight runtime.
One of the ways to access a database in that case is by using WCF Services.
Consuming a WCF Service by a Silverlight project is not straight-forward as it is with other project types. Silverlight runtime engine automatically looks for permissions in order to access the service.

I've read many posts and articles about how to configure a WCF Service in order to use it in a Silverlight project, but no post really had a complete list of steps that will actually make this thing work.
After gathering all the relevant information, I've came out with those following steps needs to be done in order to configure accessibility:

1. Place "clientaccesspolicy.xml" and "crossdomain.xml" files at the physical path of your site (hosting the WCF Service Application) in the IIS:


2. The client-access-policy file, looks as follows:


This is an XML document that grants a web client permission to handle data across multiple domains, and can also specify access to directories in the server.

3. The cross-domain file, looks as follows:


This file is an XML document that grants a web client permission to handle data across multiple domains. It also used to grant access for Flash.


The question is, why do we need two files to define permissions to our WCF Service ?
Well, actually we don't. I've used "Fiddler" sniffer to dive into the following situations.
- If "clientaccesspolicy.xml" file exists, our silverlight application uses it to check for permissions:

"clientaccesspolicy.xml" allows the domain to access the service:


"clientaccesspolicy.xml" doesn't allow the domain to access the service (HTTPRequest to 'DatabaseService.svc' is not shown because the domain has insufficient permissions):


- If "clientaccesspolicy.xml" file doesn't exist, our silverlight application looks for "crossdomain.xml" to check for permissions to access the service:

"crossdomain.xml" allows the domain to access the service:


- If both "clientaccesspolicy.xml" and "crossdomain.xml" files doesn't exist, our silverlight application can't access the service:

Thank you Blogger, hello Medium

Hey guys, I've been writing in Blogger for almost 10 years this is a time to move on. I'm happy to announce my new blog at Med...