public void HttpOperationDescription_Update_ReturnValue_From_OperationDescription() { OperationDescription od = GetOperationDescription(typeof(MockService2), "GetAStringFromInt"); HttpOperationDescription hod = od.ToHttpOperationDescription(); Assert.IsNotNull(hod, "Failed to create HttpOperationDescription"); HttpParameterDescription returnParameter = hod.ReturnValue; Assert.IsNotNull(returnParameter, "Return parameter was null"); Assert.AreEqual(typeof(string), returnParameter.ParameterType, "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.ParameterType, "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.ParameterType, "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"); }
/// <summary> /// /// </summary> public JsonNetProcessor(HttpOperationDescription operation, MediaTypeProcessorMode mode) : base(operation, mode) { if (this.Parameter != null) { this.parameterType = this.Parameter.ParameterType; } }
public void HttpOperationDescription_Multiple_OutputParameter_Matches_Method() { OperationDescription od = GetOperationDescription(typeof(MockService2), "OneInMultipleOutReturnsString"); HttpOperationDescription hod = od.ToHttpOperationDescription(); Assert.IsNotNull(hod, "Failed to create HttpOperationDescription"); IList <HttpParameterDescription> inputParameters = hod.InputParameters; Assert.IsNotNull(inputParameters, "InputParameters should not be null"); Assert.AreEqual(1, inputParameters.Count, "Expected single input parameters"); IList <HttpParameterDescription> 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].ParameterType, "Expected int parameter1"); Assert.AreEqual("parameter1", inputParameters[0].Name, "Expected parameter1 name match"); Assert.AreEqual(0, inputParameters[0].Index, "Parameter1 index wrong"); Assert.AreEqual(TestNamespace, inputParameters[0].Namespace, "Parameter1 namespace wrong"); Assert.AreEqual(typeof(double), outputParameters[0].ParameterType, "Parameter2 incorrect type"); Assert.AreEqual("parameter2", outputParameters[0].Name, "Expected parameter2 name match"); Assert.AreEqual(0, outputParameters[0].Index, "Parameter2 index wrong"); Assert.AreEqual(TestNamespace, outputParameters[0].Namespace, "Parameter2 namespace wrong"); Assert.AreEqual(typeof(char), outputParameters[1].ParameterType, "Parameter3 incorrect type"); Assert.AreEqual("parameter3", outputParameters[1].Name, "Expected parameter3 name match"); Assert.AreEqual(1, outputParameters[1].Index, "Parameter3 index wrong"); Assert.AreEqual(TestNamespace, outputParameters[1].Namespace, "Parameter3 namespace wrong"); }
public static string GetUriTemplateString(this HttpOperationDescription operation) { if (operation == null) { throw new ArgumentNullException("operation"); } WebGetAttribute webGet = operation.GetWebGetAttribute(); WebInvokeAttribute webInvoke = operation.GetWebInvokeAttribute(); if (webGet != null && webGet.UriTemplate != null) { return(webGet.UriTemplate); } if (webInvoke != null && webInvoke.UriTemplate != null) { return(webInvoke.UriTemplate); } if (operation.GetWebMethod() == "GET") { return(GetDefaultWebGetUriTemplate(operation)); } return(operation.Name); }
// 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); }
public void HttpOperationDescription_Insert_OutputParameters_From_HttpOperationDescription() { OperationDescription od = GetOperationDescription(typeof(MockService2), "OneInOneOutReturnsString"); 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(); HttpParameterDescriptionCollection hpdColl = hod.OutputParameters; // Get an HPD for the newly created MPD MessagePartDescription mpdNew = new MessagePartDescription("NewMPD", "NewMPDNS") { Type = typeof(byte) }; HttpParameterDescription hpd = mpdNew.ToHttpParameterDescription(); // 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"); }
public void HttpOperationDescription_Multiple_InputParameter_Matches_Method() { OperationDescription od = GetOperationDescription(typeof(MockService2), "GetAStringFromMultiple"); HttpOperationDescription hod = od.ToHttpOperationDescription(); Assert.IsNotNull(hod, "Failed to create HttpOperationDescription"); IList <HttpParameterDescription> parameters = hod.InputParameters; Assert.IsNotNull(parameters, "InputParameters should not be null"); Assert.AreEqual(3, parameters.Count, "Expected multiple parameters"); Assert.AreEqual(typeof(int), parameters[0].ParameterType, "Expected int parameter1"); Assert.AreEqual("parameter1", parameters[0].Name, "Expected parameter1 name match"); Assert.AreEqual(0, parameters[0].Index, "Parameter1 index wrong"); Assert.AreEqual(TestNamespace, parameters[0].Namespace, "Parameter1 namespace wrong"); Assert.AreEqual(typeof(double), parameters[1].ParameterType, "Expected double parameter2"); Assert.AreEqual("parameter2", parameters[1].Name, "Expected parameter2 name match"); Assert.AreEqual(1, parameters[1].Index, "Parameter2 index wrong"); Assert.AreEqual(TestNamespace, parameters[1].Namespace, "Parameter2 namespace wrong"); Assert.AreEqual(typeof(string), parameters[2].ParameterType, "Expected string parameter3"); Assert.AreEqual("parameter3", parameters[2].Name, "Expected parameter3 name match"); Assert.AreEqual(2, parameters[2].Index, "Parameter3 index wrong"); Assert.AreEqual(TestNamespace, parameters[2].Namespace, "Parameter3 namespace wrong"); }
public CustomMessageFormatter(HttpOperationDescription httpOperation) { if (httpOperation == null) { throw new ArgumentNullException("httpOperation"); } if (httpOperation.InputParameters.Count == 1 && httpOperation.InputParameters[0].ParameterType == 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.ParameterType == typeof(HttpResponseMessage)) { this.hasResponseMessage = true; } else if (httpOperation.ReturnValue.ParameterType != typeof(void)) { throw new NotSupportedException( "The MessageFormatter only supports an optional return type of 'HttpResponseMessage'."); } }
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); }
// Helper to validate input and parameters private void AssertValidateParameters(string operationName, ContractDescription contractForHelpPage, Dictionary <string, Type> parameters, bool isInput) { OperationDescription operationDescription = contractForHelpPage.Operations.Where(od => od.Name == operationName).FirstOrDefault(); HttpOperationDescription httpOperationDescription = operationDescription.ToHttpOperationDescription(); HttpParameter[] httpParameterDescriptions; if (isInput) { httpParameterDescriptions = httpOperationDescription.InputParameters.ToArray(); } else { httpParameterDescriptions = httpOperationDescription.OutputParameters.ToArray(); } Assert.AreEqual(parameters.Count, httpParameterDescriptions.Length, "Operation " + operationName + " found incorrect number of " + (isInput? "input" : "output") + " parameters"); for (int i = 0; i < httpParameterDescriptions.Length; i++) { Type type; if (!parameters.TryGetValue(httpParameterDescriptions[i].Name, out type)) { Assert.Fail("Expected {0} parameter {1} not found in operation {2}", isInput? "input" : "output", httpParameterDescriptions[i].Name, operationName); } if (type != httpParameterDescriptions[i].Type) { Assert.Fail("Type mismatch for {0} parameter {1} in operation {2}, Found {3}, Expected {4}", isInput ? "input" : "output", httpParameterDescriptions[i].Name, operationName, httpParameterDescriptions[i].Type, type); } } }
public void HttpOperationDescription_Update_ReturnValue_From_Incomplete_HttpOperationDescription() { OperationDescription od = GetOperationDescription(typeof(MockService2), "GetAStringFromInt"); MessagePartDescription mpd = od.Messages[1].Body.ReturnValue; HttpOperationDescription hod = od.ToHttpOperationDescription(); HttpParameterDescription hpd = hod.ReturnValue; // Clear out all of Messages[] od.Messages.Clear(); Assert.IsNull(hod.ReturnValue, "ReturnValue should be null with no backing Messages[1]"); // Attempting to set a mock HPD should throw ExceptionAssert.ThrowsInvalidOperation( "Setting unsynchronized HPD to ReturnValue should throw", () => hod.ReturnValue = new HttpParameterDescription() { ParameterType = typeof(string) } ); // 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]"); }
public void HttpOperationDescription_Extension_Method_Returns_HttpOperationDescription() { OperationDescription od = GetOperationDescription(typeof(MockService2), "GetAStringFromInt"); HttpOperationDescription hod = od.ToHttpOperationDescription(); Assert.IsNotNull(hod, "Failed to create HttpOperationDescription"); this.AssertValidHttpOperationDescription(hod, od); }
public JsonExtractHandler(HttpOperationDescription desc) { _prms = desc.InputParameters.Where( p => p.ParameterType.IsPrimitive || p.ParameterType == typeof(string) ).ToArray(); }
// Helper to return Type private void AssertValidateReturnParameter(string operationName, ContractDescription contractForHelpPage, Type returnType) { OperationDescription operationDescription = contractForHelpPage.Operations.Where(od => od.Name == operationName).FirstOrDefault(); HttpOperationDescription httpOperationDescription = operationDescription.ToHttpOperationDescription(); HttpParameter returnParameter = httpOperationDescription.ReturnValue; Assert.AreEqual(returnType, returnParameter.Type, "Return type is not matching"); }
protected virtual HttpMessageFormatter OnGetFormatter(HttpOperationDescription description, DispatchOperation operation) { var requestProcessors = this.GetRequestProcessors(description); var responseProcessors = this.GetResponseProcessors(description); var pipelineFormatter = new HttpPipelineFormatter(requestProcessors, responseProcessors, description); pipelineFormatter.Initialize(); return(pipelineFormatter); }
public void HttpOperationDescription_Returns_Attributes() { OperationDescription od = GetOperationDescription(typeof(MockService2), "MethodWithAttribute"); 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"); }
public void HttpOperationDescription_Return_Parameter_Matches_Void_Method() { OperationDescription od = GetOperationDescription(typeof(MockService2), "NoParametersReturnsVoid"); HttpOperationDescription hod = od.ToHttpOperationDescription(); Assert.IsNotNull(hod, "Failed to create HttpOperationDescription"); HttpParameterDescription returnParameter = hod.ReturnValue; Assert.IsNotNull(returnParameter, "Return parameter was null"); Assert.AreEqual(typeof(void), returnParameter.ParameterType, "Return parameter type should have been void"); }
public void HttpOperationDescription_Extension_Method_Null_Throws() { OperationDescription od = null; ExceptionAssert.ThrowsArgumentNull( "Null operation description should throw ArgumentNull", "description", () => { HttpOperationDescription hod = od.ToHttpOperationDescription(); }); }
public void HttpOperationDescription_Can_Be_Created_From_Simple_Types() { HttpOperationDescription hod = new HttpOperationDescription(); hod.ReturnValue = new HttpParameterDescription() { Name = "Return", Namespace = "ReturnNS", ParameterType = typeof(string), Index = 0 }; hod.InputParameters.Add(new HttpParameterDescription() { Name = "InParameter", Namespace = "InParameterNS", ParameterType = typeof(int), Index = 0 }); hod.OutputParameters.Add(new HttpParameterDescription() { Name = "OutParameter", Namespace = "OutParameterNS", ParameterType = typeof(string), Index = 1 }); HttpParameterDescription parmDesc = hod.ReturnValue; Assert.AreEqual("Return", parmDesc.Name, "ReturnValue.Name incorrect"); Assert.AreEqual("ReturnNS", parmDesc.Namespace, "ReturnValue.Namespace incorrect"); Assert.AreEqual(typeof(string), parmDesc.ParameterType, "ReturnValue.ParameterType incorrect"); Assert.AreEqual(0, parmDesc.Index, "ReturnValue.Index incorrect"); HttpParameterDescriptionCollection 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("InParameterNS", parmDesc.Namespace, "InputParameter.Namespace incorrect"); Assert.AreEqual(typeof(int), parmDesc.ParameterType, "InputParameter.ParameterType incorrect"); Assert.AreEqual(0, parmDesc.Index, "InputParameter.Index 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("OutParameterNS", parmDesc.Namespace, "OutParameter.Namespace incorrect"); Assert.AreEqual(typeof(string), parmDesc.ParameterType, "OutParameter.ParameterType incorrect"); Assert.AreEqual(1, parmDesc.Index, "OutParameter.Index incorrect"); }
public XmlProcessor(HttpOperationDescription operation, MediaTypeProcessorMode mode) : base(operation, mode) { var returnType = operation.ReturnValue; //IQueryable support if (operation.Behaviors.Contains(typeof(QueryCompositionAttribute))) { usesQueryComposition = true; var queryCompositionItemType = operation.ReturnValue.ParameterType.GetGenericArguments()[0]; queryCompositionType = typeof(List <>).MakeGenericType(queryCompositionItemType); } }
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]); } }
public void RegisterResponseProcessorsForOperation(HttpOperationDescription operation, IList <Processor> processors, MediaTypeProcessorMode mode) { var xmlProcessor = processors.OfType <XmlProcessor>().FirstOrDefault(); if (xmlProcessor != null) { processors.Remove(xmlProcessor); } processors.Add(new DataContractXmlProcessor(operation, mode)); processors.Add(new JsonProcessor(operation, mode)); processors.Add(new ProtocolBufferProcessor(operation, mode)); }
protected override Collection<HttpOperationHandler> OnCreateRequestHandlers(ServiceEndpoint endpoint, HttpOperationDescription operation) { var coll = base.OnCreateRequestHandlers(endpoint, operation); coll.Add(new LoggingOperationHandler(operation)); if (operation.Name == "GetTimeString") { Formatters.Remove(Formatters.XmlFormatter); Formatters.Remove(Formatters.JsonFormatter); Formatters.Add(new WaveFromTextFormatter()); Formatters.Add(new ImageFromTextFormatter()); } return coll; }
public Processor[] GetRequestProcessors(HttpOperationDescription operation) { var processors = new List <Processor>(); processors.Add(new UriTemplateProcessor(this.baseAddress, new UriTemplate(operation.GetUriTemplateString()))); processors.Add(new XmlProcessor(operation, MediaTypeProcessorMode.Request)); if (this.processorProvider != null) { this.processorProvider.RegisterRequestProcessorsForOperation(operation, processors, MediaTypeProcessorMode.Request); } return(processors.ToArray()); }
public FormUrlEncodedProcessor(HttpOperationDescription operation, MediaTypeProcessorMode mode) : base(operation, MediaTypeProcessorMode.Request) { if (mode == MediaTypeProcessorMode.Response) { throw new ArgumentException("mode", "This processor cannot be used in the response"); } if (this.Parameter != null) { this.parameterType = Parameter.ParameterType; this.isJsonValueParameter = typeof(JsonValue).IsAssignableFrom(this.parameterType); } }
protected override System.Collections.ObjectModel.Collection<HttpOperationHandler> OnCreateResponseHandlers(ServiceEndpoint endpoint, HttpOperationDescription operation) { var handlers = new Collection<HttpOperationHandler>(); handlers = base.OnCreateResponseHandlers(endpoint, operation); foreach (var handlerItem in this.createResponseHandlers) { if (handlerItem.Condition(endpoint, operation)) { handlerItem.Handlers(handlers); } } return handlers; }
public void HttpOperationDescription_All_Properties_Mutable() { HttpOperationDescription hod = new HttpOperationDescription(); MethodInfo methodInfo = MethodInfo.GetCurrentMethod() as MethodInfo; hod.BeginMethod = methodInfo; Assert.AreSame(methodInfo, hod.BeginMethod, "BeginMethod was not settable"); hod.BeginMethod = null; Assert.IsNull(hod.BeginMethod, "BeginMethod was not resettable"); hod.EndMethod = methodInfo; Assert.AreSame(methodInfo, hod.EndMethod, "EndMethod was not settable"); hod.EndMethod = null; Assert.IsNull(hod.EndMethod, "EndMethod was not resettable"); hod.SyncMethod = methodInfo; Assert.AreSame(methodInfo, hod.SyncMethod, "SyncMethod was not settable"); hod.SyncMethod = null; Assert.IsNull(hod.SyncMethod, "SyncMethod was not resettable"); 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"); }
// 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"); HttpParameterDescription returnParameter = hod.ReturnValue; Assert.AreEqual(returnParameter.ParameterType, method.ReturnType, "Return type mismatch"); IList <HttpParameterDescription> 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].ParameterType, "Input parameter type mismatch"); } IList <HttpParameterDescription> 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].ParameterType, "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); } } }
public void HttpOperationDescription_Return_Parameter_Matches_Method() { OperationDescription od = GetOperationDescription(typeof(MockService2), "GetAStringFromInt"); HttpOperationDescription hod = od.ToHttpOperationDescription(); Assert.IsNotNull(hod, "Failed to create HttpOperationDescription"); HttpParameterDescription returnParameter = hod.ReturnValue; Assert.IsNotNull(returnParameter, "Return parameter was null"); Assert.AreEqual(typeof(string), returnParameter.ParameterType, "Return parameter type should have been string"); Assert.AreEqual("GetAStringFromIntResult", returnParameter.Name, "Return parameter name match operation + Result"); Assert.AreEqual(TestNamespace, returnParameter.Namespace, "Return parameter namespace should match namespace"); Assert.AreEqual(0, returnParameter.Index, "Return parameter index should be zero"); }
public void HttpOperationDescription_OutputParameter_Matches_Method() { OperationDescription od = GetOperationDescription(typeof(MockService2), "OneInOneOutReturnsString"); HttpOperationDescription hod = od.ToHttpOperationDescription(); Assert.IsNotNull(hod, "Failed to create HttpOperationDescription"); IList <HttpParameterDescription> 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].ParameterType, "Expected out parameter of type out double"); Assert.AreEqual("parameter2", parameters[0].Name, "Expected parameter name match"); Assert.AreEqual(TestNamespace, parameters[0].Namespace, "Expected parameter2 name space match"); Assert.AreEqual(0, parameters[0].Index, "Parameter2 should have index 0"); }
public void HttpOperationDescription_InputParameter_Matches_Method() { OperationDescription od = GetOperationDescription(typeof(MockService2), "GetAStringFromInt"); HttpOperationDescription hod = od.ToHttpOperationDescription(); Assert.IsNotNull(hod, "Failed to create HttpOperationDescription"); IList <HttpParameterDescription> parameters = hod.InputParameters; Assert.IsNotNull(parameters, "InputParameters should not be null"); Assert.AreEqual(1, parameters.Count, "Expected only one parameter"); Assert.AreEqual(typeof(int), parameters[0].ParameterType, "Expected int parameter"); Assert.AreEqual("parameter1", parameters[0].Name, "Expected parameter name match"); Assert.AreEqual(0, parameters[0].Index, "Parameter index mismatch"); Assert.AreEqual(TestNamespace, parameters[0].Namespace, "Parameter namespace should match"); }
public static WebGetAttribute GetWebGetAttribute(this HttpOperationDescription operation) { if (operation == null) { throw new ArgumentNullException("operation"); } var webGet = operation.ToOperationDescription().Behaviors.Find <WebGetAttribute>(); if (webGet != null) { return(webGet); } return(null); }
/// <summary> /// Returns the ordered collection of <see cref="HttpOperationHandler"/> instances to use when handling /// <see cref="HttpRequestMessage"/> instances for the given <paramref name="operation"/>. /// </summary> /// <param name="endpoint">The service endpoint.</param> /// <param name="operation"> /// The <see cref="HttpOperationDescription"/> for the given operation that the <see cref="HttpOperationHandler"/> /// instances will be associated with.</param> /// <returns> /// The ordered collection of <see cref="HttpOperationHandler"/> instances to use when handling /// <see cref="HttpRequestMessage"/> instances for the given operation. /// </returns> public Collection<HttpOperationHandler> CreateRequestHandlers(ServiceEndpoint endpoint, HttpOperationDescription operation) { if (endpoint == null) { throw Fx.Exception.ArgumentNull("endpoint"); } if (operation == null) { throw Fx.Exception.ArgumentNull("operation"); } Collection<HttpOperationHandler> handlers = this.OnCreateRequestHandlers(endpoint, operation); return handlers ?? new Collection<HttpOperationHandler>(); }
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); } }
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); } } }
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); } }
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; }
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; }
protected override Collection<HttpOperationHandler> OnCreateResponseHandlers(ServiceEndpoint endpoint, HttpOperationDescription operation) { if (this.innerFactory != null) return this.innerFactory.CreateResponseHandlers(endpoint, operation); return base.OnCreateResponseHandlers(endpoint, operation); }
/// <summary> /// Called when the ordered collection of <see cref="HttpOperationHandler"/> instances is being created for /// the given <paramref name="operation"/>. Can be overridden in a derived class to customize the /// collection of <see cref="HttpOperationHandler"/> instances returned. /// </summary> /// <remarks> /// The base implemenation returns the standard request <see cref="HttpOperationHandler"/> instances for the given /// operation. /// </remarks> /// <param name="endpoint">The service endpoint.</param> /// <param name="operation">The description of the service operation.</param> /// <returns> /// The ordered collection of <see cref="HttpOperationHandler"/> instances to use when handling /// <see cref="HttpRequestMessage"/> instances for the given operation. /// </returns> protected virtual Collection<HttpOperationHandler> OnCreateRequestHandlers(ServiceEndpoint endpoint, HttpOperationDescription operation) { if (endpoint == null) { throw Fx.Exception.ArgumentNull("endpoint"); } if (operation == null) { throw Fx.Exception.ArgumentNull("operation"); } Collection<HttpOperationHandler> requestHandlers = new Collection<HttpOperationHandler>(); HttpMethod method = operation.GetHttpMethod(); UriTemplate uriTemplate = operation.GetUriTemplate(); string[] uriTemplateParameterNames = uriTemplate.PathSegmentVariableNames.Concat(uriTemplate.QueryValueVariableNames).ToArray(); if (uriTemplateParameterNames.Length > 0) { requestHandlers.Add(new UriTemplateHandler(endpoint.Address.Uri, uriTemplate)); } if (method != HttpMethod.Get && method != HttpMethod.Head) { HttpParameter requestContentParameter = GetRequestContentHandler(operation, uriTemplateParameterNames); if (requestContentParameter != null) { requestContentParameter.IsContentParameter = true; requestHandlers.Add(new RequestContentHandler(requestContentParameter, this.Formatters)); SetXmlAndJsonSerializers(operation, requestContentParameter, this.Formatters); } } return requestHandlers; }
private static void EnsureOneOneWebAttribute(WebGetAttribute webGet, WebInvokeAttribute webInvoke, HttpOperationDescription operation) { if (webGet != null && webInvoke != null) { throw Fx.Exception.AsError( new InvalidOperationException( SR.MultipleWebAttributes( operation.Name, operation.DeclaringContract.Name, webGetAttributeType.Name, webInvokeAttributeType.Name))); } }
/// <summary> /// Gets the <see cref="HttpMessageFormatter"/> to use for the given /// <paramref name="operations"/> for the specified <paramref name="endpoint"/>. /// </summary> /// <remarks> /// The base implementation returns an <see cref="HttpMessageFormatter"/> with the /// <see cref="HttpOperationHandler"/> insances applied to the given operation. /// </remarks> /// <param name="endpoint">The endpoint exposing the operations.</param> /// <param name="operation">The <see cref="HttpOperationDescription"/>.</param> /// <returns>The <see cref="HttpMessageFormatter"/> to use for the <paramref name="operation"/>.</returns> protected virtual HttpMessageFormatter OnGetMessageFormatter(ServiceEndpoint endpoint, HttpOperationDescription operation) { Fx.Assert(endpoint != null, "The 'endpoint' parameter should not be null."); Fx.Assert(operation != null, "The 'operation' parameter should not be null."); OperationHandlerPipeline pipeline = null; if (this.OperationHandlerFactory == null) { Collection<HttpOperationHandler> handlers = new Collection<HttpOperationHandler>(); pipeline = new OperationHandlerPipeline(handlers, handlers, operation); } else { Collection<HttpOperationHandler> requestHandlers = this.OperationHandlerFactory.CreateRequestHandlers(endpoint, operation); Collection<HttpOperationHandler> responseHandlers = this.OperationHandlerFactory.CreateResponseHandlers(endpoint, operation); pipeline = new OperationHandlerPipeline(requestHandlers, responseHandlers, operation); } OperationHandlerFormatter formatter = new OperationHandlerFormatter(pipeline); return formatter; }
protected override Collection<HttpOperationHandler> OnCreateRequestHandlers(ServiceEndpoint endpoint, HttpOperationDescription operation) { var handlers = this.innerFactory != null ? this.innerFactory.CreateRequestHandlers(endpoint, operation) : base.OnCreateRequestHandlers(endpoint, operation); handlers.Add(new UriMultiValueTemplateHandler(endpoint.Address.Uri, operation.GetUriTemplate())); return handlers; }
/// <summary> /// Called when the ordered collection of <see cref="HttpOperationHandler"/> instances is being created for /// the given <paramref name="operation"/>. Can be overridden in a derived class to customize the /// collection of <see cref="HttpOperationHandler"/> instances returned. /// </summary> /// <remarks> /// The base implemenation returns the standard response <see cref="HttpOperationHandler"/> instances for the given /// operation. /// </remarks> /// <param name="endpoint">The service endpoint.</param> /// <param name="operation"> /// The <see cref="HttpOperationDescription"/> for the given operation that the <see cref="HttpOperationHandler"/> /// instances will be associated with.</param> /// <returns> /// The ordered collection of <see cref="HttpOperationHandler"/> instances to use when handling /// <see cref="HttpResponseMessage"/> instances for the given operation. /// </returns> protected virtual Collection<HttpOperationHandler> OnCreateResponseHandlers(ServiceEndpoint endpoint, HttpOperationDescription operation) { if (endpoint == null) { throw Fx.Exception.ArgumentNull("endpoint"); } if (operation == null) { throw Fx.Exception.ArgumentNull("operation"); } Collection<HttpOperationHandler> responseHandlers = new Collection<HttpOperationHandler>(); List<HttpParameter> possibleContentParameters = new List<HttpParameter>(); HttpParameter returnValue = operation.ReturnValue; if (returnValue != null && IsPossibleResponseContentParameter(returnValue)) { possibleContentParameters.Add(returnValue); } foreach (HttpParameter parameter in operation.OutputParameters) { if (parameter != null && IsPossibleResponseContentParameter(parameter)) { possibleContentParameters.Add(returnValue); } } if (possibleContentParameters.Count > 1) { ThrowExceptionForMulitpleResponseContentParameters(possibleContentParameters, returnValue, operation.Name); } HttpParameter responseContentParameter = possibleContentParameters.Count == 1 ? possibleContentParameters[0] : returnValue; if (responseContentParameter != null && responseContentParameter.Type != TypeHelper.VoidType) { bool isReturnValue = responseContentParameter == returnValue; ValidateResponseContentParameter(responseContentParameter, isReturnValue, operation.Name); responseContentParameter.IsContentParameter = true; SetXmlAndJsonSerializers(operation, responseContentParameter, this.Formatters); } ResponseContentHandler responseContentHandler = new ResponseContentHandler(responseContentParameter, this.Formatters); responseHandlers.Add(responseContentHandler); return responseHandlers; }
public PngProcessor(HttpOperationDescription operation, MediaTypeProcessorMode mode) : base(operation, mode) { }