I recently began getting my feet wet with the WPF and WCF frameworks. As with any new development technology I am learning I like to actually build something useful. Currently I am using the WebGuide Windows Home Server (WHS) plugin for remote access to my media collection. While this is a decent plugin, I thought it would be cool to try and write a really slick WPF front-end with some eye candy. To this end I decided to develop my own WHS media plugin implemented as a XAML browser application (XBAP) that communicates with the server via a WCF service. As of .NET 3.5, WCF communication from a partial trust application is now possible with some limitations (http://msdn2.microsoft.com/en-us/library/bb412186.aspx).
Almost immediately I ran into a problem that prevented me from debugging the application in Visual Studio 2008. Whenever the application attempted to connect to the WCF service I received the following error:
Request for the permission of type 'System.Net.WebPermission, System, Version=22.214.171.124, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed.
The possible reasons for this error are numerous but in this case it was because the default Debug->Start Action for an XBAP project is "Start browser in URL", where the URL simply points to the .xbap file on disk (e.g. C:\projects\myproject\bin\debug\myproject.xbap). For a partial trust XBAP application to communicate with a WCF service it must be deployed from the same domain and port as the WCF service. The way to solve this dilemma is to fake the XBAP URL by starting the XBAP application with PresentationHost.exe and supplying the XBAP URL (copied from the "Start browser with URL" value) via the debug parameter and the WCF service URL via the debugSecurityZoneURL parameter. For example the debugging options for your XBAP project should look something like this:
Start Action->Start external program = %windir%system32\PresentationHost.exe
Start Options->Command line arguments = -debug "c:\projects\myproject\bin\debug\MyProject.xbap" -debugSecurityZoneUrl "http://localhost:2022"