static void ValidateReceiveParametersWithReceiveParameters(ReceiveParametersContent receiveParameters1, ReceiveParametersContent receiveParameters2, string receiveOperationName)
        {
            Fx.Assert(receiveParameters1 != null && receiveParameters2 != null, "Validation argument cannot be null!");

            int count = receiveParameters1.ArgumentNames.Length;
            if (count != receiveParameters2.ArgumentNames.Length)
            {
                throw FxTrace.Exception.AsError(new ValidationException(SR2.TwoReceiveParametersWithSameNameButDifferentParameterCount(receiveOperationName)));
            }
            for (int i = 0; i < count; i++)
            {
                if (receiveParameters1.ArgumentNames[i] != receiveParameters2.ArgumentNames[i])
                {
                    throw FxTrace.Exception.AsError(new ValidationException(SR2.TwoReceiveParametersWithSameNameButDifferentParameterName(receiveOperationName)));
                }
                if (receiveParameters1.ArgumentTypes[i] != receiveParameters2.ArgumentTypes[i])
                {
                    throw FxTrace.Exception.AsError(new ValidationException(SR2.TwoReceiveParametersWithSameNameButDifferentParameterType(receiveOperationName)));
                }
            }
        }
Esempio n. 2
0
        public static Receive FromOperationDescription(OperationDescription operation)
        {
            if (operation == null)
            {
                throw FxTrace.Exception.ArgumentNull("operation", "OperationDescription should not be null");
            }

            MessageDescription message;
            Receive receiveActivity = new Receive();
            receiveActivity.ServiceContractName = XName.Get(operation.DeclaringContract.Name, operation.DeclaringContract.Namespace);
            receiveActivity.OperationName = operation.Name;
            receiveActivity.DisplayName = operation.Name + "Receive";
            receiveActivity.ProtectionLevel = operation.ProtectionLevel;

            if (operation.Messages != null && operation.Messages.Count > 0)
            {
                receiveActivity.Action = operation.Messages[0].Action;
            }

            Collection<Type> knownTypes = operation.KnownTypes;
            if (knownTypes != null)
            {
                foreach (Type knownType in knownTypes)
                {
                    receiveActivity.KnownTypes.Add(knownType);
                }
            }

            // Set SerializerOption
            if (operation.Behaviors.Contains(typeof(XmlSerializerOperationBehavior)))
            {
                receiveActivity.SerializerOption = SerializerOption.XmlSerializer;
            }
            else
            {
                receiveActivity.SerializerOption = SerializerOption.DataContractSerializer;
            }

            bool contentIsParameter = false;
            bool noReceiveMessageContent = false;

            message = operation.Messages[0];

            // MessageType is null indicating it is not typed message contract
            if (message.MessageType == null)
            {
                if (message.Body.Parts != null)
                {
                    if (message.Body.Parts.Count != 0)
                    {
                        foreach (MessagePartDescription messagePart in message.Body.Parts)
                        {
                            if (messagePart.Index > 0)
                            {
                                contentIsParameter = true;
                                break;
                            }
                            // Indicating it is a untyped message contract
                            if (!messagePart.Type.IsAssignableFrom(typeof(System.ServiceModel.Channels.Message)))
                            {
                                contentIsParameter = true;
                            }
                        }
                    }
                    else
                    {
                        noReceiveMessageContent = true;
                    }
                }
                else
                {
                    noReceiveMessageContent = true;
                }
            }

            if (!noReceiveMessageContent)
            {
                if (contentIsParameter)
                {
                    ReceiveParametersContent content = new ReceiveParametersContent();
                    if (message.Direction == MessageDirection.Input && message.Body.Parts != null)
                    {
                        foreach (MessagePartDescription messagePart in message.Body.Parts)
                        {
                            Argument outArgument = OutArgument.Create(messagePart.Type, ArgumentDirection.Out);
                            content.Parameters.Add(messagePart.Name, (OutArgument)(outArgument));
                        }
                    }

                    receiveActivity.Content = content;
                }
                else
                {
                    ReceiveMessageContent content = new ReceiveMessageContent();
                    if (message.Direction == MessageDirection.Input)
                    {
                        if (message.MessageType != null)
                        {
                            content.DeclaredMessageType = message.MessageType;
                        }
                        else
                        {
                            content.DeclaredMessageType = message.Body.Parts[0].Type;
                        }

                        Argument outArgument = OutArgument.Create(content.DeclaredMessageType, ArgumentDirection.Out);
                        content.Message = (OutArgument)outArgument;
                    }

                    receiveActivity.Content = content;
                }
            }
            else
            {
                if ((message.Body.ReturnValue != null && message.Body.ReturnValue.Type.IsDefined(typeof(MessageContractAttribute), false))
                    || (message.Body.ReturnValue != null && message.Body.ReturnValue.Type.IsAssignableFrom(typeof(System.ServiceModel.Channels.Message))))
                {
                    receiveActivity.Content = new ReceiveMessageContent();
                }
                else if (operation.Messages.Count > 1)
                {
                    if (operation.Messages[1].MessageType != null || operation.Messages[1].Body.ReturnValue.Type.IsAssignableFrom(typeof(System.ServiceModel.Channels.Message)))
                    {
                        receiveActivity.Content = new ReceiveMessageContent();
                    }
                    else
                    {
                        receiveActivity.Content = new ReceiveParametersContent();
                    }
                }
                else
                {
                    receiveActivity.Content = new ReceiveParametersContent();
                }
            }

            return receiveActivity;
        }