Suppose you have a nice Console Application. Suppose you want to host a WCF service. And suppose you want to create a fancy Silverlight app that consumes this WCF service.

You’ll soon discover that something isn’t working as expected…long story short, the client app is looking for a ClientAccessPolicy.xml file. And why in the world shouldn’t we provide it?
Here’s what you have to do:

1) Create another WCF Service, call it CrossDomainService

2) Add this method:


[OperationContract]
[WebGet(UriTemplate = "ClientAccessPolicy.xml")]
public Message GetClientAccessPolicy()
{
using (var filestream = File.Open(@"ClientAccessPolicy.xml", FileMode.Open))
{
var buff = new byte[filestream.Length];
filestream.Read(buff, 0, (int)filestream.Length);
filestream.Close();
var stream = new MemoryStream(buff);
return Message.CreateMessage(MessageVersion.None, "", XmlReader.Create(stream));
}
}

3) Add a ClientAccessPolicy.xml file to your project (you may find thousand of examples online…look at this maybe)

4) Add this to your app.config:

<behaviors>
<endpointBehaviors>
<behavior name="CrossDomainServiceBehavior">
<webHttp/>
</behavior>
</endpointBehaviors>
</behaviors>
<services>
<service name="CrossDomainService">
<endpoint address="" behaviorConfiguration="CrossDomainServiceBehavior"
binding="webHttpBinding" contract="CrossDomainService" />
<host>
<baseAddresses>
<add baseAddress="http://localhost:12345/" />
</baseAddresses>
</host>
</service>
</services>

Obviously make sure that the baseAddress has the same root as the WCF Service you want to expose at first 🙂