WCF + Console Application

2011, May 12    

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:

<br /> [OperationContract]<br /> [WebGet(UriTemplate = "ClientAccessPolicy.xml")]<br /> public Message GetClientAccessPolicy()<br /> {<br /> using (var filestream = File.Open(@"ClientAccessPolicy.xml", FileMode.Open))<br /> {<br /> var buff = new byte[filestream.Length];<br /> filestream.Read(buff, 0, (int)filestream.Length);<br /> filestream.Close();<br /> var stream = new MemoryStream(buff);<br /> return Message.CreateMessage(MessageVersion.None, "", XmlReader.Create(stream));<br /> }<br /> }<br />
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>
` `


<endpoint address="" behaviorConfiguration="CrossDomainServiceBehavior"
binding="webHttpBinding" contract="CrossDomainService" />






</services>`

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