public IAsyncResult InvokeBegin(object instance, object[] inputs, AsyncCallback callback, object state) { if (instance == null) { throw new ArgumentNullException("instance"); } int inputCount = inputs == null ? 0 : inputs.Length; if (inputCount != targetInputParameterCount) { throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, Errors.OperationMissingInputParameters, method.Name, targetInputParameterCount, inputCount)); } var arguments = CreateArguments(inputs); var observable = method.Invoke(instance, bindingFlags, Type.DefaultBinder, arguments, null); var outputs = CreateOutputs(arguments); if (returnAsList) { observable = toList.Invoke(null, new[] { observable }); } var task = toTask.Invoke(null, new[] { observable, state }); Contract.Assume(task != null); var result = new ServiceAsyncResult((IAsyncResult)task, outputs); /* Testing has revealed that WCF passes the result object that is supplied to the callback * to the InvokeEnd method instead of passing the object that is returned by InvokeBegin. * * Therefore, to ensure that the strong-typed result is passed to InvokeEnd, it must be * passed directly to the callback. Just in case this behavior changes in the future, the * strong-typed object is also being returned from InvokeBegin instead of the Task that * is returned by the ContinueWith method. */ Action <IAsyncResult> invokeCallback = _ => callback(result); continueWith.Invoke(task, new[] { invokeCallback }); return(result); }
public IAsyncResult InvokeBegin(object instance, object[] inputs, AsyncCallback callback, object state) { if (instance == null) { throw new ArgumentNullException("instance"); } int inputCount = inputs == null ? 0 : inputs.Length; if (inputCount != targetInputParameterCount) { throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, Errors.OperationMissingInputParameters, method.Name, targetInputParameterCount, inputCount)); } var arguments = CreateArguments(inputs); var observable = method.Invoke(instance, bindingFlags, Type.DefaultBinder, arguments, null); var outputs = CreateOutputs(arguments); if (returnAsList) { observable = toList.Invoke(null, new[] { observable }); } var task = toTask.Invoke(null, new[] { observable, state }); Contract.Assume(task != null); var result = new ServiceAsyncResult((IAsyncResult)task, outputs); /* Testing has revealed that WCF passes the result object that is supplied to the callback * to the InvokeEnd method instead of passing the object that is returned by InvokeBegin. * * Therefore, to ensure that the strong-typed result is passed to InvokeEnd, it must be * passed directly to the callback. Just in case this behavior changes in the future, the * strong-typed object is also being returned from InvokeBegin instead of the Task that * is returned by the ContinueWith method. */ Action<IAsyncResult> invokeCallback = _ => callback(result); continueWith.Invoke(task, new[] { invokeCallback }); return result; }