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); } }
// 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); } } }
public static OperationDescription GetEquivalentOperationDescription(OperationDescription operation) { Fx.Assert(operation != null, "OperationDescription cannnot be null"); OperationDescription copy = CreateEmptyOperationDescription(operation); HttpOperationDescription httpDescription = copy.ToHttpOperationDescription(); HttpOperationDescription originalHttpDescription = operation.ToHttpOperationDescription(); UriTemplate template = originalHttpDescription.GetUriTemplate(); List <string> templateVariables = new List <string>(template.PathSegmentVariableNames.Concat(template.QueryValueVariableNames)); IEnumerable <HttpParameter> originalRequestBodyParameters = originalHttpDescription.InputParameters.Where(param => param.IsContentParameter); IEnumerable <HttpParameter> originalResponseBodyParameters = originalHttpDescription.OutputParameters.Where(param => param.IsContentParameter); IEnumerable <HttpParameter> originalUriTemplateParameters = originalHttpDescription.InputParameters.Where( (param) => { foreach (string templateVariable in templateVariables) { if (string.Equals(templateVariable, param.Name, StringComparison.OrdinalIgnoreCase)) { return(true); } } return(false); }); httpDescription.ReturnValue = originalHttpDescription.ReturnValue; // add UriTemplate parameters foreach (HttpParameter parameter in originalUriTemplateParameters) { httpDescription.InputParameters.Add(parameter); } // add body parameters foreach (HttpParameter parameter in originalRequestBodyParameters) { httpDescription.InputParameters.Add(parameter); } int index = httpDescription.InputParameters.Count; int requestBodyParametersCount = originalRequestBodyParameters.Count <HttpParameter>(); int responseBodyParametersCount = originalResponseBodyParameters.Count <HttpParameter>(); if (requestBodyParametersCount == 0 || responseBodyParametersCount == 0) { // Special case if any input parameter is HttpRequestMessage or HttpResponseMessage foreach (HttpParameter inputParameter in originalHttpDescription.InputParameters) { if (requestBodyParametersCount == 0 && originalHttpDescription.GetHttpMethod() != HttpMethod.Get && httpRequestMessageType.IsAssignableFrom(inputParameter.Type)) { // add the HttpRequestmessage as a body parameter of type Message HttpParameter parameter = new HttpParameter(new MessagePartDescription(inputParameter.Name, string.Empty) { Type = typeof(Message), Index = index++ }); httpDescription.InputParameters.Add(parameter); } if (!operation.IsOneWay && responseBodyParametersCount == 0 && httpResponseMessageType.IsAssignableFrom(inputParameter.Type)) { // add the HttpResponsemessage as a return value of type Message httpDescription.ReturnValue = new HttpParameter(new MessagePartDescription(inputParameter.Name, string.Empty) { Type = typeof(Message) }); } } } foreach (HttpParameter parameter in originalResponseBodyParameters) { // cannot do a byRef comparison here if (parameter.Type != originalHttpDescription.ReturnValue.Type || !string.Equals(parameter.Name, originalHttpDescription.ReturnValue.Name, StringComparison.OrdinalIgnoreCase)) { httpDescription.OutputParameters.Add(parameter); } } if (responseBodyParametersCount == 0) { foreach (HttpParameter outputParameter in originalHttpDescription.OutputParameters) { // special case HttpResponseMessage when it is set as an out parameter if (httpResponseMessageType.IsAssignableFrom(outputParameter.Type)) { httpDescription.ReturnValue = new HttpParameter(new MessagePartDescription(outputParameter.Name, string.Empty) { Type = typeof(Message) }); } } } if (templateVariables.Count > originalUriTemplateParameters.Count <HttpParameter>()) { // this means that we have some UriTemplate variables that are not explicitly bound to an input parameter foreach (HttpParameter parameter in originalUriTemplateParameters) { templateVariables.Remove(parameter.Name); } foreach (string variable in templateVariables) { HttpParameter parameter = new HttpParameter(new MessagePartDescription(variable, operation.DeclaringContract.Namespace) { Type = typeof(string), Index = index++ }); httpDescription.InputParameters.Add(parameter); } } return(httpDescription.ToOperationDescription()); }