PageRenderTime 38ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/WCFWebApi/prototype/Microsoft.ApplicationServer.HttpEnhancements/System/ServiceModel/Description/TaskServiceAttribute.cs

#
C# | 100 lines | 59 code | 8 blank | 33 comment | 12 complexity | d55b0280f02ceb2bd18c861e8c124b33 MD5 | raw file
Possible License(s): CC-BY-SA-3.0, Apache-2.0
  1. // <copyright>
  2. // Copyright (c) Microsoft Corporation. All rights reserved.
  3. // </copyright>
  4. namespace System.ServiceModel.Description
  5. {
  6. using System;
  7. using System.Collections.ObjectModel;
  8. using System.ServiceModel;
  9. using System.ServiceModel.Channels;
  10. using System.Threading.Tasks;
  11. using Microsoft.Server.Common;
  12. /// <summary>
  13. /// Add the <see cref="TaskServiceAttribute"/> attribute to a service class to enable <see cref="Task"/>-based service operations.
  14. /// In addition, use the <see cref="TaskOperationAttribute"/> on individual service operations to indicate that they
  15. /// are <see cref="Task"/>-based.
  16. /// </summary>
  17. public class TaskServiceAttribute : Attribute, IServiceBehavior
  18. {
  19. /// <summary>
  20. /// Passes custom data to binding elements to support the contract implementation.
  21. /// </summary>
  22. /// <remarks>This <see cref="IServiceBehavior"/> implementation does not pass any data.</remarks>
  23. /// <param name="serviceDescription">The service description of the service.</param>
  24. /// <param name="serviceHostBase">The host of the service.</param>
  25. /// <param name="endpoints">The service endpoints.</param>
  26. /// <param name="bindingParameters">Custom objects to which binding elements have access.</param>
  27. public void AddBindingParameters(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase, Collection<ServiceEndpoint> endpoints, BindingParameterCollection bindingParameters)
  28. {
  29. return;
  30. }
  31. /// <summary>
  32. /// Provides the ability to change run-time property values or insert custom extension objects such as error handlers, message or parameter interceptors, security extensions, and other custom extension objects.
  33. /// </summary>
  34. /// <param name="serviceDescription">The service description.</param>
  35. /// <param name="serviceHostBase">The host that is currently being built.</param>
  36. public void ApplyDispatchBehavior(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase)
  37. {
  38. if (serviceDescription == null)
  39. {
  40. throw Fx.Exception.ArgumentNull("serviceDescription");
  41. }
  42. if (serviceHostBase == null)
  43. {
  44. throw Fx.Exception.ArgumentNull("serviceHostBase");
  45. }
  46. foreach (ServiceEndpoint endpoint in serviceDescription.Endpoints)
  47. {
  48. foreach (OperationDescription operationDescription in endpoint.Contract.Operations)
  49. {
  50. if (operationDescription.SyncMethod != null)
  51. {
  52. if (ServiceReflector.taskType.IsAssignableFrom(operationDescription.SyncMethod.ReturnType))
  53. {
  54. foreach (MessageDescription messageDescription in operationDescription.Messages)
  55. {
  56. if (messageDescription.Direction == MessageDirection.Output)
  57. {
  58. MessagePartDescription returnValue = messageDescription.Body.ReturnValue;
  59. if (returnValue != null &&
  60. ServiceReflector.taskType.IsAssignableFrom(returnValue.Type))
  61. {
  62. messageDescription.Body.ReturnValue.Type = ExtractTaskResultType(returnValue.Type);
  63. }
  64. }
  65. }
  66. }
  67. }
  68. }
  69. }
  70. }
  71. /// <summary>
  72. /// Provides the ability to inspect the service host and the service description to confirm that the service can run successfully.
  73. /// </summary>
  74. /// <remarks>This <see cref="IServiceBehavior"/> implementation does not do any validation.</remarks>
  75. /// <param name="serviceDescription">The service description.</param>
  76. /// <param name="serviceHostBase">The service host that is currently being constructed.</param>
  77. public void Validate(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase)
  78. {
  79. }
  80. /// <summary>
  81. /// Extracts the <see cref="Type"/> of the <see cref="Task{T}"/> or <c>void</c> in the case of
  82. /// the non-generic <see cref="Task"/>.
  83. /// </summary>
  84. /// <param name="taskType">Type of the task.</param>
  85. /// <returns></returns>
  86. internal static Type ExtractTaskResultType(Type taskType)
  87. {
  88. Fx.Assert(taskType != null, "taskType cannot be null");
  89. return taskType.IsGenericType ? taskType.GetGenericArguments()[0] : ServiceReflector.VoidType;
  90. }
  91. }
  92. }