Exemple #1
0
        static SendReply BuildFaultReplies(FaultDescription faultDescription, bool isMessageContract)
        {
            Fx.Assert(faultDescription != null, "fault Description cannot be null");
            if (faultDescription.DetailType == TypeHelper.VoidType || faultDescription.DetailType == null)
            {
                throw FxTrace.Exception.ArgumentNullOrEmpty("FaultDescription.DetailType");
            }

            SendReply faultReply = new SendReply()
            {
                DisplayName = faultDescription.Name + "SendFaultReply",
                Action      = faultDescription.Action,
            };

            Type[] substitute = { faultDescription.DetailType };
            Type   faultType  = typeof(FaultException <>).MakeGenericType(substitute);

            if (isMessageContract)
            {
                faultReply.Content = new SendMessageContent()
                {
                    Message = (InArgument)(InArgument.Create(faultType, ArgumentDirection.In)),
                };
            }
            else
            {
                InArgument            argument = (InArgument)(InArgument.Create(faultType, ArgumentDirection.In));
                SendParametersContent faultReplyParameterContent = new SendParametersContent();
                faultReplyParameterContent.Parameters.Add(faultDescription.Name, argument);
                faultReply.Content = faultReplyParameterContent;
            }

            return(faultReply);
        }
Exemple #2
0
        public static void AddFaultDescription(Receive activity, OperationDescription operation)
        {
            if (activity.HasFault)
            {
                foreach (SendReply sendFault in activity.FollowingFaults)
                {
                    string action = null;
                    Type   type   = null;

                    action = sendFault.Action;

                    SendMessageContent sendReply = sendFault.InternalContent as SendMessageContent;
                    if (sendReply != null)
                    {
                        type = sendReply.InternalDeclaredMessageType;
                    }
                    else
                    {
                        SendParametersContent sendReplyParameters = sendFault.InternalContent as SendParametersContent;
                        if (sendReplyParameters != null)
                        {
                            type = sendReplyParameters.ArgumentTypes[0];  // Exception should be the only parameter in SendFault
                        }
                    }

                    Fx.Assert(type != null, "Exception type cannot be null!");
                    if (type.IsGenericType && type.GetGenericTypeDefinition() == FaultExceptionType)
                    {
                        Type faultType = type.GetGenericArguments()[0];
                        bool exists    = false;

                        // We expect the number of fault types to be small, so we use iterative comparison
                        foreach (FaultDescription faultDescription in operation.Faults)
                        {
                            if (faultDescription.DetailType == faultType)
                            {
                                if (faultDescription.Action != action)
                                {
                                    throw FxTrace.Exception.AsError(new ValidationException(SR.SendRepliesHaveSameFaultTypeDifferentAction));
                                }
                                else
                                {
                                    exists = true;
                                    break;
                                }
                            }
                        }

                        if (!exists)
                        {
                            FaultDescription faultDescription = MessageBuilder.CreateFaultDescription(operation, faultType, action);
                            operation.Faults.Add(faultDescription);
                        }
                    }
                }
            }
        }
        public static void ValidateSendReplyWithSendReply(SendReply sendReply1, SendReply sendReply2)
        {
            Fx.Assert(sendReply1 != null && sendReply2 != null, "Validation argument cannot be null!");
            Fx.Assert(sendReply1.Request != null, "Request cannot be null in SendReply");
            string operationName = sendReply1.Request.OperationName;

            if (sendReply1.Action != sendReply2.Action)
            {
                throw FxTrace.Exception.AsError(new ValidationException(SR2.TwoSendRepliesWithSameNameButDifferentAction(operationName)));
            }

            if (sendReply1.InternalContent is SendMessageContent && sendReply2.InternalContent is SendMessageContent)
            {
                SendMessageContent sendMessage1 = sendReply1.InternalContent as SendMessageContent;
                SendMessageContent sendMessage2 = sendReply2.InternalContent as SendMessageContent;

                if (sendMessage1.InternalDeclaredMessageType != sendMessage2.InternalDeclaredMessageType)
                {
                    throw FxTrace.Exception.AsError(new ValidationException(SR2.TwoSendRepliesWithSameNameButDifferentValueType(operationName)));
                }
            }
            else if (sendReply1.InternalContent is SendParametersContent && sendReply2.InternalContent is SendParametersContent)
            {
                SendParametersContent sendReplyParameters1 = sendReply1.InternalContent as SendParametersContent;
                SendParametersContent sendReplyParameters2 = sendReply2.InternalContent as SendParametersContent;

                int count = sendReplyParameters1.ArgumentNames.Length;
                if (count != sendReplyParameters2.ArgumentNames.Length)
                {
                    throw FxTrace.Exception.AsError(new ValidationException(SR2.TwoSendReplyParametersWithSameNameButDifferentParameterCount(operationName)));
                }
                for (int i = 0; i < count; i++)
                {
                    if (sendReplyParameters1.ArgumentNames[i] != sendReplyParameters2.ArgumentNames[i])
                    {
                        throw FxTrace.Exception.AsError(new ValidationException(SR2.TwoSendReplyParametersWithSameNameButDifferentParameterName(operationName)));
                    }
                    if (sendReplyParameters1.ArgumentTypes[i] != sendReplyParameters2.ArgumentTypes[i])
                    {
                        throw FxTrace.Exception.AsError(new ValidationException(SR2.TwoSendReplyParametersWithSameNameButDifferentParameterType(operationName)));
                    }
                }
            }
            else
            {
                throw FxTrace.Exception.AsError(new ValidationException(SR2.ReceivePairedWithSendReplyAndSendReplyParameters(operationName)));
            }
        }
 public static void AddFaultDescription(Receive activity, OperationDescription operation)
 {
     if (activity.HasFault)
     {
         foreach (SendReply reply in activity.FollowingFaults)
         {
             string      overridingAction            = null;
             System.Type internalDeclaredMessageType = null;
             overridingAction = reply.Action;
             SendMessageContent internalContent = reply.InternalContent as SendMessageContent;
             if (internalContent != null)
             {
                 internalDeclaredMessageType = internalContent.InternalDeclaredMessageType;
             }
             else
             {
                 SendParametersContent content2 = reply.InternalContent as SendParametersContent;
                 if (content2 != null)
                 {
                     internalDeclaredMessageType = content2.ArgumentTypes[0];
                 }
             }
             if (internalDeclaredMessageType.IsGenericType && (internalDeclaredMessageType.GetGenericTypeDefinition() == FaultExceptionType))
             {
                 System.Type faultType = internalDeclaredMessageType.GetGenericArguments()[0];
                 bool        flag      = false;
                 foreach (FaultDescription description in operation.Faults)
                 {
                     if (description.DetailType == faultType)
                     {
                         if (description.Action != overridingAction)
                         {
                             throw System.ServiceModel.Activities.FxTrace.Exception.AsError(new ValidationException(System.ServiceModel.Activities.SR.SendRepliesHaveSameFaultTypeDifferentAction));
                         }
                         flag = true;
                         break;
                     }
                 }
                 if (!flag)
                 {
                     FaultDescription item = MessageBuilder.CreateFaultDescription(operation, faultType, overridingAction);
                     operation.Faults.Add(item);
                 }
             }
         }
     }
 }
Exemple #5
0
        public static void ValidateSendReplyWithSendReply(SendReply sendReply1, SendReply sendReply2)
        {
            string operationName = sendReply1.Request.OperationName;

            if (sendReply1.Action != sendReply2.Action)
            {
                throw System.ServiceModel.Activities.FxTrace.Exception.AsError(new ValidationException(System.ServiceModel.Activities.SR.TwoSendRepliesWithSameNameButDifferentAction(operationName)));
            }
            if ((sendReply1.InternalContent is SendMessageContent) && (sendReply2.InternalContent is SendMessageContent))
            {
                SendMessageContent internalContent = sendReply1.InternalContent as SendMessageContent;
                SendMessageContent content2        = sendReply2.InternalContent as SendMessageContent;
                if (internalContent.InternalDeclaredMessageType != content2.InternalDeclaredMessageType)
                {
                    throw System.ServiceModel.Activities.FxTrace.Exception.AsError(new ValidationException(System.ServiceModel.Activities.SR.TwoSendRepliesWithSameNameButDifferentValueType(operationName)));
                }
            }
            else
            {
                if (!(sendReply1.InternalContent is SendParametersContent) || !(sendReply2.InternalContent is SendParametersContent))
                {
                    throw System.ServiceModel.Activities.FxTrace.Exception.AsError(new ValidationException(System.ServiceModel.Activities.SR.ReceivePairedWithSendReplyAndSendReplyParameters(operationName)));
                }
                SendParametersContent content3 = sendReply1.InternalContent as SendParametersContent;
                SendParametersContent content4 = sendReply2.InternalContent as SendParametersContent;
                int length = content3.ArgumentNames.Length;
                if (length != content4.ArgumentNames.Length)
                {
                    throw System.ServiceModel.Activities.FxTrace.Exception.AsError(new ValidationException(System.ServiceModel.Activities.SR.TwoSendReplyParametersWithSameNameButDifferentParameterCount(operationName)));
                }
                for (int i = 0; i < length; i++)
                {
                    if (content3.ArgumentNames[i] != content4.ArgumentNames[i])
                    {
                        throw System.ServiceModel.Activities.FxTrace.Exception.AsError(new ValidationException(System.ServiceModel.Activities.SR.TwoSendReplyParametersWithSameNameButDifferentParameterName(operationName)));
                    }
                    if (content3.ArgumentTypes[i] != content4.ArgumentTypes[i])
                    {
                        throw System.ServiceModel.Activities.FxTrace.Exception.AsError(new ValidationException(System.ServiceModel.Activities.SR.TwoSendReplyParametersWithSameNameButDifferentParameterType(operationName)));
                    }
                }
            }
        }
        static SendReply BuildFaultReplies(FaultDescription faultDescription, bool isMessageContract)
        {
            Fx.Assert(faultDescription != null, "fault Description cannot be null");
            if (faultDescription.DetailType == TypeHelper.VoidType || faultDescription.DetailType == null)
            {
                throw FxTrace.Exception.ArgumentNullOrEmpty("FaultDescription.DetailType");
            }

            SendReply faultReply = new SendReply()
            {
                DisplayName = faultDescription.Name + "SendFaultReply",
                Action = faultDescription.Action,
            };

            Type[] substitute = { faultDescription.DetailType };
            Type faultType = typeof(FaultException<>).MakeGenericType(substitute);
            if (isMessageContract)
            {
                faultReply.Content = new SendMessageContent()
                {
                    Message = (InArgument)(InArgument.Create(faultType, ArgumentDirection.In)),
                };
            }
            else
            {
                InArgument argument = (InArgument)(InArgument.Create(faultType, ArgumentDirection.In));
                SendParametersContent faultReplyParameterContent = new SendParametersContent();
                faultReplyParameterContent.Parameters.Add(faultDescription.Name, argument);
                faultReply.Content = faultReplyParameterContent;
            }

            return faultReply;
        }
        public static SendReply FromOperationDescription(OperationDescription operation, out IEnumerable<SendReply> faultReplies)
        {
            if (operation == null)
            {
                throw FxTrace.Exception.ArgumentNull("operation", "OperationDescription cannot be null");
            }

            bool contentIsParameter = false;
            bool contentIsMessage = false; 
            bool isSendContentEmpty = false;
            
            MessageDescription message;

            faultReplies = null; 
            List<SendReply> faultRepliesList = new List<SendReply>();
            SendReply reply = null;
            
            if (operation.IsOneWay)
            {
                return null;
            }

            if (operation.Messages.Count > 1)
            {
                reply = new SendReply();
                reply.Action = operation.Messages[1].Action;
                reply.DisplayName = operation.Name + "SendReply";

                message = operation.Messages[1];

                contentIsParameter = false;

                if (message.MessageType == null)
                {
                    if (message.Body.ReturnValue != null && message.Body.ReturnValue.Type != typeof(void))
                    {
                        if (!message.Body.ReturnValue.Type.IsAssignableFrom(typeof(System.ServiceModel.Channels.Message)))
                        {
                            contentIsParameter = true;
                        }

                        isSendContentEmpty = true;
                    }
                }

                if (message.MessageType == null)
                {
                    if (message.Body.Parts != null)
                    {
                        if (message.Body.Parts.Count > 0)
                        {
                            MessagePartDescriptionCollection parts = message.Body.Parts;
                            foreach (MessagePartDescription messagePart in parts)
                            {
                                if (messagePart.Index >= 0)
                                {
                                    contentIsParameter = true;
                                    break;
                                }
                                if (!messagePart.Type.IsAssignableFrom(typeof(System.ServiceModel.Channels.Message)))
                                {
                                    contentIsParameter = true;
                                }
                            }
                            isSendContentEmpty = true;
                        }
                    }
                }

                if (isSendContentEmpty)
                {
                    if (contentIsParameter)
                    {
                        SendParametersContent content = new SendParametersContent();
                        if (message.Direction == MessageDirection.Output
                            && message.Body.ReturnValue != null
                            && message.Body.ReturnValue.Type != typeof(void))
                        {
                            Argument returnArgument = InArgument.Create(message.Body.ReturnValue.Type, ArgumentDirection.In);
                            content.Parameters.Add(message.Body.ReturnValue.Name, (InArgument)returnArgument);
                        }

                        if (message.Direction == MessageDirection.Output && message.Body.Parts != null)
                        {
                            foreach (MessagePartDescription messagePart in message.Body.Parts)
                            {
                                Argument inArgument = InArgument.Create(messagePart.Type, ArgumentDirection.In);
                                content.Parameters.Add(messagePart.Name, (InArgument)(inArgument));
                            }
                        }
                        contentIsMessage = false;
                        reply.Content = content;
                    }
                    else
                    {
                        // We must have an untyped message contract
                        // 
                        SendMessageContent content = new SendMessageContent();
                        if (message.Direction == MessageDirection.Output)
                        {
                            content.DeclaredMessageType = message.Body.ReturnValue.Type;
                            Argument inArgument = InArgument.Create(content.DeclaredMessageType, ArgumentDirection.In);
                            content.Message = (InArgument)(inArgument);
                        }
                        contentIsMessage = true; 
                        reply.Content = content;
                    }
                }
                else
                {
                    if (message.MessageType != null && message.MessageType.IsDefined(typeof(MessageContractAttribute), false))
                    {
                        SendMessageContent sendMessageContent;
                        sendMessageContent = new SendMessageContent();
                        sendMessageContent.DeclaredMessageType = message.MessageType;
                        Argument inArgument = InArgument.Create(sendMessageContent.DeclaredMessageType, ArgumentDirection.In);
                        sendMessageContent.Message = (InArgument)(inArgument);
                        reply.Content = sendMessageContent;
                        contentIsMessage = true; 
                    }
                    else if (operation.Messages[0].MessageType != null)
                    {
                        reply.Content = new SendMessageContent();
                        contentIsMessage = true; 
                    }
                    else if (operation.Messages[0].Body.Parts != null
                        && operation.Messages[0].Body.Parts.Count == 1
                        && operation.Messages[0].Body.Parts[0].Type.IsAssignableFrom(typeof(System.ServiceModel.Channels.Message)))
                    {
                        reply.Content = new SendMessageContent();
                        contentIsMessage = true; 
                    }
                    else
                    {
                        reply.Content = new SendParametersContent();
                        contentIsMessage = false; 
                    }
                }
            }

            if (operation.Faults != null)
            {
                foreach (FaultDescription faultDescription in operation.Faults)
                {
                    faultRepliesList.Add(BuildFaultReplies(faultDescription, contentIsMessage));
                }
            }

            faultReplies = faultRepliesList;

            return reply;
        }
Exemple #8
0
        public static SendReply FromOperationDescription(OperationDescription operation, out IEnumerable <SendReply> faultReplies)
        {
            if (operation == null)
            {
                throw FxTrace.Exception.ArgumentNull("operation", "OperationDescription cannot be null");
            }

            bool contentIsParameter = false;
            bool contentIsMessage   = false;
            bool isSendContentEmpty = false;

            MessageDescription message;

            faultReplies = null;
            List <SendReply> faultRepliesList = new List <SendReply>();
            SendReply        reply            = null;

            if (operation.IsOneWay)
            {
                return(null);
            }

            if (operation.Messages.Count > 1)
            {
                reply             = new SendReply();
                reply.Action      = operation.Messages[1].Action;
                reply.DisplayName = operation.Name + "SendReply";

                message = operation.Messages[1];

                contentIsParameter = false;

                if (message.MessageType == null)
                {
                    if (message.Body.ReturnValue != null && message.Body.ReturnValue.Type != typeof(void))
                    {
                        if (!message.Body.ReturnValue.Type.IsAssignableFrom(typeof(System.ServiceModel.Channels.Message)))
                        {
                            contentIsParameter = true;
                        }

                        isSendContentEmpty = true;
                    }
                }

                if (message.MessageType == null)
                {
                    if (message.Body.Parts != null)
                    {
                        if (message.Body.Parts.Count > 0)
                        {
                            MessagePartDescriptionCollection parts = message.Body.Parts;
                            foreach (MessagePartDescription messagePart in parts)
                            {
                                if (messagePart.Index >= 0)
                                {
                                    contentIsParameter = true;
                                    break;
                                }
                                if (!messagePart.Type.IsAssignableFrom(typeof(System.ServiceModel.Channels.Message)))
                                {
                                    contentIsParameter = true;
                                }
                            }
                            isSendContentEmpty = true;
                        }
                    }
                }

                if (isSendContentEmpty)
                {
                    if (contentIsParameter)
                    {
                        SendParametersContent content = new SendParametersContent();
                        if (message.Direction == MessageDirection.Output &&
                            message.Body.ReturnValue != null &&
                            message.Body.ReturnValue.Type != typeof(void))
                        {
                            Argument returnArgument = InArgument.Create(message.Body.ReturnValue.Type, ArgumentDirection.In);
                            content.Parameters.Add(message.Body.ReturnValue.Name, (InArgument)returnArgument);
                        }

                        if (message.Direction == MessageDirection.Output && message.Body.Parts != null)
                        {
                            foreach (MessagePartDescription messagePart in message.Body.Parts)
                            {
                                Argument inArgument = InArgument.Create(messagePart.Type, ArgumentDirection.In);
                                content.Parameters.Add(messagePart.Name, (InArgument)(inArgument));
                            }
                        }
                        contentIsMessage = false;
                        reply.Content    = content;
                    }
                    else
                    {
                        // We must have an untyped message contract
                        //
                        SendMessageContent content = new SendMessageContent();
                        if (message.Direction == MessageDirection.Output)
                        {
                            content.DeclaredMessageType = message.Body.ReturnValue.Type;
                            Argument inArgument = InArgument.Create(content.DeclaredMessageType, ArgumentDirection.In);
                            content.Message = (InArgument)(inArgument);
                        }
                        contentIsMessage = true;
                        reply.Content    = content;
                    }
                }
                else
                {
                    if (message.MessageType != null && message.MessageType.IsDefined(typeof(MessageContractAttribute), false))
                    {
                        SendMessageContent sendMessageContent;
                        sendMessageContent = new SendMessageContent();
                        sendMessageContent.DeclaredMessageType = message.MessageType;
                        Argument inArgument = InArgument.Create(sendMessageContent.DeclaredMessageType, ArgumentDirection.In);
                        sendMessageContent.Message = (InArgument)(inArgument);
                        reply.Content    = sendMessageContent;
                        contentIsMessage = true;
                    }
                    else if (operation.Messages[0].MessageType != null)
                    {
                        reply.Content    = new SendMessageContent();
                        contentIsMessage = true;
                    }
                    else if (operation.Messages[0].Body.Parts != null &&
                             operation.Messages[0].Body.Parts.Count == 1 &&
                             operation.Messages[0].Body.Parts[0].Type.IsAssignableFrom(typeof(System.ServiceModel.Channels.Message)))
                    {
                        reply.Content    = new SendMessageContent();
                        contentIsMessage = true;
                    }
                    else
                    {
                        reply.Content    = new SendParametersContent();
                        contentIsMessage = false;
                    }
                }
            }

            if (operation.Faults != null)
            {
                foreach (FaultDescription faultDescription in operation.Faults)
                {
                    faultRepliesList.Add(BuildFaultReplies(faultDescription, contentIsMessage));
                }
            }

            faultReplies = faultRepliesList;

            return(reply);
        }