所以我正在开发一个使用 Web 服务的客户端。我使用服务中的 WSDL 和 XSD 文件来生成代理类,所有同步功能都可以正常工作。但是,鉴于它们的同步特性,进行任何调用都会导致 UI 停止响应,直到调用完成。使用异步方法的经典原因,对吧?
问题是,我还在学校攻读学位,对异步编程知之甚少。我试图在网上阅读它(我的雇主甚至订阅了 Books 24x7),但我很难掌握应该如何拨打电话以及如何处理回复。这是我所拥有的:
/// <remarks/>
[System.Web.Services.Protocols.SoapDocumentMethodAttribute("http://localhost:8080/getRecords", RequestNamespace="http://www.<redacted>.com/ws/schemas", ResponseNamespace="http://www.<redacted>.com/ws/schemas", Use=System.Web.Services.Description.SoapBindingUse.Literal, ParameterStyle=System.Web.Services.Protocols.SoapParameterStyle.Wrapped)]
[return: System.Xml.Serialization.XmlArrayAttribute("records", Form=System.Xml.Schema.XmlSchemaForm.Unqualified, IsNullable=true)]
[return: System.Xml.Serialization.XmlArrayItemAttribute("list", Form=System.Xml.Schema.XmlSchemaForm.Unqualified, IsNullable=false)]
public record[] getRecords([System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified, IsNullable=true)] string username, [System.Xml.Serialization.XmlArrayAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified, IsNullable=true)] [System.Xml.Serialization.XmlArrayItemAttribute("list", Form=System.Xml.Schema.XmlSchemaForm.Unqualified, DataType="integer", IsNullable=false)] string[] ids) {
object[] results = this.Invoke("getRecords", new object[] {
username,
ids});
return ((record[])(results[0]));
}
/// <remarks/>
public void getRecordsAsync(string username, string[] ids) {
this.getRecordsAsync(username, ids, null);
}
/// <remarks/>
public void getRecordsAsync(string username, string[] ids, object userState) {
if ((this.getRecordsOperationCompleted == null)) {
this.getRecordsOperationCompleted = new System.Threading.SendOrPostCallback(this.OngetRecordsOperationCompleted);
}
this.InvokeAsync("getRecords", new object[] {
username,
ids}, this.getRecordsOperationCompleted, userState);
}
private void OngetRecordsOperationCompleted(object arg) {
if ((this.getRecordsCompleted != null)) {
System.Web.Services.Protocols.InvokeCompletedEventArgs invokeArgs = ((System.Web.Services.Protocols.InvokeCompletedEventArgs)(arg));
this.getRecordsCompleted(this, new getRecordsCompletedEventArgs(invokeArgs.Results, invokeArgs.Error, invokeArgs.Cancelled, invokeArgs.UserState));
}
}
还有这个:
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Web.Services", "4.0.30319.1")]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
public partial class getRecordsCompletedEventArgs : System.ComponentModel.AsyncCompletedEventArgs {
private object[] results;
internal getRecordsCompletedEventArgs(object[] results, System.Exception exception, bool cancelled, object userState) :
base(exception, cancelled, userState) {
this.results = results;
}
/// <remarks/>
public record[] Result {
get {
this.RaiseExceptionIfNecessary();
return ((record[])(this.results[0]));
}
}
}
和这个:
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Web.Services", "4.0.30319.1")]
public delegate void getRecordsCompletedEventHandler(object sender, getRecordsCompletedEventArgs e);
我选择这个例子是因为同步调用有一个返回类型,而异步没有——至少在函数调用本身中没有。我知道 getRecordsCompletedEventArgs 类具有正确的返回类型,这就是我从调用中获取数据的方式。我似乎无法弄清楚如何实际做到这一点。
假设我将当前对 getRecords 的调用替换为 getRecordsAsync:
如何设置客户端在异步调用完成时响应?我需要使用我已经编写的 LINQ 过程将 XML 放入文件中,我需要记录操作的成功或失败,并且我需要通知用户操作已完成。
如何确保调用实际上是异步发生的?我记得有一次读到,简单地调用异步 SOAP 方法实际上并不会与当前线程异步发生,除非您先执行其他操作。有小费吗?
还有其他我遗漏的主要考虑因素吗?(如:“如果你忘记这样做,它会炸毁你的程序!”)
这些都是迄今为止我无法找到令人信服的确切答案的问题。预先感谢您提供的任何帮助。