When using SilverLight 4.0 along with WCF, many are often trying to figure out why you are getting an error as such:
An error occurred while trying to make a request to URI ‘http://localhost:8732/Design_Time_Addresses/MyService/Service1/’</a>. This could be due to attempting to access a service in a cross-domain way without a proper cross-domain policy in place, or a policy that is unsuitable for SOAP services. You may need to contact the owner of the service to publish a cross-domain policy file and to ensure it allows SOAP-related HTTP headers to be sent. This error may also be caused by using internal types in the web service proxy without using the InternalsVisibleToAttribute attribute. Please see the inner exception for more details.
Well, for me, I got that error, but I was able to resolve it rather quickly.
If you are like me, you will create your solution including a Web Service Layer (WCF), a Data Access Layer, Presentation Layer (SilverLight), and even more layers…
…To put it simple, you can’t call your WCF Service unless it is deployed to an IIS Server because you must have a "Cross Domain Policy” in place. And to add a cross domain policy you must add a file to your IIS Root path.
So in short, as far as I can tell, you can’t debug your WCF service and your SilverLight application very well. So this is where writing a bunch of unit tests will come in very well with your web service.
I hope that I have explained the problem and the solution well enough above, below I will take you trough a small walk through below so you can see how I was able to accomplish the task as described above.
Create a whole new solution in Visual Studio 2010. Select SilverLight, SilverLight Application, and provide a name. Then select OK.
The next part is up to you, we simply have no need to have a separate host for the SilverLight application. But if you would like one created, you are more than welcome to at this point. For my demo, I will not, I will deselect it, ensure I have SilverLight 4 selected, then click OK.
Add a new project to your solution.
Select WCF, and WCF Service Library, I called my project MyService, select OK
Rename your IService1 to something else by refactoring it. You can do so with F2, or you can do a Control+H (I usually end up going this route)
If you did the F2 route (The refactoring route), you can select the new name, add search in comments, and click OK.
Check out your changes, then click Apply
Create your first Operation Contract as shown below…
Be sure to change your web service and interface file names.
Repeat the refactoring for the service application as well…
Important Step: Be sure to change wsHttpBinding to basicHttpBinding in your app.config file. This step is 100% required because SilverLight does not allow any other type of binding.
Next add a few controls as shown below to your WCF application
Double click your Call Service button to access the code behind
Add the following code…
Important Step: This step is the step that requires your WCF application to be hosted when building your SilverLight application.
Create a directory in your wwwroot folder as I did below…
In the WCFHOST directory create two new XML files as shown below…
<?xml version="1.0" encoding="utf-8" ?>
<resource path="/" include-subpaths="true"/>
<!DOCTYPE cross-domain-policy SYSTEM "http://www.macromedia.com/xml/dtds/cross-domain-policy.dtd"</a>
<allow-http-request-headers-from domain="*" headers="*"/>
In IIS create a new web site calling it wcfhost as shown below… I did this so that I can use port 88 as SharePoint happens to be installed on my 80 port.
You will see the new site as shown below…
Now its time to deploy your new web service… Right click on MyService and click Publish…
Select your new web service site, then click publish.
Back in IIS, select your new web site and then refresh
Right click on your new MyService then select “Convert to Application”, this will enable it to run as an application
Be sure the application pool for the wcfhost site is actually .NET Framework 4.0. This by default is 2.0, and it will not work.
Now test your WCF web service application…
Now right click on Service References in your SilverLight application, select Add Service Reference…
Select your new service location URL, then click GO to select your service, enter a namespace as shown below, then click OK
Hit F5 to run the SilverLight application, once you do this, you will be presented with this wonderful dialog. Well, the web service is not hosted within the project, but it is now hosted, so even though the message box below does indicate it will not work, it will… …Click Yes…
Enter your name, then click Call Service, cross your fingers…
And hopefully you will be presented with a dialog box as you indicated with the code above to show when it gets a string from the web service.
I hope this posting helped if you found it from your favorite search engine.