MIDI Source: Blog Park Published: 2010-01-01 17:39 read: 204 times original link[Collection] when Silverlight uses WebService, WCF, WebRequest, etc. for data communication, it is often necessary to face cross-domain access issues. Silverlight 2 Beta 1 provides support for team cross-domain access and supports crossdomain.xml policy files like Flash. On the Internet to check some relevant information, found that the policy file content is as follows:
<?xml version= "1.0"?>
<cross-domain-policy>
<!--Http://192.168.0.1/crossdomain.xml--
<allow-access-from domain= "www.aaa.com"/>
<allow-access-from domain= "*.bbb.com"/>
<allow-access-from domain= "192.168.1.1"/>
</cross-domain-policy>
The example above indicates that Silverlight from Www.aaa.com, *.bbb.com, 192.168.1.1 is allowed to access native data (Ftp,http,https mode) across domains.
If you need to allow Silverlight from any domain to access native data, the file contents are as follows:
<?xml version= "1.0"?>
<!--Http://localhost/crossdomain.xml--
<cross-domain-policy>
<allow-access-from domain= "*"/>
</cross-domain-policy>
After you have written the above files, copy the files to the root site of the server.
----------
One: Silverlight cross-Domain
Silverlight has done a lot of thinking about network security when it comes to design, depending on the Silverlight SDK.
Cross-domain communication is a Web service that uses the correct cross-domain policy file in the root deployment of another domain to enable Silverlight-based applications to invoke Web services in that domain. Silverlight supports two types of cross-domain policy files.
· Silverlight Cross-Domain Policy (clientaccesspolicy.xml)
· A subset of Flash cross-domain policies (crossdomain.xml)
Cross-domain communication using cross-domain policy files
Typically, if a Silverlight-based application detects that its request is a cross-domain request, the Silverlight cross-domain Policy file (clientaccesspolicy.xml) is first located at the application root of the Web service. If this request results in a "404 Not Found" or other error, the application looks for the Flash cross-domain Policy file (crossdomain.xml) at the root of the application. Redirection of cross-domain policy files is not allowed. Additionally, the cross-domain policy file remains valid for the application session.
Now you know. As long as you deploy a cross-domain policy file in your Web site, you can resolve Silverlight cross-domain request issues. Where do the cross-domain policy files go?
Note: cross-domain Policy files must also be placed in the root directory only on the site.
clientaccesspolicy.xml configuration :
<?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>
Crossdomain.xml Configuration:
<?xml version= "1.0"?>
<cross-domain-policy>
<allow-access-from domain= "*"/>
</cross-domain-policy>
Two: Silverlight in the IIS Deployment
programs developed with Silverlight, deployed to IIS, will often pop up "Sys.InvalidOperationException:InitializeError error #2104 in control ' XAML1 ': The Silverlight application could not be downloaded. Please review the WEB server settings exception.
The reason for this exception: the XAP in the Silverlight program ClientBin directory and files with XAML suffix names are not recognized by IIS requests.
Workaround: In IIS, add the MIME type.
MIME type
. xaml Application/xaml+xml
. xap Application/x-silverlight-app
If you still have a problem, check the configuration of IE, and let go of the XAML Active download option in security.
To add a MIME type to IIS:
Three: Silverlight dynamically bound Pictures
when the asynchronous request data is dynamically bound with the image tag, the "Sys.InvalidOperationException:ImageError error #4001 in control ' Xaml1" occurs every time the page reloads: ag_ E_network_error "Exception.
The image tag is bound in the following form:
<image x:name= "rect" stretch= "Fill" canvas.left= "" "canvas.top=" "" source= "{Binding productimage}" </image >
The reason for this exception: binding occurs before setting DataContext, so the image path is not set to the value you expect, so the source path of the image tag is not available at this time.
Modify the method code as follows:
Remove source= "{Binding productimage}" in XAML, DataContext bind the data, and then set the binding value on the image tag. For example:
<image x:name= "rect" stretch= "Fill" canvas.left= "canvas.top=" </Image>
View Plaincopy to Clipboardprint?
- private void Responseready (IAsyncResult asyncResult)
- {
- WebRequest request = asyncresult.asyncstate as WebRequest;
- WebResponse response = Request. EndGetResponse (AsyncResult);
- using (Stream Responsestream = response. GetResponseStream ())
- {
- DataContractJsonSerializer Jsonserializer =
- New DataContractJsonSerializer (typeof (ProductList));
- ProductList productlist = Jsonserializer.readobject (responsestream) as productlist;
- New Thread (() =
- {
- Mygrid. Dispatcher.begininvoke (
- () =
- {
- Mygrid. DataContext = Productlist.products[0];
- Binding binding = new binding ("Productimage");
- Rect. SetBinding (image.sourceproperty, binding);
- }
- ); }). Start ();
- }
- }
"Go" Silverlight cross-domain access