您的位置:首页 > 移动开发 > Objective-C

Silverlight 2 Beta2调用WebService跨域问题解决方案

2008-09-04 15:49 706 查看
最近很多朋友问我Silverlight 2 Beta2调用WebService跨域问题,现在总结整理和大家分享一下

 

问题:

 

用户代码未处理 System.ServiceModel.ProtocolException
  Message="The remote server returned an unexpected response: (404) Not Found."
  StackTrace:
      位于 System.ServiceModel.AsyncResult.End[TAsyncResult](IAsyncResult result)
      位于 System.ServiceModel.Channels.ServiceChannel.SendAsyncResult.End(SendAsyncResult result)
      位于 System.ServiceModel.Channels.ServiceChannel.EndCall(String action, Object[] outs, IAsyncResult result)
      位于 System.ServiceModel.ClientBase`1.ChannelBase`1.EndInvoke(String methodName, Object[] args, IAsyncResult result)
      位于 DeviceQuery.ServiceReference1.DeviceServiceSoapClient.DeviceServiceSoapClientChannel.EndSelectStations(IAsyncResult result)
      位于 DeviceQuery.ServiceReference1.DeviceServiceSoapClient.DeviceQuery.ServiceReference1.DeviceServiceSoap.EndSelectStations(IAsyncResult result)
      位于 DeviceQuery.ServiceReference1.DeviceServiceSoapClient.EndSelectStations(IAsyncResult result)
      位于 DeviceQuery.ServiceReference1.DeviceServiceSoapClient.OnEndSelectStations(IAsyncResult result)
      位于 System.ServiceModel.ClientBase`1.OnAsyncCallCompleted(IAsyncResult result)
  InnerException:

 



 

   在Silverlight 2 beta 2中,访问webservice 或者 wcf 的时候,常常会出现这样的错误:
An exception of type 'System.ServiceModel.ProtocolException' occurred in System.ServiceModel.dll but was not handled in user code
Additional information: The remote server returned an unexpected response: (404) Not Found.
   在地址栏中,直接输入asmx 或者.svc 文件,运行一切正常。
   查了下资料,发现是客户端跨域访问策略文件(clientaccesspolicy.xml)产生的问题。
   正确的文件应该是:




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> 



     但是,很多网友却很迷惑,这个文件在什么地方?或者找到了地方,修改了为什么还是不生效呢?
     原来是clientaccesspolicy.xml 文件的地址不正确,比如我们webservice目录叫做webservice1,引用地址是http://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.asmx 发布到IIS中(http://localhost/webservice1/webservice.asmx),然后再进行调试,这样一般都能解决问题。
 
 
 


 

   写完上一个试验项目,基本上技术上没有什么大的难点了,我马上开始写我计划了好几天的项目。

    不过,刚一开始,就遇到点小麻烦:Silverlight程序访问WCF服务时,调试错误信息如下:

用户代码未处理 System.ServiceModel.ProtocolException
  Message="The remote server returned an unexpected response: (404) Not Found."
  StackTrace:
       位于 System.ServiceModel.AsyncResult.End[TAsyncResult](IAsyncResult result)
       位于 System.ServiceModel.Channels.ServiceChannel.SendAsyncResult.End(SendAsyncResult result)
       位于 System.ServiceModel.Channels.ServiceChannel.EndCall(String action, Object[] outs, IAsyncResult result)
       位于 System.ServiceModel.ClientBase`1.ChannelBase`1.EndInvoke(String methodName, Object[] args, IAsyncResult result)
       位于 TISCS.Bamhut.Paper.BamhutService.BamhutServiceClient.BamhutServiceClientChannel.EndUserLogin(IAsyncResult result)
       位于 TISCS.Bamhut.Paper.BamhutService.BamhutServiceClient.TISCS.Bamhut.Paper.BamhutService.IBamhutService.EndUserLogin(IAsyncResult result)
       位于 TISCS.Bamhut.Paper.BamhutService.BamhutServiceClient.OnEndUserLogin(IAsyncResult result)
       位于 System.ServiceModel.ClientBase`1.OnAsyncCallCompleted(IAsyncResult result)
  InnerException:

    郁闷!到网上找了找资料,参考了一下这两个帖子:

一:http://forums.iis.net/t/1149643.aspx  二:http://www.cnblogs.com/invinboy/archive/2008/06/29/1232037.html

    大概的意思是添加一个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>

    至于这个文件的存放位置,帖子二做了较详细的说明,但我个人认为还不够详细,其实,这个文件,是要放在IIS站点的根目录下的,不是WCF所在目录,也不是Web项目根目录,更不是Silverlight所在的目录,一定要在localhost所在的物理目录!

    问题已解决,解决方案给大家共享一下。以上内容如有错误或疏漏,敬请指正。

 

引用:http://f-lone.spaces.live.com/blog/cns!DDC109DFD23B993D!349.entry

 

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息