Silverlight的RIA应用中访问远端的WebService或WCF服务,都是通过异步线程模式调用的。在某些情况下我们的调用是需要同步进行,虽然Silverlight没有内置同步线程模式调用远端服务接口,但是我们可以通过多线程的处理来伪装出同步调用的实现。在.NET Framework的多线程编程中提供了丰富的线程接口,其中AutoResetEvent和ManualResetEvent在多线程编码中最为常用,本文将介绍如何通过AutoResetEvent的线程等待特性实现Silverlight同步调用远端WCF服务。
一、定义WCF服务
为了演示同步调用WCF服务的实现,提供一个简单的WCF服务接口,完成返回一本图书基本信息,WCF服务接口定义如下:
@H_404_26@public @H_404_26@interface IDataService
{
[OperationContract]
Book GetBook();
}
@H_404_26@public @H_404_26@class Book
{
@H_404_26@public @H_404_26@int ID { @H_404_26@get ; @H_404_26@set ; }
@H_404_26@public @H_404_26@string Name { @H_404_26@get ; @H_404_26@set ; }
@H_404_26@public @H_404_26@string Author { @H_404_26@get ; @H_404_26@set ; }
@H_404_26@public @H_404_26@double Price { @H_404_26@get ; @H_404_26@set ; }
}
接口提供一个返回图书基本信息的方法,包括图书编好,图书名,图书作者以及图书价格。接口具体的实现如下代码:
如上提供可正常运行的WCF服务接口,在需要调用接口的地方通过WEB引用既可生成该服务的客户端代理对象。
二、基于MVVM模式的视图模型
MVVM模式的核心为INotifyPropertyChanged接口,对于实体模型对象和UI控件元素间提供了完善的同步更新特性。为了方便界面元素同步更新,这里引入了MVVP模式的简单应用。
{
@H_404_26@public @H_404_26@event PropertyChangedEventHandler PropertyChanged;
@H_404_26@protected @H_404_26@void RaisePropertyChangedEvent( @H_404_26@string propertyName)
{
var handler = PropertyChanged;
@H_404_26@if (handler != @H_404_26@null )
handler( @H_404_26@this , @H_404_26@new PropertyChangedEventArgs(propertyName));
}
}
还需要对应于服务接口中的Book对象定义一个viewmodel对象,详细如下代码所示:
{
@H_404_26@private @H_404_26@int iD;
/// <summary>
/// 图书ID
/// </summary>
@H_404_26@public @H_404_26@int ID
{
@H_404_26@get { @H_404_26@return iD; }
@H_404_26@set
{
iD = value;
RaisePropertyChangedEvent( " ID " );
}
}
@H_404_26@private @H_404_26@string name;
/// <summary>
/// 图书名称
/// </summary>
@H_404_26@public @H_404_26@string Name
{
@H_404_26@get { @H_404_26@return name; }
@H_404_26@set
{
name = value;
RaisePropertyChangedEvent( " Name " );
}
}
@H_404_26@private @H_404_26@string author;
/// <summary>
/// 图书作者
/// </summary>
@H_404_26@public @H_404_26@string Author
{
@H_404_26@get { @H_404_26@return author; }
@H_404_26@set
{
author = value;
RaisePropertyChangedEvent( " Author " );
}
}
@H_404_26@private @H_404_26@double price;
/// <summary>
/// 图书价格
/// </summary>
@H_404_26@public @H_404_26@double Price
{
@H_404_26@get { @H_404_26@return price; }
@H_404_26@set
{
price = value;
RaisePropertyChangedEvent( " Price " );
}
}
}
三、基于AutoResetEvent的同步实现
利用AutoResetEvent的线程等待特性,可以折中实现Silverlight同步调用远端WCF服务。其原理就是在Silverlight发起异步调用远端WCF的时候进行线程阻塞,比记录异步调用远端WCF服务接口的完成事件,当异步调用完成后就终止线程阻塞,从而获取状态事件对象中或得调用远程接口所返回的结果。由于视图模型对象实现了INotifyPropertyChanged接口能够及时的更新界面元素,以此间接的就实现了同步方式调用。
{
@H_404_26@private AutoResetEvent autoResetEvent = @H_404_26@new AutoResetEvent( @H_404_26@false );
@H_404_26@public @H_404_26@void GetBook(Bookviewmodel viewmodel)
{
@H_404_26@if (viewmodel == @H_404_26@null )
{
@H_404_26@throw @H_404_26@new ArgumentNullException( " viewmodel " , " 参数不能为空。 " );
}
DataService.DataServiceClient client = @H_404_26@new DataService.DataServiceClient();
client.GetBookCompleted += client_GetBookCompleted;
var status = @H_404_26@new AsyncCallStatus < GetBookCompletedEventArgs > ();
client.GetBookAsync(status);
// 阻塞线程
autoResetEvent.WaitOne();
@H_404_26@if (status.CompletedEventArgs.Error != @H_404_26@null )
{
@H_404_26@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;
}
@H_404_26@private @H_404_26@void client_GetBookCompleted( @H_404_26@object sender, GetBookCompletedEventArgs e)
{
var status = e.UserState @H_404_26@as AsyncCallStatus < GetBookCompletedEventArgs > ;
status.CompletedEventArgs = e;
// 终止线程阻塞
autoResetEvent.Set();
}
}
四、Silverlight前端调用
Siverlight前端就简单布局一个表单作为数据呈现界面,其代码如下:
@H_404_26@< Grid HorizontalAlignment @H_404_26@="Left" Name @H_404_26@="grid1" VerticalAlignment @H_404_26@="Top" Width @H_404_26@="300" Margin @H_404_26@="20" @H_404_26@>
@H_404_26@< Grid.RowDeFinitions @H_404_26@>
@H_404_26@< RowDeFinition Height @H_404_26@="30" @H_404_26@></ RowDeFinition @H_404_26@>
@H_404_26@< RowDeFinition Height @H_404_26@="30" @H_404_26@></ RowDeFinition @H_404_26@>
@H_404_26@< RowDeFinition Height @H_404_26@="30" @H_404_26@></ RowDeFinition @H_404_26@>
@H_404_26@< RowDeFinition Height @H_404_26@="30" @H_404_26@></ RowDeFinition @H_404_26@>
@H_404_26@< RowDeFinition Height @H_404_26@="30" @H_404_26@></ RowDeFinition @H_404_26@>
@H_404_26@</ Grid.RowDeFinitions @H_404_26@>
@H_404_26@< Grid.ColumnDeFinitions @H_404_26@>
@H_404_26@< ColumnDeFinition Width @H_404_26@="60" @H_404_26@></ ColumnDeFinition @H_404_26@>
@H_404_26@< ColumnDeFinition Width @H_404_26@="*" @H_404_26@></ ColumnDeFinition @H_404_26@>
@H_404_26@</ Grid.ColumnDeFinitions @H_404_26@>
@H_404_26@< sdk:Label HorizontalAlignment @H_404_26@="Left" Content @H_404_26@="图书编号:" VerticalAlignment @H_404_26@="Center" Grid.Column @H_404_26@="0" Grid.Row @H_404_26@="0" @H_404_26@/>
@H_404_26@< TextBox Text @H_404_26@=" {Binding ID} @H_404_26@" Grid.Column @H_404_26@="1" Grid.Row @H_404_26@="0" @H_404_26@></ TextBox @H_404_26@>
@H_404_26@< sdk:Label HorizontalAlignment @H_404_26@="Left" Content @H_404_26@="图书名称:" VerticalAlignment @H_404_26@="Center" Grid.Column @H_404_26@="0" Grid.Row @H_404_26@="1" @H_404_26@/>
@H_404_26@< TextBox Text @H_404_26@=" {Binding Name} @H_404_26@" Grid.Column @H_404_26@="1" Grid.Row @H_404_26@="1" @H_404_26@></ TextBox @H_404_26@>
@H_404_26@< sdk:Label HorizontalAlignment @H_404_26@="Left" Content @H_404_26@="图书作者:" VerticalAlignment @H_404_26@="Center" Grid.Column @H_404_26@="0" Grid.Row @H_404_26@="2" @H_404_26@/>
@H_404_26@< TextBox Text @H_404_26@=" {Binding Author} @H_404_26@" Grid.Column @H_404_26@="1" Grid.Row @H_404_26@="2" @H_404_26@></ TextBox @H_404_26@>
@H_404_26@< sdk:Label HorizontalAlignment @H_404_26@="Left" Content @H_404_26@="图书价格:" VerticalAlignment @H_404_26@="Center" Grid.Column @H_404_26@="0" Grid.Row @H_404_26@="3" @H_404_26@/>
@H_404_26@< TextBox Text @H_404_26@=" {Binding Price} @H_404_26@" Grid.Column @H_404_26@="1" Grid.Row @H_404_26@="3" @H_404_26@></ TextBox @H_404_26@>
@H_404_26@< Button Content @H_404_26@="查询" Grid.Column @H_404_26@="1" Grid.Row @H_404_26@="4" Width @H_404_26@="60" Height @H_404_26@="23" Click @H_404_26@="Button_Click" @H_404_26@></ Button @H_404_26@>
@H_404_26@</ Grid @H_404_26@>
@H_404_26@</ Grid @H_404_26@>
通过按钮执行调用WCF服务接口查询图书信息,按钮事件直接使用上面所写的图书门面类(BookFacade)的调用服务方法即可。
{
@H_404_26@try
{
ThreadPool.QueueUserWorkItem( @H_404_26@delegate ( @H_404_26@object o)
{
Bookviewmodel viewmodel = @H_404_26@new Bookviewmodel();
@H_404_26@new BookFacade().GetBook(viewmodel);
Deployment.Current.dispatcher.BeginInvoke(() => @H_404_26@this .DataContext = viewmodel);
});
}
@H_404_26@catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
最终的运行如下图所示效果:
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。