linux c调用wcf服务,Silverlight+WCF实现跨域调用
在這篇文章中,WCF扮演服務(wù)器,向外提供LoginVaild服務(wù);Silverlight扮演客戶端,調(diào)用WCF提供的LoginVaild服務(wù)。思路有了,下面進(jìn)行代碼實(shí)現(xiàn)。
數(shù)據(jù)庫腳本實(shí)現(xiàn)
新建T_User表,在表中添加兩個(gè)字段username、password,向表中插入一條數(shù)據(jù)admin admin,腳本如下:
USE [test]
GO
/****** Object:? Table [dbo].[T_User]? ? Script Date: 09/28/2014 21:12:02 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE 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]
GO
SET ANSI_PADDING OFF
GO
INSERT [dbo].[T_User] ([username], [password]) VALUES (N'admin', N'admin')
WCF服務(wù)器實(shí)現(xiàn)
1.新建一個(gè)WCF服務(wù)庫項(xiàng)目,在默認(rèn)生成的IServer1.cs接口在添加LoginVaild服務(wù)的聲明:
[OperationContract]
bool LoginVaild(string userName, string password);
2.添加ADO.Net實(shí)體數(shù)據(jù)模型文件--Model.edmx,用于對(duì)數(shù)據(jù)表T_User的訪問;
3.在Service1.svc中對(duì)LoginVaild方法進(jìn)行實(shí)現(xiàn):
public bool LoginVaild(string userName, string password)
{
bool result = false;
//需要訪問的ADO.Net數(shù)據(jù)實(shí)體模型
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.在項(xiàng)目的根目錄添加跨域訪問文件clientaccesspolicy.xml,內(nèi)容如下:
5.設(shè)定WCF服務(wù)器使用特定端口進(jìn)行訪問,方法:選中WCF服務(wù)器項(xiàng)目-->郵件屬性-->Web-->特定端口,輸入1316。這樣保證我們每次可以通過1316端口訪問WCF提供的服務(wù)。
到此,WCF服務(wù)器端配置完成,選中Service1.svc文件,在瀏覽器中瀏覽器下WCF提供的服務(wù)。
Silverlight客戶端實(shí)現(xiàn)
1.新建Silverlight應(yīng)用程序,在引用中添加服務(wù)引用,在地址欄輸入Service1.svc文件在瀏覽器中的路徑,比如我的是:http://localhost:1316/Service1.svc;
2.新建Silverlight用戶控件Login.xaml文件,在顯示頁面添加用戶名、密碼TextBox和登錄Button;
3.在Login.xaml后臺(tái)通過調(diào)用WCF提供的服務(wù)對(duì)用戶輸入進(jìn)行判斷,代碼如下:
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(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("用戶名或密碼錯(cuò)誤!");
}
}
else
{
MessageBox.Show(e.Error.ToString());
}
}
4.在App.xaml配置文件設(shè)置Login.xaml為起始頁,代碼如下:
private void Application_Startup(object sender, StartupEventArgs e)
{
this.RootVisual = new Login();
}
到此,客戶端配置完成,運(yùn)行Silverlight客戶端項(xiàng)目即可查看結(jié)果。源碼下載地址:
------------------------------------------分割線------------------------------------------
具體下載目錄在 /2015年資料/2月/27日/Silverlight+WCF實(shí)現(xiàn)跨域調(diào)用/
------------------------------------------分割線------------------------------------------
現(xiàn)在回首整個(gè)實(shí)現(xiàn)過程,有點(diǎn)面向服務(wù)編程的意思:WCF提供一個(gè)服務(wù),然后把訪問服務(wù)的接口公開,想調(diào)用此服務(wù)的項(xiàng)目只要添加此服務(wù)的引用便能調(diào)用WCF提供的服務(wù)。
在實(shí)現(xiàn)過程中,有個(gè)問題至今尚未解決,在WCF服務(wù)器項(xiàng)目中配置ADO.Net實(shí)體數(shù)據(jù)模型時(shí),如果是用SQL Server的SQL Server身份驗(yàn)證方式登錄,便會(huì)報(bào)“服務(wù)器返回了錯(cuò)誤 Not found”異常;改成Windows身份驗(yàn)證問題解決。這是個(gè)治標(biāo)不治本的解決方案,對(duì)此問題,希望大神能夠給出解釋。
總結(jié)
以上是生活随笔為你收集整理的linux c调用wcf服务,Silverlight+WCF实现跨域调用的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: linux结束ping进程,linux常
- 下一篇: u盘魔术师装linux,使用U盘魔术师安