InvokeCompletedEventArgs

InvokeCompletedEventArgs

这可能是一个愚蠢的问题,因为我不熟悉这里使用的所有技术(C#、. NET和SOAP)。无论如何,可能根本没有我想要做的事情。

我有一个异步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);
}

最佳答案

原来这是一个愚蠢的问题。 wsdl生成已将InvokeCompletedEventArgs子类化。我在上面的评论中建议创建的类已存在于生成的代码中。我只需要找到它。

关于c# - 有什么方法可以构造System.Web.Services.Protocols.InvokeCompletedEventArgs?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/7616244/

10-13 03:54