標籤:web silverlight wcf
在這篇文章中,WCF扮演伺服器,向外提供LoginVaild服務;Silverlight扮演用戶端,調用WCF提供的LoginVaild服務。思路有了,下面進行代碼實現。
資料庫指令碼實現 建立T_User表,在表中添加兩個欄位username、password,向表中插入一條資料admin admin,指令碼如下:
USE [test]GO/****** Object: Table [dbo].[T_User] Script Date: 09/28/2014 21:12:02 ******/SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOSET ANSI_PADDING ONGOCREATE TABLE [dbo].[T_User]([username] [varchar](20) NOT NULL,[password] [varchar](20) NOT NULL, CONSTRAINT [PK_T_User] PRIMARY KEY CLUSTERED ([username] ASC)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]) ON [PRIMARY]GOSET ANSI_PADDING OFFGOINSERT [dbo].[T_User] ([username], [password]) VALUES (N'admin', N'admin')
WCF伺服器實現
1.建立一個WCF服務庫項目,在預設產生的IServer1.cs介面在添加LoginVaild服務的聲明:
[OperationContract] bool LoginVaild(string userName, string password);
2.添加ADO.Net實體資料模型檔案--Model.edmx,用於對資料表T_User的訪問;
3.在Service1.svc中對LoginVaild方法進行實現:
public bool LoginVaild(string userName, string password) { bool result = false; //需要訪問的ADO.Net資料實體模型 using (SLtestEntitiesSecond entities = new SLtestEntitiesSecond()) { var user = entities.T_User.Where(c => c.username == userName && c.password == password).SingleOrDefault(); if (user == null) { result = false; } else { result = true; } } return result; } 4.在項目的根目錄添加跨域訪問檔案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>
5.設定WCF伺服器使用特定連接埠進行訪問,方法:選中WCF伺服器項目-->郵件屬性-->Web-->特定連接埠,輸入1316。這樣保證我們每次可以通過1316連接埠訪問WCF提供的服務。
到此,WCF伺服器端配置完成,選中Service1.svc檔案,在瀏覽器中瀏覽器下WCF提供的服務。
Silverlight用戶端實現 1.建立Silverlight應用程式,在引用中加入服務參考,在地址欄輸入Service1.svc檔案在瀏覽器中的路徑,比如我的是:http://localhost:1316/Service1.svc; 2.建立Silverlight使用者控制項Login.xaml檔案,在顯示頁面添加使用者名稱、密碼TextBox和登入Button; 3.在Login.xaml後台通過調用WCF提供的服務對使用者輸入進行判斷,代碼如下:
private void button1_Click(object sender, RoutedEventArgs e) { string userName = txtusername.Text.Trim(); string password = txtpassword.Text.Trim(); Service1Client client = new Service1Client(); client.LoginVaildCompleted += new EventHandler<LoginVaildCompletedEventArgs>(client_LoginVaildCompleted); client.LoginVaildAsync(userName, password); client.CloseAsync(); } void client_LoginVaildCompleted(object sender, LoginVaildCompletedEventArgs e) { if (e.Error == null) { //MessageBox.Show(e.Result.ToString()); if (e.Result == true) { this.Content = new MainPage(); } else { MessageBox.Show("使用者名稱或密碼錯誤!"); } } else { MessageBox.Show(e.Error.ToString()); } } 4.在App.xaml設定檔設定Login.xaml為起始頁,代碼如下:
private void Application_Startup(object sender, StartupEventArgs e) { this.RootVisual = new Login(); } 到此,用戶端配置完成,運行Silverlight用戶端項目即可查看結果。源碼:http://pan.baidu.com/s/1mgn3IEO 現在回首整個實現過程,有點面向服務編程的意思:WCF提供一個服務,然後把訪問服務的介面公開,想調用此服務的項目只要添加此服務的引用便能調用WCF提供的服務。 在實現過程中,有個問題至今尚未解決,在WCF伺服器項目中配置ADO.Net實體資料模型時,如果是用SQL Server的SQL Server身分識別驗證方式登入,便會報“伺服器返回了錯誤 Not found”異常;改成Windows身分識別驗證問題解決。這是個治標不治本的解決方案,對此問題,希望大神能夠給出解釋。
Silverlight+WCF實現跨域調用