This one had me stuck for hours earlier this week, and judging by the number of posts on forums plenty of other people are suffering the same, so I hope this little titbit saves someone the frustration I suffered.
I am using Visual Studio 2008 to create a Silverlight 2.0 (beta 2 at the moment) application. In my solution I have two web site projects, one for hosting the application, the other to host WCF services. All I want is to hit F5 and have both sites run in debug mode and my SL app call my WCF services and work. So I set my projects up as I believed they should be via the properties sheet for the project root as follows.
- Disabled dynamic ports so I know the fixed address of the WCF
- Remove the virtual directory name to make both root applications
- Add the following clientaccesspolicy.xml file to the root of the WCF site (wide open for now)
<?xml version="1.0" encoding="utf-8"?>
<access-policy>
<cross-domain-access>
<policy>
<allow-from>
<domain uri="*"/>
</allow-from>
<grant-to>
<resource path="/"
include-subpaths="true"/>
</grant-to>
</policy>
</cross-domain-access>
</access-policy>
So having written the code for my SL app to call my WCF service, references and all that stuff working fine I expect it all to work, I hit F5 in VS 2008 and both sites run, I can browse to the .svc file and get a working web page telling me how to create a client but in my SL application I get an error when I hit the button to call the service.
WTF? double check, triple check, hours of googling to make sure I have done it right, that my clientaccesspolicy.xml file is right still not working. I read hundreds of blog/forum posts and articles and still no clue. Many talked about problems with WebDev.WebServer.exe not having a root to place the policy file in but I had that covered you can change it in the properties sheet for the project. Still no luck.
Just when I was about to give up and wire my project up to IIS I re-read an article that I had spotted earlier about "Self Hosting" WCF services in a Windows Service and what was required in the policy file to support this. Light bulb moment, WebDev.WebServer.exe may not be a Windows Service but it is effectively a "Self Hosting Process", a quick change to my clientaccesspolicy.xml file and bingo it works. What did I change? Look below at the attribute in bold on the <allow-from> element.
<?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>
So make this note in your little black book:
When it comes to web services (WCF or ASMX) WebDev.WebServer.exe is a Self Hosting Process and any requirements for self hosting apply.