TaskOperationDescriptionValidator.cs 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. // <copyright>
  2. // Copyright (c) Microsoft Corporation. All rights reserved.
  3. // </copyright>
  4. namespace System.ServiceModel.Description
  5. {
  6. using System.Reflection;
  7. internal static class TaskOperationDescriptionValidator
  8. {
  9. internal static void Validate(OperationDescription operationDescription, bool isForService)
  10. {
  11. MethodInfo taskMethod = operationDescription.TaskMethod;
  12. if (taskMethod != null)
  13. {
  14. if (isForService)
  15. {
  16. // no other method ([....], async) is allowed to co-exist with a task-based method on the server-side.
  17. EnsureNoSyncMethod(operationDescription);
  18. EnsureNoBeginEndMethod(operationDescription);
  19. }
  20. else
  21. {
  22. // no out/ref parameter is allowed on the client-side.
  23. EnsureNoOutputParameters(taskMethod);
  24. }
  25. EnsureParametersAreSupported(taskMethod);
  26. }
  27. }
  28. private static void EnsureNoSyncMethod(OperationDescription operation)
  29. {
  30. if (operation.SyncMethod != null)
  31. {
  32. string method1Name = operation.TaskMethod.Name;
  33. string method2Name = operation.SyncMethod.Name;
  34. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.CannotHaveTwoOperationsWithTheSameName3, method1Name, method2Name, operation.DeclaringContract.ContractType)));
  35. }
  36. }
  37. private static void EnsureNoBeginEndMethod(OperationDescription operation)
  38. {
  39. if (operation.BeginMethod != null)
  40. {
  41. string method1Name = operation.TaskMethod.Name;
  42. string method2Name = operation.BeginMethod.Name;
  43. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.CannotHaveTwoOperationsWithTheSameName3, method1Name, method2Name, operation.DeclaringContract.ContractType)));
  44. }
  45. }
  46. private static void EnsureParametersAreSupported(MethodInfo method)
  47. {
  48. foreach (ParameterInfo parameter in method.GetParameters())
  49. {
  50. Type parameterType = parameter.ParameterType;
  51. if ((parameterType == ServiceReflector.CancellationTokenType) ||
  52. (parameterType.IsGenericType && parameterType.GetGenericTypeDefinition() == ServiceReflector.IProgressType))
  53. {
  54. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new NotSupportedException(SR.GetString(SR.TaskMethodParameterNotSupported, parameterType)));
  55. }
  56. }
  57. }
  58. private static void EnsureNoOutputParameters(MethodInfo method)
  59. {
  60. if (ServiceReflector.HasOutputParameters(method, false))
  61. {
  62. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.TaskMethodMustNotHaveOutParameter)));
  63. }
  64. }
  65. }
  66. }