AsyncPatternContract.cs 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.ServiceModel;
  5. using System.Threading;
  6. namespace MonoTests.Features.Contracts
  7. {
  8. // Define a service contract.
  9. [ServiceContract (Namespace = "http://MonoTests.Features.Contracts")]
  10. public interface IAsyncPattern
  11. {
  12. [OperationContractAttribute (AsyncPattern = true)]
  13. IAsyncResult BeginAsyncMethod (AsyncCallback callback, object asyncState);
  14. int EndAsyncMethod (IAsyncResult result);
  15. // TODO: Need to investigate asyn methods that have ref/out params that are not necessarily first
  16. // e.g. how does foo(in, ref, out, in) map to AsyncPattern?
  17. }
  18. public class AsyncPatternServer : IAsyncPattern
  19. {
  20. // Simple async result implementation.
  21. class CompletedAsyncResult<T> : IAsyncResult
  22. {
  23. T data;
  24. object state;
  25. public CompletedAsyncResult (T data, object state) { this.data = data; this.state = state; }
  26. public T Data { get { return data; } }
  27. #region IAsyncResult Members
  28. public object AsyncState { get { return (object) state; } }
  29. public WaitHandle AsyncWaitHandle { get { throw new Exception ("The method or operation is not implemented."); } }
  30. public bool CompletedSynchronously { get { return true; } }
  31. public bool IsCompleted { get { return true; } }
  32. #endregion
  33. }
  34. public IAsyncResult BeginAsyncMethod (AsyncCallback callback, object asyncState) {
  35. IAsyncResult result = new CompletedAsyncResult<int> (3, asyncState);
  36. new Thread (new ThreadStart (
  37. delegate {
  38. callback (result);
  39. })).Start ();
  40. return result;
  41. }
  42. public int EndAsyncMethod (IAsyncResult r) {
  43. return ((CompletedAsyncResult<int>) r).Data;
  44. }
  45. }
  46. }