Exemplo n.º 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);
        }
Exemplo n.º 2
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);
        }