コード例 #1
0
        /// <summary>
        /// Initializes all service actions in the provider.
        /// </summary>
        /// <param name="operationContext">The operation context instance of the request.</param>
        private void InitializeServiceActions(DataServiceOperationContext operationContext)
        {
            if (operationContext == null)
            {
                throw new DataServiceException("operationContext must not be null!");
            }

            var actionInfos = Interlocked.Exchange(ref this.serviceActionInfos, new Dictionary <string, DSPServiceActionInfo>());

            if (actionInfos.Count > 0)
            {
                IDataServiceMetadataProvider metadataProvider = (IDataServiceMetadataProvider)operationContext.GetService(typeof(IDataServiceMetadataProvider));
                if (metadataProvider == null)
                {
                    throw new DataServiceException("DataServiceOperationContext.GetService(typeof(IDataServiceMetadataProvider)) must return a valid instance of the IDataServiceMetadataProvider.");
                }

                foreach (var entry in actionInfos)
                {
                    DSPServiceActionInfo actionInfo       = entry.Value;
                    DSPActionAttribute   actionAttribute  = actionInfo.ActionAttribute;
                    MethodInfo           actionMethodInfo = actionInfo.Method;

                    ResourceType returnType = DSPActionProvider.GetResourceTypeFromType(metadataProvider, actionInfo.Method.ReturnType, actionAttribute.ReturnElementTypeName);
                    var          parameters = DSPActionProvider.GetServiceActionParameters(metadataProvider, actionAttribute, actionMethodInfo);

                    ServiceAction action;
                    if (!string.IsNullOrEmpty(actionAttribute.ReturnSetPath))
                    {
                        if (actionAttribute.OperationParameterBindingKind != OperationParameterBindingKind.Always && actionAttribute.OperationParameterBindingKind != OperationParameterBindingKind.Sometimes)
                        {
                            throw new DataServiceException("DSPActionAttribute.IsBindable must be true when DSPActionAttribute.ReturnSetPath is not null.");
                        }

                        ResourceSetPathExpression pathExpression = new ResourceSetPathExpression(actionAttribute.ReturnSetPath);
                        action = new ServiceAction(actionMethodInfo.Name, returnType, OperationParameterBindingKind.Sometimes, parameters, pathExpression);
                    }
                    else
                    {
                        ResourceSet returnSet = null;
                        if (!string.IsNullOrEmpty(actionAttribute.ReturnSet))
                        {
                            metadataProvider.TryResolveResourceSet(actionAttribute.ReturnSet, out returnSet);
                        }

                        action = new ServiceAction(actionMethodInfo.Name, returnType, returnSet, actionAttribute.OperationParameterBindingKind, parameters);
                    }

                    action.CustomState = actionInfo;
                    action.SetReadOnly();
                    this.serviceActions.Add(actionMethodInfo.Name, action);
                }
            }
        }
コード例 #2
0
        /// <summary>
        /// Initializes a new <see cref="ServiceAction"/> instance.
        /// </summary>
        /// <param name="method">The methodInfo for the operation.</param>
        /// <param name="instance">The instance on which to invoke the operation.</param>
        public void AddAction(MethodInfo method, object instance = null)
        {
            DSPActionAttribute actionAttribute = method.GetCustomAttributes(typeof(DSPActionAttribute), true /*inherit*/).OfType <DSPActionAttribute>().FirstOrDefault();

            if (actionAttribute == null)
            {
                throw new InvalidOperationException("The method '" + method.Name + "' must contain a DSPActionAttribute.");
            }

            DSPServiceActionInfo actionInfo = new DSPServiceActionInfo {
                ActionAttribute = actionAttribute, Method = method, Instance = instance
            };

            this.serviceActionInfos.Add(method.Name, actionInfo);
        }