专注收集记录技术开发学习笔记、技术难点、解决方案
网站信息搜索 >> 请输入关键词:
您当前的位置: 首页 > Web Service

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

发布时间:2010-05-20 14:01:29 文章来源:www.iduyao.cn 采编人员:星星草
碰到个头疼无比的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
友情提示:
信息收集于互联网,如果您发现错误或造成侵权,请及时通知本站更正或删除,具体联系方式见页面底部联系我们,谢谢。

其他相似内容:

热门推荐: