Silverlight accessing WCF serivce throws Crossdomain error

7.3k views Asked by At

I understand this question has been answered many times but i could not resolve it for some reason. I hope some one can solve my problem which might be straightforward for many, but i some how couldnt figure it out as I am relatively new to silverlight and web. I have tried all the possible samples available in the internet on cross domain errors but couldnt fix it. I appreciate if any once can help me on this issue i am facing.

I am accessing WCF service from Silverlight 4 client. I have Clientacccesspolicy.xml and Crossdomain.xml in the wwwroot.

I can access my file by using [http://localhost/Remoteapp.html]. But i am getting cross domain error inspite of having the Clientaccesspolicy.xml file in the root, when the application tries to make a webservice call.

In the webdevelopementhelper i can see that the clientaccesspolicy is being requested at the wcfservice port which is [http//localhost:600061/clientaccesspolicy.xml], which is where my service is located and i am getting a 502 response[Connection failed].

When I type [http://localhost/Clientaccesspolicy.xml] in the browser i can locate the file. But silverlight is requesting the policy file at a wrong location.

Every thing works properly in the design time, but when i deploy it to IIS i am getting this error.

Can any one help me how to resolve this issue? Thanks to every one in advance.

4

There are 4 answers

1
Sajeetharan On

Step 1: check that you have a clientaccesspolicy.xml file or crossdomain.xml file on the WCF service host.

The following clientaccesspolicy.xml

<?xml version="1.0" encoding="utf-8"?>
<access-policy>
  <cross-domain-access>
    <policy>
      <allow-from http-request-headers="SOAPAction">
        <domain uri="*"/>
      </allow-from>
      <grant-to>
        <resource path="/" include-subpaths="true"/>
      </grant-to>
    </policy>
  </cross-domain-access>
</access-policy>

A similar crossdomain.xml file would be:

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

if that does not work, try these steps

  1. On the server where the silver light application is deployed , typically in the ClientBin folder of the ASP.NET application, rename the silverlight application file *.xap to *.zip

  2. Extract the contents of the zip file

  3. Edit the ServiceReferences.ClientConfig file

  4. Update the end point address from localhost to the server address where the WCF service is hosted.

  5. Save the file. Zip the contents and rename back to .xap

0
Sunil Khalas On

Step 1: Put crossdomain.xml with the following code into your web service hosting folder.

<?xml version="1.0" ?>
<cross-domain-policy>
    <site-control permitted-cross-domain-policies="master-only"/>
    <allow-access-from domain="*"/>
    <allow-http-request-headers-from domain="*" headers="*"/>
</cross-domain-policy>

Step 2: Put the same file in your silverlight project also when you add reference of above WCF webservice.

Step 3: Update your reference and publish silverlight project.

Try these steps. It will work for sure.

0
KHV On

I have also faced the same issue and after a week of my trails got to know that having ClientAcessPolicy.xml and CrossDomainPolicy.xml in the root directory will not serve u r request, the ClientAcessPolicy.xml and CrossDomianPolicy.xml must be sent through the service only.

Follow the below steps to solve this issue

  1. Add a new NameSpace in Iservice1.cs as shown below

    [ServiceContract(Namespace = "http://ServiceWCF")]
    
    public interface IPolicyRetriever
     {
       [OperationContract, WebGet(UriTemplate = "/clientaccesspolicy.xml")]
    
       Stream GetSilverlightPolicy();
    
       [OperationContract, WebGet(UriTemplate = "/crossdomain.xml")]
    
       Stream GetFlashPolicy();
     };
    
  2. Now edit the Service1.svc file with edits,

    public class PolicyClass : IPolicyRetriever
    {
        Stream StringToStream(string result)
        {
            WebOperationContext.Current.OutgoingResponse.ContentType = "application/xml";
            return new MemoryStream(Encoding.UTF8.GetBytes(result));
        }
        public Stream GetSilverlightPolicy()
        {
            string result = @"<?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>";
            return StringToStream(result);
        }
        public Stream GetFlashPolicy()
        {
            string result = @"<?xml version=""1.0""?>
                            <!DOCTYPE cross-domain-policySYSTEM""http://www.macromedia.com/xml/dtds/cross-domain-policy.dtd"">
                            <cross-domain-policy>
                                <allow-access-from domain=""*"" />
                            </cross-domain-policy>";
            return StringToStream(result);
        }
    }
    
  3. Now Add both the file in the Project Location

  4. Better to avoid some of the problems we can add both the file in the Root Folder also
0
KHV On

These are some of the changes that have to be made in the Internet Explorer also, after the service is up and running now test in the browser whether the ClientAccesspolicy.xml and Crossdoaminpolicy.xml is accessible from Internet explorer. After all this is done the Microsoft Azure VM link must be added as a trusted site or domain in the browser

  1. First navigate to Internet Explorer-> Internet Options ->Security ->Local Intranet-> Sites

Now check the Check box for Automatically detect intranet network click on Advanced and add the respective Site URL of the VM

  1. Internet Explorer-> Internet Options ->Security ->Trusted Site, add the SP2013 URL which as an added URL

Now in the same window click on the Custom Level Enable all .Net Framework, Active X Controls, Enable .Net Framework Setup

  1. Now move to the advanced Tab and uncheck the Disable the Script Debugging(Internet Explorer), Disable the Script Debugging(others)

In the same Tab we also need to enable some of the options and they are Allow Active Contents from CD's to run on my computer, Allow Active Controls to run in files on my Computer, Enable Native XMLHTTP Support.

After all these are done check weather the Sliverlight application is running properly. For sure if these steps are handles correctly the application will run perfectly.