首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > Web Service >

碰到个头疼无比的silverlight跨域访问WCF的有关问题,MSDN逛了好几圈还是没找到解决方案

2012-02-09 
碰到个头疼无比的silverlight跨域访问WCF的问题,MSDN逛了好几圈还是没找到解决方案先把WCF的程序贴出来WCF

碰到个头疼无比的silverlight跨域访问WCF的问题,MSDN逛了好几圈还是没找到解决方案
先把WCF的程序贴出来

WCF中的SLService.svc代码放出来

C# code
 public class SLService : SL.WCF.IServices.ISLService    {        private SL.WCF.SQLProvide.SQLHelp help = new SQLProvide.SQLHelp();        public List<Book> PreviewAllBooks()        {            List<Book> bookLibrary = new List<Book>();            DataTable ds = help.GetBooksInfo();            foreach (DataRow dr in ds.Rows)            {                bookLibrary.Add(new Book { ID = Guid.Parse(dr[0].ToString()), Name = dr[1].ToString(), Count = int.Parse(dr[2].ToString()) });            }            return bookLibrary;        }        public string AddNewBook(Book b)        {            int result = help.AddBook(b.ID, b.Name);            switch (result)            {                case 0:                    return "Fail to add new book: " + b.Name;                case 1:                    return "You have added a new book: " + b.Name;                default:                    return "Unexpect Error: " + result.ToString();            }        }        public string BuyBooks(Book b, int count)        {            int result = help.BuyBook(b.ID, count);            switch (result)            {                case 0:                    return "Fail to buy book: " + b.Name;                case 1:                    return "You have buy " + result.ToString() + " books " + b.Name;                case 2:                    return "You don't select any book to buy";                default:                    return "Unexpect Error: " + result.ToString();            }        }

这个是wcf的clientaccesspolicy.xml
XML code
<?xml version="1.0" encoding="utf-8"?><access-policy>  <cross-domain-access>    <policy>      <allow-from http-methods="*" http-request-headers="*">        <!--http-request-headers="SOAPAction"-->        <domain uri="*"/>      </allow-from>      <grant-to>        <resource path="/" include-subpaths="true"/>      </grant-to>    </policy>  </cross-domain-access></access-policy>

这个是wcf的crossdomain.xml
XML code
<?xml version="1.0" encoding="utf-8" ?><cross-domain-policy>  <allow-access-from domain="*" headers="*"/></cross-domain-policy>



下面是SL程序添加了wcf web reference后的代码
C# code
 public partial class DataValidation : UserControl    {        private SLServiceClient ssc = null;        public DataValidation()        {            InitializeComponent();            if (ssc == null)            {                ssc = new SLServiceClient();                ssc.PreviewAllBooksCompleted += new EventHandler<PreviewAllBooksCompletedEventArgs>(ssc_PreviewAllBooksCompleted);            }        }         private void UserControl_Loaded(object sender, RoutedEventArgs e)        {            ssc.PreviewAllBooksAsync();        }        void ssc_PreviewAllBooksCompleted(object sender, PreviewAllBooksCompletedEventArgs e)        {            if (!e.Cancelled && e.Error == null)            {                this.dgView.DataContext = e.Result;                this.dgView.ItemsSource = e.Result;            }        }}

这个是sl程序添加了web reference后生成的的ServiceReferences.ClientConfig


XML code
<configuration>    <system.serviceModel>        <bindings>            <basicHttpBinding>                <binding name="BasicHttpBinding_ISLService" maxBufferSize="2147483647"                    maxReceivedMessageSize="2147483647">                    <security mode="None" />                </binding>            </basicHttpBinding>        </bindings>        <client>            <endpoint address="http://localhost:27860/SLService.svc" binding="basicHttpBinding"                bindingConfiguration="BasicHttpBinding_ISLService" contract="BookService.ISLService"                name="BasicHttpBinding_ISLService" />        </client>    </system.serviceModel></configuration>



C# code
 ssc.PreviewAllBooksAsync();


执行完后就出现那个
This could be due to attempting to access a service in a cross-domain way without a proper cross-domain policy in place, or a policy that is unsuitable for SOAP services. You may need to contact the owner of the service to publish a cross-domain policy file and to ensure it allows SOAP-related HTTP headers to be sent. This error may also be caused by using internal types in the web service proxy without using the InternalsVisibleToAttribute attribute. Please see the inner exception for more details
的exception

我搞了2天还没搞定,求专家赐教

[解决办法]
这些文件需要放在要调用的服务器网站的根目录下,不是工程或者虚拟目录的根目录
[解决办法]
这是一个默认的例子
<?xml version="1.0" encoding="utf-8"?>
<configuration>

<system.web>
<compilation debug="true" targetFramework="4.0" />
</system.web>
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior>
<!-- 为避免泄漏元数据信息,请在部署前将以下值设置为 false 并删除上面的元数据终结点 -->
<serviceMetadata httpGetEnabled="true"/>
<!-- 要接收故障异常详细信息以进行调试,请将以下值设置为 true。在部署前设置为 false 以避免泄漏异常信息 -->
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
</serviceBehaviors>
</behaviors>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
</system.serviceModel>
 <system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
</system.webServer>
  
</configuration>

[解决办法]
探讨
引用:

你访问
http://xxxx.com/clientaccesspolicy.xml等看是否都能正常访问


可以正常访问

热点排行