This is an old problem, and I also encountered it, that is, providing services through WCF,SilverlightConsume the service in the previous section. If the address provided by WCF is http:/localhost: 8839/, and the port running by Silverlight is inconsistent, for example, http: // localhost: 7777/, it is not in a domain, cross-origin access occurs. By defaultWebCross-origin is blocked by security.
I checked it online. The main solution is to add a clientacesspolicy. xml file to the root path of WCF. TheXMLThe content is
<? 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> |
However, the addition does not work. I checked it online to provide it as a Service. Later, based on the wxwinter example, this example can beGoogleAdd a service to solve the problem.
The Service Interface of this service is as follows:
[Servicecontract] Public interface icrosdomainservice { [Operationcontract] [Webget (uritemplate = "clientaccesspolicy. xml")] Message providepolicyfile (); } |
Specific services:
Public class crossdomainservice: icrosdomainservice { Public System. servicemodel. channels. Message providepolicyfile () { Filestream = file. Open (@ "cross-origin access \ clientacesspolicy. xml", filemode. Open, fileaccess. Read, fileshare. readwrite); // access the XML address here Xmlreader reader = xmlreader. Create (filestream ); System. servicemodel. channels. Message result = message. createmessage (messageversion. None, "", Reader ); System. Console. writeline ("it start ..."); Return result; } } |
Default questions about app Configuration
<? XML version = "1.0" encoding = "UTF-8"?> <Configuration> <! -- When deploying a service library project, you must add the configuration file content In the app. config file of the host. System. configuration does not support database configuration files. --> <System. servicemodel> <Servicehostingenvironment aspnetcompatibilityenabled = "true"/> <Behaviors> <Endpointbehaviors> <Behavior. Name = "crossdomainservice. crossdomainservicebehavior"> <Webhttp/>
</Behavior> </Endpointbehaviors> <Servicebehaviors> </Servicebehaviors> </Behaviors> <Services> <Service name = "crossdomainservice. crossdomainservice"> <Endpoint address = "" behaviorconfiguration = "crossdomainservice. crossdomainservicebehavior" Binding = "webhttpbinding" Contract = "crossdomainservice. icrosdomainservice"/> <Host> <Baseaddresses>
<Add baseaddress = "http: // localhost: 7656/"/> <! --- Baseaddress: the address that you want to provide cross-origin access --> </Baseaddresses> </Host> </Service> </Services> </System. servicemodel> </Configuration> |
After you add this service to the WCF Service, the domain http: // localhost: 7656/can provide cross-origin access.
Check whether it is successful. You can view http: // localhost: 7656/clientacesspolicy. XML to see whether the XML document is displayed.
TheSource codeIn the attachment, change crossdomainservice.gifto crossdomainservice.rar.
Http://space.itpub.net/22259926/viewspace-627039#xspace-itemreply