Getting Started with ASP.NET MVC - Part 5: How to do programming with razor syntax
First Time? You can support us by signing up. It takes only 5 seconds. Click here to sign up. If you already have an account, click here to login.


Upload Image Close it
Select File

Browse by Tags · View All
BRH 48
#DOTNET 34
#ASP.NET 29
jQuery 22
ASP.NET 20
.NET 20
WPF 9
jquery interview questions 9
jquery faq 8
ASP.NET4 8

Archive · View All
February 2011 10
September 2011 4
August 2011 4
July 2011 4
May 2011 4
April 2011 4
March 2011 4
October 2011 4
June 2011 4
January 2011 4

Cross Domain Issue - Case Study in Silverlight

Jan 8 2011 4:30PM by Hima   

Introduction

While working with Silverlight applications, calling a WCF Service from Silverlight client, you might have faced cross domain issues. I would like to demonstrate the problem, root cause and solution for the issue.  I have created a WCF Service a separate project and tried to consume the service in Silverlight project. To learn how to consume WCF Service in Silverlight project please refer to this blog post. All the service calls from the Silverlight client are asynchronous.

Problem

I have Referred the project using add service reference, and written the code to call the service methods asynchronously in Silverlight client. Able to build successfully but when I ran the application I got the following error on Debug.

The error details are below:

An error occurred while trying to make a request to URI 'http://localhost:5050/Labcalls/ AudioRecorder.Web /Audiorecorder.svc'. 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. Please see the inner exception for more detail.

Cause

The WCF service and the website hosting the Silverlight app have a different port number!  So this is called cross domain, issue since they have different port number silverlight considers it as different domain.  So the silverlight application is expecting the website to use the same port number that service  uses. It is not accepting the urls from other domains. This is due to Security restrictions in silverlight

Work Around ?

To fix this we need to have same port number for both Web Application and WCF Application.   So Create WCF Service  in the same web application project.

In other words if we need to have the service and client on the same domain in Visual Studio, we need to add WCF Service to the same Web Application project.

This can be done by right-click the Web Application that gets created when you create a Silverilght application, - >Add new Item-> Select Silverlight enabled WCF Service. This will ensure automatically that service and the application stay on the same domain.

silverlight-WCF 

Otherwise we need to give the port number of web application and service.

Solution

I have my service in different domain from application, how will I solve this issue?

The solution is simple. We need to have ClientAccessPolicy.xml and Crossdomain.xml in the root folder of the website.

Important thing is we need to have these files under the root folder of the website to make the service accessible across cross domain

You should be able to access these files

http://localhost:3126//CrossDomain.xml

ClientAccessPolicy.xml

<?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>
 

This helps in granting access across the board with no restrictions.

Cross Domain.xml

<?xml version="1.0"?>
<!DOCTYPE cross-domain-policy SYSTEM "http://www.macromedia.com/xml/dtds/cross-domain-policy.dtd">
<cross-domain-policy>
  <allow-http-request-headers-from domain="*" headers="*"/>
</cross-domain-policy>

Then your issue resolved, now you have access to the WCF Service and they are getting called with out any problems.

Tags: BRH, .NET, #DOTNET, Hima Vejella, ASP.NET4, #ASP.NET, .NET4, Silverlight4, WCF, Silverlight, CrossDomain access, clientaccesspolicy,


Hima
26 · 7% · 1504
0
Liked
 
0
Lifesaver
 
0
Refreshed
 
0
Learned
 
0
Incorrect



Submit

2  Comments  

  • Hi, I am using wcf service library and calling it through silverlight and tried the above solution and tried to change the port numbers also.but still getting the same issue can you let me know the solution when we are using wcf service libraryl.

    commented on May 26 2011 6:48AM  .  Report Abuse This post is not formatted correctly
    Sayali
    2298 · 0% · 2
  • Try using debugging tools like fiddler and try to find out the cause. Once you know the problem why its caused then its easy to find the solution

    commented on Jul 5 2011 7:11AM  .  Report Abuse This post is not formatted correctly
    Hima
    26 · 7% · 1504

Your Comment


Sign Up or Login to post a comment.

    Copyright © Beyondrelational.com Contact us      Privacy Policy      Terms of use      Report Abuse      Advertising