为了研究silverlight5的特性,下载了vs2012,测试Silverlight调用WCF,起初basicHttpBinding没问题,跨域解决了;试用net.tcp的时候还是遇到跨域的问题:
Could not connect to net.tcp://localhost:4502/Service1. The connection attempt lasted for a time span of 00:00:00.0550032. TCP error code 10013: 试图以其访问权限所禁止的方式访问套接字。. This could be due to attempting to access a service in a cross-domain way while the service is not configured for cross-domain access. You may need to contact the owner of the service to expose a sockets cross-domain policy over HTTP and host the service in the allowed sockets port range 4502-4534.
起初看到异常信息始终不得其解,后来google了N多资料也没有解决---很多谈的都是IIS Host,而我用的是Self-Host,后来逐字逐句看异消息,发现expose a sockets cross-domain policy over HTTP and host the service这句很有玄机,HTTP难道说的就是默认的80端口?尝试修改WCF的配置文件,将ICrossDomainService中的baseAddress端口设为80,加以辅助修改,测试成功!
ps:测试代码的背景
http://silverlightchina.net/html/tips/2010/1208/4010.html
注意:经测试发现ClientAccessPolicy.xml中grant-to一节写法对于不同的Binding有所区别:
basicHTTPBinding:
<?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>
net.tcpBinding
<?xml version="1.0" encoding="utf-8"?> <access-policy> <cross-domain-access> <policy> <allow-from http-request-headers="*"> <domain uri="*"/> </allow-from> <grant-to> <socket-resource port="4502" protocol="tcp" />
</grant-to> </policy> </cross-domain-access> </access-policy>
浙公网安备 33010602011771号