예제 #1
0
        private static HttpParameter GetRequestContentHandler(HttpOperationDescription operation, string[] uriTemplateParameterNames)
        {
            Fx.Assert(operation != null, "The 'operation' parameter should not be null.");
            Fx.Assert(uriTemplateParameterNames != null, "The 'uriTemplateParameterNames' parameter should not be null.");

            HttpParameter        requestContentParameter   = null;
            List <HttpParameter> parametersNotUriBound     = new List <HttpParameter>();
            List <HttpParameter> possibleContentParameters = new List <HttpParameter>();

            foreach (HttpParameter parameter in operation.InputParameters)
            {
                if (IsPossibleRequestContentParameter(parameter))
                {
                    possibleContentParameters.Add(parameter);
                }

                bool uriTemplateParameterMatch = false;
                foreach (string uriTemplateParameterName in uriTemplateParameterNames)
                {
                    if (string.Equals(uriTemplateParameterName, parameter.Name, StringComparison.OrdinalIgnoreCase))
                    {
                        uriTemplateParameterMatch = true;
                        break;
                    }
                }

                if (!uriTemplateParameterMatch)
                {
                    parametersNotUriBound.Add(parameter);
                }
            }

            if (possibleContentParameters.Count > 1)
            {
                ThrowExceptionForMulitpleRequestContentParameters(possibleContentParameters, operation.Name);
            }

            if (possibleContentParameters.Count == 1)
            {
                requestContentParameter = possibleContentParameters[0];
            }
            else
            {
                if (parametersNotUriBound.Count > 1)
                {
                    ThrowExceptionForUnknownRequestContentParameter(operation.Name);
                }

                if (parametersNotUriBound.Count == 0)
                {
                    return(null);
                }

                requestContentParameter = parametersNotUriBound[0];
            }

            ValidateRequestContentParameter(requestContentParameter, operation.Name);

            return(requestContentParameter);
        }
예제 #2
0
        public void HttpOperationDescription_Can_Be_Created_From_Simple_Types()
        {
            HttpOperationDescription hod = new HttpOperationDescription();

            hod.ReturnValue = new HttpParameter("Return", typeof(string));
            hod.InputParameters.Add(new HttpParameter("InParameter", typeof(int)));
            hod.OutputParameters.Add(new HttpParameter("OutParameter", typeof(string)));

            HttpParameter parmDesc = hod.ReturnValue;

            Assert.AreEqual("Return", parmDesc.Name, "ReturnValue.Name incorrect");
            Assert.AreEqual(typeof(string), parmDesc.Type, "ReturnValue.Type incorrect");

            IList <HttpParameter> coll = hod.InputParameters;

            Assert.AreEqual(1, coll.Count, "Input parameter collection should have 1 element");
            parmDesc = coll[0];
            Assert.AreEqual("InParameter", parmDesc.Name, "InputParameter.Name incorrect");
            Assert.AreEqual(typeof(int), parmDesc.Type, "InputParameter.Type incorrect");

            coll = hod.OutputParameters;
            Assert.AreEqual(1, coll.Count, "Output parameter collection should have 1 element");
            parmDesc = coll[0];
            Assert.AreEqual("OutParameter", parmDesc.Name, "OutParameter.Name incorrect");
            Assert.AreEqual(typeof(string), parmDesc.Type, "OutParameter.Type incorrect");
        }
예제 #3
0
        public CustomMessageFormatter(HttpOperationDescription httpOperation)
        {
            if (httpOperation == null)
            {
                throw new ArgumentNullException("httpOperation");
            }

            if (httpOperation.InputParameters.Count == 1 &&
                 httpOperation.InputParameters[0].Type == typeof(HttpRequestMessage))
            {
                this.hasRequestMessage = true;
            }
            else if (httpOperation.InputParameters.Count != 0)
            {
                throw new NotSupportedException(
                    "The MessageFormatter only supports a single optional input parameter of type 'HttpRequestMessage'.");
            }

            if (httpOperation.OutputParameters.Count > 0)
            {
                throw new NotSupportedException(
                    "The MessageFormatter only does not support outputt parameters.");
            }

            if (httpOperation.ReturnValue.Type == typeof(HttpResponseMessage))
            {
                this.hasResponseMessage = true;
            }
            else if (httpOperation.ReturnValue.Type != typeof(void))
            {
                throw new NotSupportedException(
                    "The MessageFormatter only supports an optional return type of 'HttpResponseMessage'.");
            }
        }
예제 #4
0
        public void HttpOperationDescription_Update_ReturnValue_From_OperationDescription()
        {
            OperationDescription     od  = GetOperationDescription(typeof(SimpleOperationsService), "OneInputAndReturnValue");
            HttpOperationDescription hod = od.ToHttpOperationDescription();

            Assert.IsNotNull(hod, "Failed to create HttpOperationDescription");
            HttpParameter returnParameter = hod.ReturnValue;

            Assert.IsNotNull(returnParameter, "Return parameter was null");
            Assert.AreEqual(typeof(string), returnParameter.Type, "Return parameter type should have been string");

            // Update return MPD in-place
            MessagePartDescription mpd = od.Messages[1].Body.ReturnValue;

            mpd.Type = typeof(float);
            Assert.AreEqual(typeof(float), hod.ReturnValue.Type, "Updating OD ReturnValue in place should reflect in HOD");

            // Insert a new MPD
            mpd      = new MessagePartDescription("NewName", "NewNamespace");
            mpd.Type = typeof(double);
            od.Messages[1].Body.ReturnValue = mpd;
            Assert.AreEqual(typeof(double), hod.ReturnValue.Type, "Inserting new OD ReturnValue  should reflect in HOD");

            // Remove the MPD
            od.Messages.RemoveAt(1);
            Assert.IsNull(hod.ReturnValue, "Removing return value message part should yield null ReturnValue");
        }
예제 #5
0
        public void HttpOperationDescription_Update_ReturnValue_Throws_From_Unsynchronized()
        {
            OperationDescription   od  = GetOperationDescription(typeof(SimpleOperationsService), "OneInputAndReturnValue");
            MessagePartDescription mpd = od.Messages[1].Body.ReturnValue;

            // Get a synchronized HOD.
            HttpOperationDescription hod = od.ToHttpOperationDescription();

            Assert.IsNotNull(hod, "Failed to create HttpOperationDescription");

            // Get a synchronized HPD
            HttpParameter hpd = hod.ReturnValue;

            Assert.IsNotNull(hpd, "Return parameter was null");
            Assert.AreEqual(typeof(string), hpd.Type, "Return parameter type should have been string");

            // Null set should succeed
            hod.ReturnValue = null;
            Assert.IsNull(hod.ReturnValue, "Could not set to null");

            // Null should propagate
            Assert.IsNull(od.Messages[1].Body.ReturnValue, "Null did not propagate");

            // Set back to valid value and see if propagates
            hod.ReturnValue = hpd;
            Assert.AreEqual(mpd, od.Messages[1].Body.ReturnValue, "Did not reset to synced value");
        }
 protected override Collection<Microsoft.ApplicationServer.Http.Dispatcher.HttpOperationHandler> OnCreateRequestHandlers(System.ServiceModel.Description.ServiceEndpoint endpoint, HttpOperationDescription operation)
 {
     var coll = base.OnCreateRequestHandlers(endpoint, operation);
     this.Formatters.Remove(Formatters.XmlFormatter);
     return coll;
     // return base.OnCreateRequestHandlers(endpoint, operation);
 }
예제 #7
0
        internal static string GetDescription(this HttpOperationDescription operation)
        {
            Fx.Assert(operation != null, "The 'operation' parameter should not be null.");

            OperationDescription operationDescription = operation.ToOperationDescription();

            object[] attributes = null;
            if (operationDescription.SyncMethod != null)
            {
                attributes = operationDescription.SyncMethod.GetCustomAttributes(descriptionAttributeType, true);
            }
            else if (operationDescription.BeginMethod != null)
            {
                attributes = operationDescription.BeginMethod.GetCustomAttributes(descriptionAttributeType, true);
            }

            if (attributes != null && attributes.Length > 0)
            {
                return(((DescriptionAttribute)attributes[0]).Description);
            }
            else
            {
                return(String.Empty);
            }
        }
예제 #8
0
        public void GetUriTemplate_Returns_UriTemplate()
        {
            ExceptionAssert.ThrowsArgumentNull("operation", () => ((HttpOperationDescription)null).GetUriTemplate());

            ContractDescription contract = ContractDescription.GetContract(typeof(UriTemplateService));

            OperationDescription     operationDescription     = contract.Operations.Where(od => od.Name == "NoAttributeOperation").FirstOrDefault();
            HttpOperationDescription httpOperationDescription = operationDescription.ToHttpOperationDescription();
            UriTemplate template = httpOperationDescription.GetUriTemplate();

            Assert.AreEqual(0, template.PathSegmentVariableNames.Count, "HttpOperationDescription.GetUriTemplate should return a UriTemplate with zero path variables.");
            Assert.AreEqual(0, template.QueryValueVariableNames.Count, "HttpOperationDescription.GetUriTemplate should return a UriTemplate with zero query variables.");

            operationDescription     = contract.Operations.Where(od => od.Name == "WebInvokeSansTemplateStringOperation").FirstOrDefault();
            httpOperationDescription = operationDescription.ToHttpOperationDescription();
            template = httpOperationDescription.GetUriTemplate();
            Assert.AreEqual(0, template.PathSegmentVariableNames.Count, "HttpOperationDescription.GetUriTemplate should return a UriTemplate with zero path variables.");
            Assert.AreEqual(0, template.QueryValueVariableNames.Count, "HttpOperationDescription.GetUriTemplate should return a UriTemplate with zero query variables.");

            operationDescription     = contract.Operations.Where(od => od.Name == "WebGetSansTemplateStringOperation").FirstOrDefault();
            httpOperationDescription = operationDescription.ToHttpOperationDescription();
            template = httpOperationDescription.GetUriTemplate();
            Assert.AreEqual(0, template.PathSegmentVariableNames.Count, "HttpOperationDescription.GetUriTemplate should return a UriTemplate with zero path variables.");
            Assert.AreEqual(0, template.QueryValueVariableNames.Count, "HttpOperationDescription.GetUriTemplate should return a UriTemplate with zero query variables.");

            operationDescription     = contract.Operations.Where(od => od.Name == "WebInvokeWithParametersOperation").FirstOrDefault();
            httpOperationDescription = operationDescription.ToHttpOperationDescription();
            template = httpOperationDescription.GetUriTemplate();
            Assert.AreEqual(0, template.PathSegmentVariableNames.Count, "HttpOperationDescription.GetUriTemplate should return a UriTemplate with zero path variables.");
            Assert.AreEqual(0, template.QueryValueVariableNames.Count, "HttpOperationDescription.GetUriTemplate should return a UriTemplate with zero query variables.");

            operationDescription     = contract.Operations.Where(od => od.Name == "WebGetWithParametersOperation").FirstOrDefault();
            httpOperationDescription = operationDescription.ToHttpOperationDescription();
            template = httpOperationDescription.GetUriTemplate();
            Assert.AreEqual(0, template.PathSegmentVariableNames.Count, "HttpOperationDescription.GetUriTemplate should return a UriTemplate with zero path variables.");
            Assert.AreEqual(2, template.QueryValueVariableNames.Count, "HttpOperationDescription.GetUriTemplate should return a UriTemplate with two query variables.");
            Assert.AreEqual("IN1", template.QueryValueVariableNames[0], "HttpOperationDescription.GetUriTemplate should return a UriTemplate with query variable 'IN1'.");
            Assert.AreEqual("IN2", template.QueryValueVariableNames[1], "HttpOperationDescription.GetUriTemplate should return a UriTemplate with query variable 'IN2'.");

            operationDescription     = contract.Operations.Where(od => od.Name == "WebInvokeWithEmptyTemplateStringOperation").FirstOrDefault();
            httpOperationDescription = operationDescription.ToHttpOperationDescription();
            template = httpOperationDescription.GetUriTemplate();
            Assert.AreEqual(0, template.PathSegmentVariableNames.Count, "HttpOperationDescription.GetUriTemplate should return a UriTemplate with zero path variables.");
            Assert.AreEqual(0, template.QueryValueVariableNames.Count, "HttpOperationDescription.GetUriTemplate should return a UriTemplate with zero query variables.");

            operationDescription     = contract.Operations.Where(od => od.Name == "WebInvokeWithTemplateStringOperation").FirstOrDefault();
            httpOperationDescription = operationDescription.ToHttpOperationDescription();
            template = httpOperationDescription.GetUriTemplate();
            Assert.AreEqual(1, template.PathSegmentVariableNames.Count, "HttpOperationDescription.GetUriTemplate should return a UriTemplate with one path variables.");
            Assert.AreEqual(1, template.QueryValueVariableNames.Count, "HttpOperationDescription.GetUriTemplate should return a UriTemplate with one query variables.");
            Assert.AreEqual("VARIABLE1", template.PathSegmentVariableNames[0], "HttpOperationDescription.GetUriTemplate should return a UriTemplate with query variable 'VARIABLE1'.");
            Assert.AreEqual("VARIABLE2", template.QueryValueVariableNames[0], "HttpOperationDescription.GetUriTemplate should return a UriTemplate with query variable 'VARIABLE2'.");

            operationDescription     = contract.Operations.Where(od => od.Name == "WebGetWithTemplateStringOperation").FirstOrDefault();
            httpOperationDescription = operationDescription.ToHttpOperationDescription();
            template = httpOperationDescription.GetUriTemplate();
            Assert.AreEqual(1, template.PathSegmentVariableNames.Count, "HttpOperationDescription.GetUriTemplate should return a UriTemplate with one path variables.");
            Assert.AreEqual(0, template.QueryValueVariableNames.Count, "HttpOperationDescription.GetUriTemplate should return a UriTemplate with zero query variables.");
            Assert.AreEqual("VARIABLE1", template.PathSegmentVariableNames[0], "HttpOperationDescription.GetUriTemplate should return a UriTemplate with query variable 'VARIABLE1'.");
        }
예제 #9
0
        public void HttpOperationDescription_Default_Ctor()
        {
            HttpOperationDescription hod = new HttpOperationDescription();

            HttpParameter returnValue = hod.ReturnValue;

            Assert.IsNull(returnValue, "ReturnValue should initialize to null");

            Collection <Attribute> attributes = hod.Attributes;

            Assert.IsNotNull(attributes, "Attributes should initialize to non-null");
            Assert.AreEqual(0, attributes.Count, "Attributes should initialize to empty collection");

            IList <HttpParameter> inputs = hod.InputParameters;

            Assert.IsNotNull(inputs, "InputParameters should initialize to non-null");
            Assert.AreEqual(0, inputs.Count, "InputParameters should initialize to empty collection");

            IList <HttpParameter> outputs = hod.OutputParameters;

            Assert.IsNotNull(outputs, "OutputParameters should initialize to non-null");
            Assert.AreEqual(0, outputs.Count, "OutputParameters should initialize to empty collection");

            KeyedByTypeCollection <IOperationBehavior> behaviors = hod.Behaviors;

            Assert.IsNotNull(behaviors, "Behaviors should initialize to non-null");
            Assert.AreEqual(0, behaviors.Count, "Behaviors should initialize to empty");

            Collection <Type> knownTypes = hod.KnownTypes;

            Assert.IsNotNull(knownTypes, "knownTypes should initialize to non-null");
            Assert.AreEqual(0, knownTypes.Count, "knownTypes should initialize to empty");

            Assert.IsNull(hod.DeclaringContract, "DeclaringContract should initialize to null");
        }
예제 #10
0
        // Asserts that an HttpOperationDescription created from the given OperationDescription
        // matches all public properties they have in common.   Also returns that HttpOperationDescription.
        private HttpOperationDescription AssertValidHttpOperationDescription(OperationDescription od)
        {
            HttpOperationDescription hod = od.ToHttpOperationDescription();

            this.AssertValidHttpOperationDescription(hod, od);
            return(hod);
        }
예제 #11
0
        internal OperationHandlerPipeline( 
            IEnumerable<HttpOperationHandler> requestHandlers,
            IEnumerable<HttpOperationHandler> responseHandlers,
            HttpOperationDescription operation)
        {
            if (requestHandlers == null)
            {
                throw Fx.Exception.ArgumentNull("requestHttpOperationHandlers");
            }

            if (responseHandlers == null)
            {
                throw Fx.Exception.ArgumentNull("responseHttpOperationHandlers");
            }

            if (operation == null)
            {
                throw Fx.Exception.ArgumentNull("operation");
            }

            this.requestHandlers = requestHandlers.ToArray();
            this.responseHandlers = responseHandlers.ToArray();

            this.requestHandlersCount = this.requestHandlers.Length;
            this.responseHandlersCount = this.responseHandlers.Length;

            this.pipelineContextInfo = new OperationHandlerPipelineInfo(this.requestHandlers, this.responseHandlers, operation);
        }
예제 #12
0
        private static void SetSerializerForJsonFormatter(HttpOperationDescription operation, Type type, string name, MediaTypeFormatterCollection formatters)
        {
            Fx.Assert(operation != null, "The 'operation' parameter should not be null.");
            Fx.Assert(type != null, "The 'type' parameter should not be null.");
            Fx.Assert(name != null, "The 'name' parameter should not be null.");
            Fx.Assert(formatters != null, "The 'formatters' parameter should not be null.");

            JsonMediaTypeFormatter jsonFormatter = formatters.JsonFormatter;

            if (jsonFormatter != null)
            {
                DataContractJsonSerializer jsonSerializer = null;
                DataContractSerializerOperationBehavior dataContractSerializerBehavior = operation.Behaviors.Find <DataContractSerializerOperationBehavior>();
                if (dataContractSerializerBehavior != null)
                {
                    jsonSerializer = new DataContractJsonSerializer(
                        type,
                        operation.KnownTypes,
                        dataContractSerializerBehavior.MaxItemsInObjectGraph,
                        dataContractSerializerBehavior.IgnoreExtensionDataObject,
                        dataContractSerializerBehavior.DataContractSurrogate,
                        false);
                }
                else
                {
                    jsonSerializer = new DataContractJsonSerializer(type, "root", operation.KnownTypes);
                }

                jsonFormatter.SetSerializer(type, jsonSerializer);
            }
        }
예제 #13
0
        public void HttpOperationDescription_All_Properties_Mutable()
        {
            HttpOperationDescription hod = new HttpOperationDescription();

            MethodInfo methodInfo = MethodInfo.GetCurrentMethod() as MethodInfo;

            ContractDescription cd = new ContractDescription("SampleContract");

            hod.DeclaringContract = cd;
            Assert.AreSame(cd, hod.DeclaringContract, "DeclaringContract was not settable");
            hod.DeclaringContract = null;
            Assert.IsNull(hod.DeclaringContract, "DeclaringContract was not resettable");

            Collection <Attribute> attributes = hod.Attributes;
            Attribute attr = new DescriptionAttribute("SampleAttr");

            attributes.Add(attr);
            Assert.AreEqual(1, hod.Attributes.Count, "Failed to add to Attributes");
            Assert.AreSame(attr, hod.Attributes[0], "Attribute added but not readable");

            Collection <Type> knownTypes = hod.KnownTypes;
            Type kt = this.GetType();

            knownTypes.Add(kt);
            Assert.AreEqual(1, hod.KnownTypes.Count, "Failed to add to KnownTypes");
            Assert.AreSame(kt, hod.KnownTypes[0], "KnownType added but not readable");

            KeyedByTypeCollection <IOperationBehavior> behaviors = hod.Behaviors;
            IOperationBehavior opBehavior = new MockOperationBehavior();

            behaviors.Add(opBehavior);
            Assert.AreEqual(1, hod.Behaviors.Count, "Failed to add to Behaviors");
            Assert.AreSame(opBehavior, hod.Behaviors[0], "Behaviors added but not readable");
        }
예제 #14
0
        public void HttpOperationDescription_Insert_OutputParameters_From_HttpOperationDescription()
        {
            OperationDescription             od      = GetOperationDescription(typeof(SimpleOperationsService), "OneInputOneOutputAndReturnValue");
            MessagePartDescriptionCollection mpdColl = od.Messages[1].Body.Parts;

            Assert.AreEqual(1, mpdColl.Count, "Test assumes we start with 1 output param");

            // Get a synchronized HOD and HODCollection
            HttpOperationDescription hod     = od.ToHttpOperationDescription();
            IList <HttpParameter>    hpdColl = hod.OutputParameters;

            // Get an HPD for the newly created MPD
            MessagePartDescription mpdNew = new MessagePartDescription("NewMPD", "NewMPDNS")
            {
                Type = typeof(byte)
            };
            HttpParameter hpd = mpdNew.ToHttpParameter();

            // Add it to the output parameters
            hpdColl.Add(hpd);

            // Verify it appears in the MPD coll
            Assert.AreEqual(2, mpdColl.Count, "Adding new MPD to HPD collection should have updated MPD collection");
            Assert.AreEqual(2, od.Messages[1].Body.Parts.Count, "Adding new MPD should have updated Parts");
            Assert.AreEqual(typeof(byte), od.Messages[1].Body.Parts[1].Type, "Adding new MPD failed due to type");
        }
예제 #15
0
        public void HttpOperationDescription_Multiple_OutputParameter_Matches_Method()
        {
            OperationDescription     od  = GetOperationDescription(typeof(SimpleOperationsService), "OneInputTwoOutputAndReturnValue");
            HttpOperationDescription hod = od.ToHttpOperationDescription();

            Assert.IsNotNull(hod, "Failed to create HttpOperationDescription");
            IList <HttpParameter> inputParameters = hod.InputParameters;

            Assert.IsNotNull(inputParameters, "InputParameters should not be null");
            Assert.AreEqual(1, inputParameters.Count, "Expected single input parameters");

            IList <HttpParameter> outputParameters = hod.OutputParameters;

            Assert.IsNotNull(outputParameters, "OutputParameters should not be null");
            Assert.AreEqual(2, outputParameters.Count, "Expected 2 output parameters");

            Assert.AreEqual(typeof(int), inputParameters[0].Type, "Expected int parameter1");
            Assert.AreEqual("parameter1", inputParameters[0].Name, "Expected parameter1 name match");

            Assert.AreEqual(typeof(double), outputParameters[0].Type, "Parameter2 incorrect type");
            Assert.AreEqual("parameter2", outputParameters[0].Name, "Expected parameter2 name match");

            Assert.AreEqual(typeof(char), outputParameters[1].Type, "Parameter3 incorrect type");
            Assert.AreEqual("parameter3", outputParameters[1].Name, "Expected parameter3 name match");
        }
예제 #16
0
        public void HttpOperationDescription_Extension_Method_Returns_HttpOperationDescription()
        {
            OperationDescription     od  = GetOperationDescription(typeof(SimpleOperationsService), "OneInputAndReturnValue");
            HttpOperationDescription hod = od.ToHttpOperationDescription();

            Assert.IsNotNull(hod, "Failed to create HttpOperationDescription");
            this.AssertValidHttpOperationDescription(hod, od);
        }
 public void RegisterResponseProcessorsForOperation(HttpOperationDescription operation, IList<Processor> processors, MediaTypeProcessorMode mode)
 {
     processors.Add(new JsonNetProcessor(operation, mode));
     processors.Add(new BsonProcessor(operation, mode));
     processors.Add(new PngProcessor(operation, mode));
     // TODO: How would you set this up to work for any type?
     processors.Add(new ViewEngineProcessor<Contact>(operation, mode, "Templates/"));
 }
 public JsonExtractHandler(HttpOperationDescription desc)
 {
     _prms =
         desc.InputParameters.Where(
             p => p.ParameterType.IsPrimitive 
                 || p.ParameterType == typeof(string)
                 ).ToArray();
 }
예제 #19
0
        /// <summary>
        /// Gets the <see cref="UriTemplate"/> associated with the given <paramref name="operation"/>.
        /// </summary>
        /// <param name="operation">The <see cref="HttpOperationDescription"/> instance.</param>
        /// <returns>The <see cref="UriTemplate"/>.</returns>
        public static UriTemplate GetUriTemplate(this HttpOperationDescription operation)
        {
            if (operation == null)
            {
                throw Fx.Exception.ArgumentNull("operation");
            }

            return(new UriTemplate(operation.GetUriTemplateStringOrDefault()));
        }
예제 #20
0
        /// <summary>
        /// Gets the <see cref="HttpMethod"/> for the given <paramref name="operation"/>.
        /// </summary>
        /// <param name="operation">The <see cref="HttpOperationDescription"/> instance.</param>
        /// <returns>The <see cref="HttpMethod"/>.</returns>
        public static HttpMethod GetHttpMethod(this HttpOperationDescription operation)
        {
            if (operation == null)
            {
                throw Fx.Exception.ArgumentNull("operation");
            }

            return(new HttpMethod(GetWebMethod(operation)));
        }
예제 #21
0
        private static void SetSerializerForXmlFormatter(HttpOperationDescription operation, Type type, MediaTypeFormatterCollection formatters)
        {
            Fx.Assert(operation != null, "The 'operation' parameter should not be null.");
            Fx.Assert(formatters != null, "The 'formatters' parameter should not be null.");

            bool useXmlSerializer        = DetermineXmlFormat(operation);
            Collection <Type> knownTypes = operation.KnownTypes;
            bool isQueryableType         = false;

            XmlMediaTypeFormatter xmlFormatter = formatters.XmlFormatter;

            if (xmlFormatter != null)
            {
                if (useXmlSerializer)
                {
                    //FIX: GB - IQueryable
                    if (type.IsGenericType)
                    {
                        var genericTypeDef = type.GetGenericTypeDefinition();
                        if (genericTypeDef == typeof(IQueryable <>))
                        {
                            type            = typeof(List <>).MakeGenericType(type.GetGenericArguments());
                            isQueryableType = true;
                        }
                    }

                    XmlSerializer xmlSerializer = knownTypes.Count > 0
                                                          ? new XmlSerializer(type, knownTypes.ToArray())
                                                          : new XmlSerializer(type);
                    xmlFormatter.SetSerializer(type, xmlSerializer, isQueryableType);
                }
                else
                {
                    DataContractSerializerOperationBehavior behavior = operation.Behaviors.Find <DataContractSerializerOperationBehavior>();
                    XmlObjectSerializer xmlObjectSerializer          = null;
                    if (behavior != null)
                    {
                        xmlObjectSerializer = new DataContractSerializer(
                            type,
                            knownTypes,
                            behavior.MaxItemsInObjectGraph,
                            behavior.IgnoreExtensionDataObject,
                            false,
                            behavior.DataContractSurrogate,
                            behavior.DataContractResolver);
                    }
                    else
                    {
                        xmlObjectSerializer = new DataContractSerializer(type, knownTypes);
                    }

                    xmlFormatter.SetSerializer(type, xmlObjectSerializer);
                }
            }
        }
예제 #22
0
        public void HttpOperationDescription_Returns_Attributes()
        {
            OperationDescription     od  = GetOperationDescription(typeof(SimpleOperationsService), "ZeroInputsAndReturnsVoid");
            HttpOperationDescription hod = od.ToHttpOperationDescription();

            Assert.IsNotNull(hod, "Failed to create HttpOperationDescription");
            IEnumerable <Attribute> attributes = hod.Attributes;

            Assert.IsNotNull(attributes, "Attributes were null");
            Assert.IsTrue(attributes.OfType <DescriptionAttribute>().Any(), "Did not discover [Description] attribute");
        }
예제 #23
0
        public void HttpOperationDescription_Return_Parameter_Matches_Void_Method()
        {
            OperationDescription     od  = GetOperationDescription(typeof(SimpleOperationsService), "ZeroInputsAndReturnsVoid");
            HttpOperationDescription hod = od.ToHttpOperationDescription();

            Assert.IsNotNull(hod, "Failed to create HttpOperationDescription");
            HttpParameter returnParameter = hod.ReturnValue;

            Assert.IsNotNull(returnParameter, "Return parameter was null");
            Assert.AreEqual(typeof(void), returnParameter.Type, "Return parameter type should have been void");
        }
예제 #24
0
        private static bool DetermineXmlFormat(HttpOperationDescription operation)
        {
            Fx.Assert(operation != null, "The 'operation' parameter should not be null.");

            DataContractFormatAttribute  dataContract  = null;
            XmlSerializerFormatAttribute xmlSerializer = null;

            foreach (Attribute attribute in operation.Attributes)
            {
                if (dataContract == null)
                {
                    dataContract = attribute as DataContractFormatAttribute;
                }

                if (xmlSerializer == null)
                {
                    xmlSerializer = attribute as XmlSerializerFormatAttribute;
                }
            }

            if (xmlSerializer == null && dataContract != null)
            {
                return(false);
            }

            ContractDescription contract = operation.DeclaringContract;

            if (contract != null)
            {
                Type contractType = contract.ContractType;
                if (contractType != null)
                {
                    foreach (Attribute attribute in contractType.GetCustomAttributes(true).Cast <Attribute>())
                    {
                        if (dataContract == null)
                        {
                            dataContract = attribute as DataContractFormatAttribute;
                        }

                        if (xmlSerializer == null)
                        {
                            xmlSerializer = attribute as XmlSerializerFormatAttribute;
                        }
                    }

                    if (xmlSerializer == null && dataContract != null)
                    {
                        return(false);
                    }
                }
            }

            return(true);
        }
예제 #25
0
        private static OperationDescription CreateEmptyOperationDescription(OperationDescription other)
        {
            OperationDescription     copy        = ClientContractDescriptionHelper.GetCopyOfOperationDescription(other);
            HttpOperationDescription description = copy.ToHttpOperationDescription();

            // delete all parameters
            description.InputParameters.Clear();
            description.OutputParameters.Clear();

            return(copy);
        }
예제 #26
0
        // Verifies that a HttpOperationDescription built from an OperationDescription
        // matches the given MethodInfo for return type, parameters, and attributes
        private void AssertHttpOperationDescriptionMatchesMethod(HttpOperationDescription hod, MethodInfo method)
        {
            Assert.IsNotNull(hod, "HttpOperationDescription was null");
            Assert.AreEqual(hod.Name, method.Name, "Name mismatch");

            HttpParameter returnParameter = hod.ReturnValue;

            if (!hod.ToOperationDescription().IsOneWay)
            {
                Assert.AreEqual(returnParameter.Type, method.ReturnType, "Return type mismatch");
            }

            IList <HttpParameter> inputParameters = hod.InputParameters;

            ParameterInfo[] parameters = method.GetParameters().Where(p => !p.IsOut).ToArray();
            Assert.AreEqual(parameters.Length, inputParameters.Count, "Input parameter count mismatch");

            for (int i = 0; i < parameters.Length; ++i)
            {
                Assert.AreEqual(parameters[i].Name, inputParameters[i].Name, "Input parameter name mismatch");
                Assert.AreEqual(parameters[i].ParameterType, inputParameters[i].Type, "Input parameter type mismatch");
            }

            IList <HttpParameter> outputParameters = hod.OutputParameters;

            parameters = method.GetParameters().Where(p => p.IsOut).ToArray();
            Assert.AreEqual(parameters.Length, outputParameters.Count, "Output parameter count mismatch");

            for (int i = 0; i < parameters.Length; ++i)
            {
                Assert.AreEqual(parameters[i].Name, outputParameters[i].Name, "Output parameter name mismatch");

                // ServiceModel removes the ByRef part
                Type t = parameters[i].ParameterType;
                if (t.HasElementType && t.IsByRef)
                {
                    t = t.GetElementType();
                }
                Assert.AreEqual(t, outputParameters[i].Type, "Output parameter type mismatch");
            }

            IEnumerable <Attribute> hodAttributes    = hod.Attributes;
            IEnumerable <Attribute> methodAttributes = method.GetCustomAttributes(false).Cast <Attribute>().ToArray();

            foreach (Attribute a in methodAttributes)
            {
                if (!hodAttributes.Contains(a))
                {
                    Assert.Fail("Did not find attribute " + a.GetType().Name + " on method " + method.Name);
                }
            }
        }
예제 #27
0
        private void AssertAllHttpOperationDescriptions(Type contractType)
        {
            ContractDescription cd = ContractDescription.GetContract(contractType);

            MethodInfo[] methods = contractType.GetMethods().Where(m => m.GetCustomAttributes(typeof(OperationContractAttribute), false).Any()).ToArray();
            Assert.AreEqual(methods.Length, cd.Operations.Count, "Number of operations did not match our MethodInfo count");
            for (int i = 0; i < methods.Length; ++i)
            {
                OperationDescription     od  = cd.Operations[i];
                HttpOperationDescription hod = this.AssertValidHttpOperationDescription(od);
                this.AssertHttpOperationDescriptionMatchesMethod(hod, methods[i]);
            }
        }
예제 #28
0
        public void HttpOperationDescription_OutputParameter_Matches_Method()
        {
            OperationDescription     od  = GetOperationDescription(typeof(SimpleOperationsService), "OneInputOneOutputAndReturnValue");
            HttpOperationDescription hod = od.ToHttpOperationDescription();

            Assert.IsNotNull(hod, "Failed to create HttpOperationDescription");
            IList <HttpParameter> parameters = hod.OutputParameters;

            Assert.IsNotNull(parameters, "OutputParameters should not be null");
            Assert.AreEqual(1, parameters.Count, "Expected only one parameter");
            Assert.AreEqual(typeof(double), parameters[0].Type, "Expected out parameter of type out double");
            Assert.AreEqual("parameter2", parameters[0].Name, "Expected parameter name match");
        }
예제 #29
0
        public void HttpOperationDescription_Return_Parameter_Matches_Method()
        {
            OperationDescription     od  = GetOperationDescription(typeof(SimpleOperationsService), "OneInputAndReturnValue");
            HttpOperationDescription hod = od.ToHttpOperationDescription();

            Assert.IsNotNull(hod, "Failed to create HttpOperationDescription");
            HttpParameter returnParameter = hod.ReturnValue;

            Assert.IsNotNull(returnParameter, "Return parameter was null");
            Assert.AreEqual(typeof(string), returnParameter.Type, "Return parameter type should have been string");

            Assert.AreEqual("OneInputAndReturnValueResult", returnParameter.Name, "Return parameter name match operation + Result");
        }
예제 #30
0
        public void HttpOperationDescription_Unsynchronized_ReturnValue_Setter()
        {
            HttpOperationDescription hod = new HttpOperationDescription();

            HttpParameter returnValue = new HttpParameter("TheReturn", typeof(string));

            hod.ReturnValue = returnValue;

            Assert.AreSame(returnValue, hod.ReturnValue, "Failed to set return value");

            hod.ReturnValue = null;
            Assert.IsNull(hod.ReturnValue, "Failed to reset return value");
        }
예제 #31
0
        public static List<HttpOperationDescription> GenerateOperationsList2()
        {
            List<HttpOperationDescription> operationList = new List<HttpOperationDescription>();
            foreach (KeyValuePair<string, UriTemplate> item in serviceList2)
            {
                HttpOperationDescription data = new HttpOperationDescription();
                data.Name = item.Key;
                data.Behaviors.Add(new WebGetAttribute() { UriTemplate = item.Value.ToString() });
                operationList.Add(data);
            }

            return operationList;
        }
예제 #32
0
        private static void SetXmlAndJsonSerializers(HttpOperationDescription operation, HttpParameter httpParameter, MediaTypeFormatterCollection formatters)
        {
            Fx.Assert(operation != null, "The 'operation' parameter should not be null.");
            Fx.Assert(httpParameter != null, "The 'httpParameter' parameter should not be null.");
            Fx.Assert(formatters != null, "The 'formatters' parameter should not be null.");

            Type contentType = HttpTypeHelper.GetHttpInnerTypeOrNull(httpParameter.Type) ?? httpParameter.Type;

            if (contentType != typeof(JsonValue) && !HttpTypeHelper.IsHttp(contentType))
            {
                SetSerializerForXmlFormatter(operation, contentType, formatters);
                SetSerializerForJsonFormatter(operation, contentType, httpParameter.Name, formatters);
            }
        }
예제 #33
0
        public void HttpOperationDescription_From_Empty_OperationDescription_Supported()
        {
            OperationDescription     od  = new OperationDescription("name", new ContractDescription("name", "namespace"));
            HttpOperationDescription hod = od.ToHttpOperationDescription();

            Assert.IsNotNull(hod, "Failed to create HttpOperationDescription");

            Assert.IsNull(hod.ReturnValue, "Return value should be null");

            Assert.IsNotNull(hod.InputParameters, "Input parameters should not be null");
            Assert.IsFalse(hod.InputParameters.Any(), "Input parameter list should be empty");

            Assert.IsNotNull(hod.OutputParameters, "Output parameters should not be null");
            Assert.IsFalse(hod.OutputParameters.Any(), "Output parameter list should be empty");

            Assert.IsNotNull(hod.Attributes, "Attributes should not be null");
            Assert.IsFalse(hod.Attributes.Any(), "Attributes list should be empty");
        }
예제 #34
0
        private static string GetUriTemplateStringOrDefault(this HttpOperationDescription operation)
        {
            Fx.Assert(operation != null, "The 'operation' parameter should not be null.");

            string webUriTemplate = GetWebUriTemplate(operation);

            if ((webUriTemplate == null) && (GetWebMethod(operation) == HttpMethod.Get.ToString()))
            {
                webUriTemplate = MakeDefaultGetUriTemplateString(operation);
            }

            if (webUriTemplate == null)
            {
                webUriTemplate = operation.Name;
            }

            return(webUriTemplate);
        }
예제 #35
0
        public void GetHttpMethod_Returns_HttpMethod()
        {
            ExceptionAssert.ThrowsArgumentNull("operation", () => ((HttpOperationDescription)null).GetHttpMethod());

            ContractDescription contract = ContractDescription.GetContract(typeof(WebMethodService));

            OperationDescription     operationDescription     = contract.Operations.Where(od => od.Name == "NoAttributeOperation").FirstOrDefault();
            HttpOperationDescription httpOperationDescription = operationDescription.ToHttpOperationDescription();

            Assert.AreEqual(HttpMethod.Post, httpOperationDescription.GetHttpMethod(), "HttpOperationDescription.GetHttpMethod should return 'POST' for operations with no WebGet or WebInvoke attribute.");

            operationDescription     = contract.Operations.Where(od => od.Name == "WebInvokeOperation").FirstOrDefault();
            httpOperationDescription = operationDescription.ToHttpOperationDescription();
            Assert.AreEqual(HttpMethod.Post, httpOperationDescription.GetHttpMethod(), "HttpOperationDescription.GetHttpMethod should return 'POST' for operations with WebInvoke attribute but no Method set explicitly.");

            operationDescription     = contract.Operations.Where(od => od.Name == "WebGetOperation").FirstOrDefault();
            httpOperationDescription = operationDescription.ToHttpOperationDescription();
            Assert.AreEqual(HttpMethod.Get, httpOperationDescription.GetHttpMethod(), "HttpOperationDescription.GetHttpMethod should return 'GET' for operations with WebGet.");

            operationDescription     = contract.Operations.Where(od => od.Name == "WebInvokeGetOperation").FirstOrDefault();
            httpOperationDescription = operationDescription.ToHttpOperationDescription();
            Assert.AreEqual(HttpMethod.Get, httpOperationDescription.GetHttpMethod(), "HttpOperationDescription.GetHttpMethod should have return 'GET'.");

            operationDescription     = contract.Operations.Where(od => od.Name == "WebInvokeGetLowerCaseOperation").FirstOrDefault();
            httpOperationDescription = operationDescription.ToHttpOperationDescription();
            Assert.AreEqual(new HttpMethod("Get"), httpOperationDescription.GetHttpMethod(), "HttpOperationDescription.GetHttpMethod should have return 'Get'.");

            operationDescription     = contract.Operations.Where(od => od.Name == "WebInvokePutOperation").FirstOrDefault();
            httpOperationDescription = operationDescription.ToHttpOperationDescription();
            Assert.AreEqual(HttpMethod.Put, httpOperationDescription.GetHttpMethod(), "HttpOperationDescription.GetHttpMethod should have return 'PUT'.");

            operationDescription     = contract.Operations.Where(od => od.Name == "WebInvokePostOperation").FirstOrDefault();
            httpOperationDescription = operationDescription.ToHttpOperationDescription();
            Assert.AreEqual(HttpMethod.Post, httpOperationDescription.GetHttpMethod(), "HttpOperationDescription.GetHttpMethod should have return 'POST'.");

            operationDescription     = contract.Operations.Where(od => od.Name == "WebInvokeDeleteOperation").FirstOrDefault();
            httpOperationDescription = operationDescription.ToHttpOperationDescription();
            Assert.AreEqual(HttpMethod.Delete, httpOperationDescription.GetHttpMethod(), "HttpOperationDescription.GetHttpMethod should have return 'DELETE'.");

            operationDescription     = contract.Operations.Where(od => od.Name == "WebInvokeCustomOperation").FirstOrDefault();
            httpOperationDescription = operationDescription.ToHttpOperationDescription();
            Assert.AreEqual(new HttpMethod("Custom"), httpOperationDescription.GetHttpMethod(), "HttpOperationDescription.GetHttpMethod should have return 'Custom'.");
        }
예제 #36
0
        public void HttpOperationDescription_Update_ReturnValue_From_Incomplete_HttpOperationDescription()
        {
            OperationDescription     od  = GetOperationDescription(typeof(SimpleOperationsService), "OneInputAndReturnValue");
            MessagePartDescription   mpd = od.Messages[1].Body.ReturnValue;
            HttpOperationDescription hod = od.ToHttpOperationDescription();
            HttpParameter            hpd = hod.ReturnValue;

            // Clear out all of Messages[]
            od.Messages.Clear();

            Assert.IsNull(hod.ReturnValue, "ReturnValue should be null with no backing Messages[1]");

            // Setting a valid ReturnValue should auto-create Messages[1]
            hod.ReturnValue = hpd;

            Assert.IsNotNull(hod.ReturnValue, "ReturnValue was not set");
            Assert.AreSame(hpd.MessagePartDescription, hod.ReturnValue.MessagePartDescription, "ReturnValue not as expected");

            Assert.AreEqual(2, od.Messages.Count, "Setting ReturnValue should have created Messages[1]");
        }
예제 #37
0
        internal OperationHandlerPipelineInfo(
            IEnumerable<HttpOperationHandler> requestHandlers,
            IEnumerable<HttpOperationHandler> responseHandlers,
            HttpOperationDescription operation)
        {
            if (requestHandlers == null)
            {
                throw Fx.Exception.ArgumentNull("requestHandlers");
            }

            if (responseHandlers == null)
            {
                throw Fx.Exception.ArgumentNull("responseHandlers");
            }

            if (operation == null)
            {
                throw Fx.Exception.ArgumentNull("operation");
            }

            this.serviceOperationHandler = new ServiceOperationHandler(operation);

            List<HttpOperationHandler> handlers = new List<HttpOperationHandler>();
            handlers.Add(requestMessageSourceHandler);
            handlers.AddRange(requestHandlers);

            this.serviceOperationIndex = handlers.Count;

            handlers.Add(this.serviceOperationHandler);
            handlers.AddRange(responseHandlers);
            handlers.Add(responseMessageSinkHandler);

            string operationName = string.IsNullOrWhiteSpace(operation.Name) ?
                SR.UnknownName : operation.Name;

            List<HttpParameterBinding> bindings = BindHandlers(handlers, operationName, this.serviceOperationIndex);

            this.operationHandlerInfo = GenerateHandlerInfo(handlers, bindings, operationName, out this.pipelineValuesArraySize);
        }
 public TheOperationHandler(HttpOperationDescription desc)
 {
     _desc = desc;
 }
 public void RegisterRequestProcessorsForOperation(ICollection<HttpOperationHandler> handlers, HttpOperationDescription operation )
 {
     processors.Add(new JsonNetProcessor(operation, mode));
     processors.Add(new BsonProcessor(operation, mode));
     processors.Add(new FormUrlEncodedProcessor(operation, mode));
 }
예제 #40
0
파일: Policy.cs 프로젝트: pmhsfelix/Waaz
 public HttpOperationHandler GetEnforcementHandlerFor(HttpOperationDescription od)
 {
     return
         new EnforcementHttpOperationHandler(
             _ruleSets.Where(rs => rs.OperationPredicate.Func(od)).SelectMany(rs => rs.Rules));
 }
 public LoggingOperationHandler(HttpOperationDescription desc)
 {
     _desc = desc;
 }
예제 #42
0
 internal ServiceOperationHandler(HttpOperationDescription httpOperationDescription)
 {
     this.httpOperationDescription = httpOperationDescription;
 }