Beispiel #1
0
 void GetWebGetAndInvoke(OperationDescription od, out WebGetAttribute get, out WebInvokeAttribute invoke)
 {
     get = od.Behaviors.Find<WebGetAttribute>();
     invoke = od.Behaviors.Find<WebInvokeAttribute>();
     if (get == null && invoke == null)
     {
         // default is POST
         invoke = new WebInvokeAttribute();
     }
 }
		public void IOperationBehaviorMethods ()
		{
			IOperationBehavior oper = new WebInvokeAttribute ();
			var pl = new BindingParameterCollection ();
			var od = ContractDescription.GetContract (typeof (TestService)).Operations [0];
			oper.AddBindingParameters (od, pl);
			Assert.AreEqual (0, pl.Count, "#1");

			// yeah it really does nothing.
			oper.AddBindingParameters (null, null);

			oper.ApplyClientBehavior (od, null);
			oper.ApplyDispatchBehavior (od, null);
			oper.Validate (od);
		}
Beispiel #3
0
 WebMessageBodyStyle GetBodyStyle(WebGetAttribute get, WebInvokeAttribute invoke)
 {
     if (get != null)
     {
         return get.BodyStyle;
     }
     return invoke.BodyStyle;
 }
Beispiel #4
0
 Type GetRequestBodyType(OperationDescription od, out bool isXmlSerializerType)
 {
     isXmlSerializerType = (od.Behaviors.Find<XmlSerializerOperationBehavior>() != null);
     if (od.Behaviors.Find<WebGetAttribute>() != null)
     {
         return null;
     }
     WebInvokeAttribute invoke = od.Behaviors.Find<WebInvokeAttribute>();
     if (invoke == null) invoke = new WebInvokeAttribute();
     List<string> uriParameters = new List<string>();
     if (invoke.UriTemplate != null)
     {
         UriTemplate template = new UriTemplate(invoke.UriTemplate);
         foreach (string pathVariable in template.PathSegmentVariableNames)
         {
             uriParameters.Add(pathVariable);
         }
         foreach (string queryVariable in template.QueryValueVariableNames)
         {
             uriParameters.Add(queryVariable);
         }
     }
     if (od.Messages[0].MessageType != null) return null;
     List<Type> bodyParts = new List<Type>();
     foreach (MessagePartDescription messagePart in od.Messages[0].Body.Parts)
     {
         bool isUriPart = false;
         foreach (string var in uriParameters)
         {
             if (String.Equals(var, messagePart.Name, StringComparison.OrdinalIgnoreCase))
             {
                 isUriPart = true;
                 break;
             }
         }
         if (isUriPart)
         {
             continue;
         }
         bodyParts.Add(messagePart.Type);
     }
     if ((bodyParts.Count == 0) || (bodyParts.Count > 1))
     {
         return null;
     }
     return bodyParts[0];
 }
Beispiel #5
0
        private string GetMethod(WebGetAttribute get, WebInvokeAttribute invoke)
        {
            if (get != null) return "GET";
            if (invoke != null && !string.IsNullOrEmpty(invoke.Method)) return invoke.Method;
            return "POST";

        }
Beispiel #6
0
 private string GetRequestFormat(WebInvokeAttribute invoke, OperationDescription od)
 {
     if (IsRequestStream(od))
     {
         return "binary";
     }
     return "xml or json";
 }
Beispiel #7
0
 private string GetResponseFormat(WebGetAttribute get, WebInvokeAttribute invoke, OperationDescription od)
 {
     if (IsResponseStream(od))
     {
         return "binary";
     }
     if (get != null && get.IsResponseFormatSetExplicitly) return get.ResponseFormat.ToString();
     if (invoke != null && invoke.IsResponseFormatSetExplicitly) return invoke.ResponseFormat.ToString();
     return this.Behavior.DefaultOutgoingResponseFormat.ToString();
 }
        private static PreflightOperationBehavior RegisterPreflightOperation(OperationDescription operation)
        {
            OperationDescription preflightOperation = CreatePreflightOperation(operation);

            preflightOperation.Behaviors.Add(new DataContractSerializerOperationBehavior(preflightOperation));

            string originalUriTemplate = GetPreflightUriTemplate(operation);
            var wia = new WebInvokeAttribute { UriTemplate = originalUriTemplate, Method = "OPTIONS" };
            preflightOperation.Behaviors.Add(wia);

            string originalMethod = GetHttpMethod(operation);
            var preflightOperationBehavior = new PreflightOperationBehavior();
            preflightOperationBehavior.AddAllowedMethod(originalMethod);
            preflightOperation.Behaviors.Add(preflightOperationBehavior);

            operation.DeclaringContract.Operations.Add(preflightOperation);

            return preflightOperationBehavior;
        }
        private static void CreatePreflightOperation(OperationDescription operation, string originalUriTemplate, string originalMethod, ContractDescription contract, out OperationDescription preflightOperation, out PreflightOperationBehavior preflightOperationBehavior)
        {
            preflightOperation = new OperationDescription(operation.Name + CorsConstants.PreflightSuffix, contract);
            MessageDescription inputMessage = new MessageDescription(operation.Messages[0].Action + CorsConstants.PreflightSuffix, MessageDirection.Input);
            inputMessage.Body.Parts.Add(new MessagePartDescription("input", contract.Namespace) { Index = 0, Type = typeof(Message) });
            preflightOperation.Messages.Add(inputMessage);
            MessageDescription outputMessage = new MessageDescription(operation.Messages[1].Action + CorsConstants.PreflightSuffix, MessageDirection.Output);
            outputMessage.Body.ReturnValue = new MessagePartDescription(preflightOperation.Name + "Return", contract.Namespace) { Type = typeof(Message) };
            preflightOperation.Messages.Add(outputMessage);

            WebInvokeAttribute wia = new WebInvokeAttribute();
            wia.UriTemplate = originalUriTemplate;
            wia.Method = "OPTIONS";

            preflightOperation.Behaviors.Add(wia);
            preflightOperation.Behaviors.Add(new DataContractSerializerOperationBehavior(preflightOperation));
            preflightOperationBehavior = new PreflightOperationBehavior(preflightOperation);
            preflightOperationBehavior.AddAllowedMethod(originalMethod);
            preflightOperation.Behaviors.Add(preflightOperationBehavior);
        }
Beispiel #10
0
 static void EnsureOk(WebGetAttribute wga, WebInvokeAttribute wia, OperationDescription od)
 {
     if (wga != null && wia != null)
     {
         throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(
             SR2.GetString(SR2.MultipleWebAttributes, od.Name, od.DeclaringContract.Name)));
     }
 }
 private static void TryGetSurrogateBehavior(OperationDescription operationDescription, ref IOperationBehavior original, ref IOperationBehavior surrogate)
 {
     if (!IsUntypedMessage(operationDescription.Messages[0]) &&
         operationDescription.Messages[0].Body.Parts.Count != 0)
     {
         var webGetAttribute = operationDescription.Behaviors.Find<WebGetAttribute>();
         if (webGetAttribute != null)
         {
             original = webGetAttribute;
             surrogate = new WebInvokeAttribute {
                  BodyStyle = webGetAttribute.BodyStyle,
                  Method = "NONE",
                  RequestFormat = webGetAttribute.RequestFormat,
                  ResponseFormat = webGetAttribute.ResponseFormat,
                  UriTemplate = webGetAttribute.UriTemplate };
         }
         else
         {
             var webInvokeAttribute = operationDescription.Behaviors.Find<WebInvokeAttribute>();
             if (webInvokeAttribute != null && webInvokeAttribute.Method == "GET")
             {
                 original = webInvokeAttribute;
                 surrogate = new WebInvokeAttribute {
                     BodyStyle = webInvokeAttribute.BodyStyle,
                     Method = "NONE",
                     RequestFormat = webInvokeAttribute.RequestFormat,
                     ResponseFormat = webInvokeAttribute.ResponseFormat,
                     UriTemplate = webInvokeAttribute.UriTemplate };
             }
         }
     }
 }
 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)));
     }
 }
        private Endpoint CreateEndpoint(MethodInfo method, WebInvokeAttribute webInvokeAttribute)
        {
            var request = CreateRequest(method);

            var endpoint = new Endpoint()
            {
                Name = method.Name,
                Requests = request,
                HttpVerb = (HttpVerb)Enum.Parse(typeof(HttpVerb), webInvokeAttribute.Method),
                Response = method.ReturnType,
                RequestFormat = (Format)Enum.Parse(typeof(Format), webInvokeAttribute.RequestFormat.ToString()),
                ResponseFormat = (Format)Enum.Parse(typeof(Format), webInvokeAttribute.ResponseFormat.ToString()),
                UriTemplate = webInvokeAttribute.UriTemplate
            };
            return endpoint;
        }
Beispiel #14
0
        /// <summary>
        /// Serialize a .NET method definition in json.
        /// </summary>
        /// <param name="writer"></param>
        /// <param name="method"></param>
        /// <param name="getAttr"></param>
        /// <param name="invokAttr"></param>
        private void serializeMethod(JsonWriter writer, MethodInfo method, WebGetAttribute getAttr, WebInvokeAttribute invokAttr)
        {
            writer.WritePropertyName(method.Name);
            writer.WriteStartObject();

            writer.WritePropertyName("transport");
            writer.WriteValue(getAttr != null ? "GET" : invokAttr.Method);

            writer.WritePropertyName("target");
            writer.WriteValue(method.Name);

            writer.WritePropertyName("envelope");
            writer.WriteValue(getAttr != null ? "URL" : "JSON");

            writer.WritePropertyName("parameters");

            writer.WriteStartArray();

            ParameterInfo[] prams = method.GetParameters();
            foreach (ParameterInfo prm in method.GetParameters())
            {
                writer.WriteStartObject();

                writer.WritePropertyName("name");
                writer.WriteValue(prm.Name);

                writer.WritePropertyName("type");
                writer.WriteValue(prm.ParameterType.ToString());

                writer.WriteEndObject();
            }

            writer.WriteEndArray();
            writer.WriteEndObject();
        }
Beispiel #15
0
 private static void CheckWebInvokeAndWebGet(WebInvokeAttribute wia, WebGetAttribute wga, OperationDescription operationDescription)
 {
     if (wga != null && wia != null)
     {
         throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(DiagnosticUtility.GetString(SR.OperationContractConflictsWebGetOrWebInvoke, operationDescription.Name)));
     }
 }
        /// <summary>
        /// Adds the preflight operations.
        /// </summary>
        /// <param name="endpoint">The endpoint.</param>
        /// <param name="corsOperations">The cors operations.</param>
        private void AddPreflightOperations(ServiceEndpoint endpoint, List<OperationDescription> corsOperations)
        {
            Dictionary<string, PreflightOperationBehavior> uriTemplates = new Dictionary<string, PreflightOperationBehavior>(StringComparer.OrdinalIgnoreCase);

            foreach (var operation in corsOperations)
            {
                if ((operation.Behaviors.Find<WebGetAttribute>() != null) || (operation.IsOneWay))
                {
                    continue;
                }

                string originalUriTemplate;
                WebInvokeAttribute originalWia = operation.Behaviors.Find<WebInvokeAttribute>();

                if (originalWia != null && originalWia.UriTemplate != null)
                {
                    originalUriTemplate = NormalizeTemplate(originalWia.UriTemplate);
                }
                else
                {
                    originalUriTemplate = operation.Name;
                }

                string originalMethod = originalWia != null && originalWia.Method != null ? originalWia.Method : "POST";

                if (uriTemplates.ContainsKey(originalUriTemplate))
                {
                    PreflightOperationBehavior operationBehavior = uriTemplates[originalUriTemplate];
                    operationBehavior.AddAllowedMethod(originalMethod);
                }
                else
                {
                    ContractDescription contract = operation.DeclaringContract;
                    OperationDescription preflightOperation = new OperationDescription(operation.Name + CorsConstants.PreflightSuffix, contract);
                    MessageDescription inputMessage = new MessageDescription(operation.Messages[0].Action + CorsConstants.PreflightSuffix, MessageDirection.Input);
                    inputMessage.Body.Parts.Add(new MessagePartDescription("input", contract.Namespace) { Index = 0, Type = typeof(Message) });
                    preflightOperation.Messages.Add(inputMessage);
                    MessageDescription outputMessage = new MessageDescription(operation.Messages[1].Action + CorsConstants.PreflightSuffix, MessageDirection.Output);
                    outputMessage.Body.ReturnValue = new MessagePartDescription(preflightOperation.Name + "Return", contract.Namespace) { Type = typeof(Message) };
                    preflightOperation.Messages.Add(outputMessage);

                    WebInvokeAttribute wia = new WebInvokeAttribute();
                    wia.UriTemplate = originalUriTemplate;
                    wia.Method = "OPTIONS";

                    preflightOperation.Behaviors.Add(wia);
                    preflightOperation.Behaviors.Add(new DataContractSerializerOperationBehavior(preflightOperation));
                    PreflightOperationBehavior preflightOperationBehavior = new PreflightOperationBehavior(preflightOperation);
                    preflightOperationBehavior.AddAllowedMethod(originalMethod);
                    preflightOperationBehavior.AddAllowedMethod("DELETE");
                    preflightOperation.Behaviors.Add(preflightOperationBehavior);
                    uriTemplates.Add(originalUriTemplate, preflightOperationBehavior);

                    contract.Operations.Add(preflightOperation);
                }
            }
        }
 private static void EnsureOk(WebGetAttribute wga, WebInvokeAttribute wia, OperationDescription od)
 {
     if (wga != null && wia != null)
     {
         throw new InvalidOperationException();
     }
 }
Beispiel #18
0
 private string GetUriTemplate(WebGetAttribute get, WebInvokeAttribute invoke, OperationDescription od)
 {
     if (get != null)
     {
         if (get.UriTemplate != null)
         {
             return get.UriTemplate;
         }
         else
         {
             StringBuilder sb = new StringBuilder(od.Name);
             if (!IsUntypedMessage(od.Messages[0]))
             {
                 sb.Append("?");
                 foreach (MessagePartDescription mpd in od.Messages[0].Body.Parts)
                 {
                     string parameterName = mpd.Name;
                     sb.Append(parameterName);
                     sb.Append("={");
                     sb.Append(parameterName);
                     sb.Append("}&");
                 }
                 sb.Remove(sb.Length - 1, 1);
             }
             return sb.ToString();
         }
     }
     if (invoke.UriTemplate != null)
     {
         return invoke.UriTemplate;
     }
     else
     {
         return od.Name;
     }
 }