一、定義WCF服務
為了演示同步呼叫WCF服務的實現,提供一個簡單的WCF服務介面,完成返回一本圖書基本資訊,WCF服務介面定義如下:
public interface IDataService
{
[OperationContract]
Book GetBook();
}
public class Book
{
public int ID { get; set; }
public string Name { get; set; }
public string Author { get; set; }
public double Price { get; set; }
}
介面提供一個返回圖書基本資訊的方法,包括圖書編好,圖書名,圖書作者以及圖書價格。介面具體的實現如下程式碼:
{
public Book GetBook()
{
return new Book
{
ID = 1001,
Name = "《三國演義》",
Author = "羅貫中",
Price = 89.50
};
}
}
如上提供可正常執行的WCF服務介面,在需要呼叫介面的地方通過WEB引用既可生成該服務的客戶端代理物件。
二、基於MVVM模式的檢視模型
MVVM模式的核心為INotifyPropertyChanged介面,對於實體模型物件和UI控制元件元素間提供了完善的同步更新特性。為了方便介面元素同步更新,這裡引入了MVVP模式的簡單應用。
{
public event PropertyChangedEventHandler PropertyChanged;
protected void RaisePropertyChangedEvent(string propertyName)
{
var handler = PropertyChanged;
if (handler != null)
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
還需要對應於服務介面中的Book物件定義一個ViewModel物件,詳細如下程式碼所示:
{
private int iD;
/// <summary>
/// 圖書ID
/// </summary>
public int ID
{
get { return iD; }
set
{
iD = value;
RaisePropertyChangedEvent("ID");
}
}
private string name;
/// <summary>
/// 圖書名稱
/// </summary>
public string Name
{
get { return name; }
set
{
name = value;
RaisePropertyChangedEvent("Name");
}
}
private string author;
/// <summary>
/// 圖書作者
/// </summary>
public string Author
{
get { return author; }
set
{
author = value;
RaisePropertyChangedEvent("Author");
}
}
private double price;
/// <summary>
/// 圖書價格
/// </summary>
public double Price
{
get { return price; }
set
{
price = value;
RaisePropertyChangedEvent("Price");
}
}
}
三、基於AutoResetEvent的同步實現
利用AutoResetEvent的執行緒等待特性,可以折中實現Silverlight同步呼叫遠端WCF服務。其原理就是在Silverlight發起非同步呼叫遠端WCF的時候進行執行緒阻塞,比記錄非同步呼叫遠端WCF服務介面的完成事件,當非同步呼叫完成後就終止執行緒阻塞,從而獲取狀態事件物件中或得呼叫遠端介面所返回的結果。由於檢視模型物件實現了INotifyPropertyChanged介面能夠及時的更新介面元素,以此間接的就實現了同步方式呼叫。
{
public AsyncCallStatus()
{
}
public T CompletedEventArgs { get; set; }
}
{
private AutoResetEvent autoResetEvent = new AutoResetEvent(false);
public void GetBook(BookViewModel viewModel)
{
if (viewModel == null)
{
throw new ArgumentNullException("viewModel", "引數不能為空。");
}
DataService.DataServiceClient client = new DataService.DataServiceClient();
client.GetBookCompleted += client_GetBookCompleted;
var status = new AsyncCallStatus<GetBookCompletedEventArgs>();
client.GetBookAsync(status);
//阻塞執行緒
autoResetEvent.WaitOne();
if (status.CompletedEventArgs.Error != null)
{
throw status.CompletedEventArgs.Error;
}
var book = status.CompletedEventArgs.Result;
viewModel.ID = book.ID;
viewModel.Name = book.Name;
viewModel.Author = book.Author;
viewModel.Price = book.Price;
}
private void client_GetBookCompleted(object sender, GetBookCompletedEventArgs e)
{
var status = e.UserState as AsyncCallStatus<GetBookCompletedEventArgs>;
status.CompletedEventArgs = e;
//終止執行緒阻塞
autoResetEvent.Set();
}
}
四、Silverlight前端呼叫
Siverlight前端就簡單佈局一個表單作為資料呈現介面,其程式碼如下:
<Grid HorizontalAlignment="Left" Name="grid1" VerticalAlignment="Top" Width="300" Margin="20">
<Grid.RowDefinitions>
<RowDefinition Height="30"></RowDefinition>
<RowDefinition Height="30"></RowDefinition>
<RowDefinition Height="30"></RowDefinition>
<RowDefinition Height="30"></RowDefinition>
<RowDefinition Height="30"></RowDefinition>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="60"></ColumnDefinition>
<ColumnDefinition Width="*"></ColumnDefinition>
</Grid.ColumnDefinitions>
<sdk:Label HorizontalAlignment="Left" Content="圖書編號:" VerticalAlignment="Center" Grid.Column="0" Grid.Row="0"/>
<TextBox Text="{Binding ID}" Grid.Column="1" Grid.Row="0"></TextBox>
<sdk:Label HorizontalAlignment="Left" Content="圖書名稱:" VerticalAlignment="Center" Grid.Column="0" Grid.Row="1"/>
<TextBox Text="{Binding Name}" Grid.Column="1" Grid.Row="1"></TextBox>
<sdk:Label HorizontalAlignment="Left" Content="圖書作者:" VerticalAlignment="Center" Grid.Column="0" Grid.Row="2"/>
<TextBox Text="{Binding Author}" Grid.Column="1" Grid.Row="2"></TextBox>
<sdk:Label HorizontalAlignment="Left" Content="圖書價格:" VerticalAlignment="Center" Grid.Column="0" Grid.Row="3"/>
<TextBox Text="{Binding Price}" Grid.Column="1" Grid.Row="3"></TextBox>
<Button Content="查詢" Grid.Column="1" Grid.Row="4" Width="60" Height="23" Click="Button_Click"></Button>
</Grid>
</Grid>
通過按鈕執行呼叫WCF服務介面查詢圖書資訊,按鈕事件直接使用上面所寫的圖書門面類(BookFacade)的呼叫服務方法即可。
{
try
{
ThreadPool.QueueUserWorkItem(delegate(object o)
{
BookViewModel viewModel = new BookViewModel();
new BookFacade().GetBook(viewModel);
Deployment.Current.Dispatcher.BeginInvoke(() => this.DataContext = viewModel);
});
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
最終的執行如下圖所示效果:
版權說明
本文屬原創文章,歡迎轉載且註明文章出處,其版權歸作者和部落格園共有。為了儲存作者的創作熱情,請在轉載後的明顯位置標記本文出處。
作 者:Beniao
文章出處:http://beniao.cnblogs.com/ 或 http://www.cnblogs.com/