碰到个头疼无比的silverlight跨域访问WCF的问题,MSDN逛了好几圈还是没找到解决方案
先把WCF的程序贴出来
WCF中的SLService.svc代码放出来
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(); } }
<?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>
<?xml version="1.0" encoding="utf-8" ?><cross-domain-policy> <allow-access-from domain="*" headers="*"/></cross-domain-policy>
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; } }}
<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>
ssc.PreviewAllBooksAsync();