2
這可能是一個愚蠢的問題,因爲我對這裏使用的所有技術(C#,.NET和SOAP)都很陌生。無論如何,我不想在這裏做我想做的事。有什麼辦法來構建System.Web.Services.Protocols.InvokeCompletedEventArgs?
我有一個異步的SOAP調用(從WSDL生成的代碼),我試圖改變,在我的圖書館,我實際上是多次調用多個不同的Web服務器,然後聚合結果返回給調用者。問題是異步SOAP調用的SendOrPostCallback方法需要接受InvokeCompletedEventArgs的參數。我不能構造這個,因爲構造函數是內部聲明的。因此,儘管我可以隱藏我在內部做的事情,並且在我正在做的事情完成時調用回調函數,但我無法將結果返回給調用者。有什麼建議麼?
舊代碼:
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);
}
一直在想這個。可能我可以添加自己的事件類型並覆蓋/更改事件處理程序和回調。然後,我可以避免必須完全構建這種特定類型的事件。這會破壞這個庫的客戶端代碼,可能正在監聽事件並使用相應的委託,但是如果他們嘗試使用「舊」版本,我可能會棄用舊版本和/或提供一些有意義的反饋。 – Mutmansky