异步调用开发应用总结(WCF)
上一篇随笔的异步调用是方法的异步调用实现,WCF的异步调用有自己的实现逻辑(一些方法和调用过程.net已经制定好)
WCF异步调用示例:
客户端调用:
AsyncManageAgent clientAgent = this.Command as AsyncManageAgent;
clientAgent.GetNameAsync(Code, userState);
clientAgent.GetNameCompleted += delegate(object sender, Agent.GetNameCompletedEventArgs args)
{
this.GetNameCompleted(this, new GetNameCompletedEventArgs(args.results,args.Error,args.Cancelled,args.UserState));
};
WCF代理类(异步调用)
private System.IAsyncResult OnBeginGetName(object[] inValues, System.AsyncCallback callback, object asyncState)
{
string Code = inValues[0].ToString();
return this.BeginGetName(Code, callback, asyncState);
}
//onEnd
private object[] OnEndGetName(System.IAsyncResult result)
{
string retVal = this.EndGetName(result);
return new object[] {
retVal};
}
//OnCompleted
private void OnGetNameCompleted(object state)
{
if ((this.GetNameCompleted != null))
{
InvokeAsyncCompletedEventArgs e = ((InvokeAsyncCompletedEventArgs)(state));
this.GetNameCompleted(this, new GetNameCompletedEventArgs(e.Results, e.Error, e.Cancelled, e.UserState));
}
}
//Async方法
public void GetNameAsync(string Code)
{
this.GetNameAsync(Code, null);
}
public void GetNameAsync(string Code, object userState)
{
if ((this.onBeginGetNameDelegate == null))
{
this.onBeginGetNameDelegate = new BeginOperationDelegate(this.OnBeginGetName);
}
if ((this.onEndGetNameDelegate == null))
{
this.onEndGetNameDelegate = new EndOperationDelegate(this.OnEndGetName);
}
if ((this.onGetNameCompletedDelegate == null))
{
this.onGetNameCompletedDelegate = new System.Threading.SendOrPostCallback(this.OnGetNameCompleted);
}
base.InvokeAsync(this.onBeginGetNameDelegate, new object[] {
Code}, this.onEndGetNameDelegate, this.onGetNameCompletedDelegate, userState);
}
关于服务端具体实现,服务部署配置等需要另写博客,待续。。