使用Silverlight来调用WCF服务出现SecurityError,信息如下:
错误信息开头是这样的"An error occurred while trying to make a request to URI 'http://localhost:8005/Service1.svc'. This could be due to a cross domain configuration error. Please see the inner exception for more details."这时,应该检查一下WCF服务的根目录下是否有clientaccesspolicy.xml, crossdomain.xml两个文件,如果没有,则需要添加。
如果项目类型是WCF Service Application
也就是说,当启动服务项目时,会启动ASP.NET Web Development Server的那种项目。那直接在项目根目录添加这两个文件即可。这样的项目最后在IIS中运行,由于上述两个文件必须直接在http://domainname/下,也就是说,这个项目最后必须单独作为一个网站跑在一个域名下,而不能作为虚拟目录跑在其他网站下级。或者,把这两个文件添加到要运行的根网站目录下也是可以的。详细信息,可以参考这篇文章。
文件内容:
clientaccesspolicy.xml

Code
[http://www.xueit.com]
<?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>
crossdomain.xml
<!DOCTYPE cross-domain-policy SYSTEM "http://www.adobe.com/xml/dtds/cross-domain-policy.dtd">
<cross-domain-policy>
<allow-http-request-headers-from domain="*" headers="*"/>
</cross-domain-policy>
如果服务是Self Hosted Service
即是说,服务最后不是由IIS运行,而是使用桌面应用程序或者Windows Service通过ServiceHost类来运行的。这样的话,就稍微麻烦一些。服务必须有一个service使用basicHttpBinding跑在某个根地址下,并且提供上面两个文件的内容。具体方法,可以参考这篇文章。如果启动时遇到WCF Error: HTTP could not register URL http://+:xxxx/... Your process does not have access rights to this namespace,可以参考以下的解决办法:
启动WCF服务时出现WCF Error: HTTP could not register URL http://+:xxxx/... Your process does not have access rights to this namespace
1、此错误原因是WCF服务选择的endpoint中指定的地址没有预留(reserved),MSDN上这篇文章详细描述了问题和解决方法。简单地说有以下几种办法:
使用VS自动生成的地址,一般是http://localhost:xxxx/Design_Time_Addresses/ (xxx为端口号),只要是以这个地址开头的都可以正常使用。
2、有些情况下,不能使用这样的地址,比如要在域名根目录下发布一些文件(clientaccesspolicy.xml, crossdomain.xml);又或者要发布了,必须改用一个合适的地址。那么可以自己来预留(reserve)地址,在Vista以上的系统,以管理员身份运行:
netsh http add urlacl url=http://+:80/MyUri user=DOMAIN\user
注意如果要预留根,如http://+:80/则最后的"/"是不可少的。
精彩图集