先把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