在silverlight 中调用webService的webMethod
在结束这个方法 public Ods.Application.ServiceReference1.User EndGetUserByID(System.IAsyncResult result) 时(GetUserByID是我定义的Web方法)出现The remote server returned an unexpected response: (404) Not Found.
查了一下资料,出现此问题的原因是,客户端跨域访问策略文件(clientaccesspolicy.xml)产生的问题clientaccesspolicy.xml 文件的地址不正确.
比如我们webservice目录叫做webservice1,引用地址是p://localhost/webservice1/webservice.asmx,而silverlight不是在http://localhost/webservice1这个目录下找clientaccesspolicy.xml,而是在http://localhost/下找这个文件.所以假如你的IIS默认目是"c:/inetpub/wwwroot"的话,webservice1/webservice.asmx是部署在“c:/inetpub/wwwroot/webservice1”下,那么clientaccesspolicy.xml 就应该放在“c:/inetpub/wwwroot”而不是c:/inetpub/wwwroot/webservice1 下面。但是VS2008 在调试状态下运行的时候,其URL通常是http://localhost/:端口号/webservice.asmx ,这种情况下可以直接将clientaccesspolicy.xml 文件放到你的webservice项目文件的根目录下。
附clientaccesspolicy.xml 文件:
<?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>