这可能是一个愚蠢的问题,因为我对我在这里使用的所有技术(C#,.NET和SOAP)都是新手。反正可能无法做我想做的事。
我有一个异步SOAP调用(从WSDL生成的代码),我试图改变这种方式,在我的库中,我实际上是多次调用多个不同的Web服务器,然后聚合结果返回给来电者。问题是异步SOAP调用的SendOrPostCallback方法需要使用InvokeCompletedEventArgs的参数。我无法构造这个,因为构造函数被声明为internal。因此,虽然我可以隐藏我正在内部执行的操作,并在我正在执行的操作完成时调用回调,但我无法将结果返回给调用者。有什么建议吗?
旧代码:
public void getStatusesAsync(RequestId[] requestIds, object userState)
{
if ((this.getStatusesOperationCompleted == null))
{
this.getStatusesOperationCompleted = new System.Threading.SendOrPostCallback(this.OngetStatusesOperationCompleted);
}
this.InvokeAsync("getStatuses", new object[] {
requestIds}, this.getStatusesOperationCompleted, userState);
}
private void OngetStatusesOperationCompleted(object arg)
{
if ((this.getStatusesCompleted != null))
{
System.Web.Services.Protocols.InvokeCompletedEventArgs invokeArgs = ((System.Web.Services.Protocols.InvokeCompletedEventArgs)(arg));
this.getStatusesCompleted(this, new getStatusesCompletedEventArgs(invokeArgs.Results, invokeArgs.Error, invokeArgs.Cancelled, invokeArgs.UserState));
}
}
新守则:
public void getStatusesAsync(RequestId[] requestIds, object userState)
{
if ((this.getStatusesOperationCompleted == null))
{
this.getStatusesOperationCompleted = new System.Threading.SendOrPostCallback(this.OngetStatusesOperationCompleted);
}
asyncExecuteMultipleSoapCalls("getStatuses", new object[] { requestIds}, this.getStatusesOperationCompleted, userState);
}
private System.IAsyncResult asyncExecuteMultipleSoapCalls(string methodName, object[] args, System.Threading.SendOrPostCallback callback, object asyncState)
{
Thread backgroundSoapCalls = new Thread(new ParameterizedThreadStart(asyncThreadRun));
object[] threadArgs = new object[] { methodName, args, callback, asyncState };
backgroundSoapCalls.Start(threadArgs);
return null;//How to return AsyncResult?
}
private RequestStatus[] multiSoapCallResults = null;
private void asyncThreadRun(object args)
{
string methodName = (string)(((object[])args)[0]);
object[] methodArgs = (object[])(((object[])args)[1]);
System.Threading.SendOrPostCallback methodCallback = (System.Threading.SendOrPostCallback)(((object[])args)[2]);
object asyncState = (((object[])args)[3]);
//To make a long story short, I'm using this thread to execute synchronous
//SOAP calls, then call the event. The reason is that different requestIds
//in the passed in array may reside on different Web Servers. Hopefully,
//this will work similarly to the original Asynchronous call
multiSoapCallResults = executeMultipleSoapCalls(methodName, methodArgs);
//Now that I'm done, I want to pass the evenArgs to the SendOrPostCallback
RequestStatus[] returnStatusArray = new RequestStatus[multiSoapCallResults.Length];
multiSoapCallResults.CopyTo(returnStatusArray, 0);
multiSoapCallResults = null;
//This line doesn't compile of course, which is the problem
System.Web.Services.Protocols.InvokeCompletedEventArgs eventArgs = new System.Web.Services.Protocols.InvokeCompletedEventArgs(null, false, null, returnStatusArray);
//Need to pass event args here
methodCallback(eventArgs);
}
答案 0 :(得分:1)
原来这是一个愚蠢的问题。 wsdl生成已经是InvokeCompletedEventArgs的子类。我在上面的评论中建议的类已经存在于生成的代码中。我只需要找到它。