Silverlight+WCF實現跨域調用

來源:互聯網
上載者:User

標籤: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實現跨域調用

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.