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