ContractValidationHelper.cs source code in C# .NET

Source code for the .NET framework in C#

                        

Code:

/ 4.0 / 4.0 / untmp / DEVDIV_TFS / Dev10 / Releases / RTMRel / ndp / cdf / src / NetFx40 / System.ServiceModel.Activities / System / ServiceModel / Activities / ContractValidationHelper.cs / 1305376 / ContractValidationHelper.cs

                            //------------------------------------------------------------------------------ 
// Copyright (c) Microsoft Corporation.  All rights reserved.
//-----------------------------------------------------------------------------

namespace System.ServiceModel.Activities 
{
    using System.Activities; 
    using System.Runtime; 
    using System.Xml.Linq;
    using SR2 = System.ServiceModel.Activities.SR; 

    static class ContractValidationHelper
    {
        public static void ValidateReceiveWithReceive(Receive receive1, Receive receive2) 
        {
            Fx.Assert(receive1 != null && receive2 != null, "Validation argument cannot be null!"); 
            Fx.Assert(receive1.OperationName != null, "OperationName cannot be null in Receive"); 
            string receiveOperationName = receive1.OperationName;
 
            if (receive1.Action != receive2.Action)
            {
                throw FxTrace.Exception.AsError(new ValidationException(SR2.TwoReceivesWithSameNameButDifferentAction(receiveOperationName)));
            } 

            if (receive1.InternalContent is ReceiveMessageContent && receive2.InternalContent is ReceiveMessageContent) 
            { 
                ReceiveMessageContent receiveMessage1 = receive1.InternalContent as ReceiveMessageContent;
                ReceiveMessageContent receiveMessage2 = receive2.InternalContent as ReceiveMessageContent; 

                ValidateReceiveWithReceive(receiveMessage1, receiveMessage2, receiveOperationName);
            }
            else if (receive1.InternalContent is ReceiveParametersContent && receive2.InternalContent is ReceiveParametersContent) 
            {
                ReceiveParametersContent receiveParameters1 = receive1.InternalContent as ReceiveParametersContent; 
                ReceiveParametersContent receiveParameters2 = receive2.InternalContent as ReceiveParametersContent; 

                ValidateReceiveParametersWithReceiveParameters(receiveParameters1, receiveParameters2, receiveOperationName); 
            }
            else
            {
                throw FxTrace.Exception.AsError(new ValidationException(SR2.ReceiveAndReceiveParametersHaveSameName(receiveOperationName))); 
            }
 
            if (receive1.HasReply && receive2.HasReply) 
            {
                ValidateSendReplyWithSendReply(receive1.FollowingReplies[0], receive2.FollowingReplies[0]); 
            }
            else if ((receive1.HasReply || receive1.HasFault) != (receive2.HasReply || receive2.HasFault))
            {
                throw FxTrace.Exception.AsError(new ValidationException(SR2.TwoReceivesWithSameNameButDifferentIsOneWay(receiveOperationName))); 
            }
 
            if ((receive1.InternalReceive.AdditionalData.IsInsideTransactedReceiveScope != receive2.InternalReceive.AdditionalData.IsInsideTransactedReceiveScope) || 
                (receive1.InternalReceive.AdditionalData.IsFirstReceiveOfTransactedReceiveScopeTree != receive2.InternalReceive.AdditionalData.IsFirstReceiveOfTransactedReceiveScopeTree))
            { 
                throw FxTrace.Exception.AsError(new ValidationException(SR2.TwoReceivesWithSameNameButDifferentTxProperties(receiveOperationName)));
            }
        }
 
        static void ValidateReceiveWithReceive(ReceiveMessageContent receive1, ReceiveMessageContent receive2, string receiveOperationName)
        { 
            Fx.Assert(receive1 != null && receive2 != null, "Validation argument cannot be null!"); 

            if (receive1.InternalDeclaredMessageType != receive2.InternalDeclaredMessageType) 
            {
                throw FxTrace.Exception.AsError(new ValidationException(SR2.TwoReceivesWithSameNameButDifferentValueType(receiveOperationName)));
            }
        } 

        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)));
                } 
            } 
        }
 
        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 string GetErrorMessageEndpointName(string endpointName)
        { 
            return !string.IsNullOrEmpty(endpointName) ? endpointName : SR.NotSpecified; 
        }
 
        public static string GetErrorMessageEndpointServiceContractName(XName serviceContractName)
        {
            return serviceContractName != null ? serviceContractName.LocalName : SR.NotSpecified;
        } 

        public static string GetErrorMessageOperationName(string operationName) 
        { 
            return !string.IsNullOrEmpty(operationName) ? operationName : SR.NotSpecified;
        } 
    }
}

// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
                        

Link Menu

Network programming in C#, Network Programming in VB.NET, Network Programming in .NET
This book is available now!
Buy at Amazon US or
Buy at Amazon UK