ToReply.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 / ToReply.cs / 1305376 / ToReply.cs

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

namespace System.ServiceModel.Activities 
{
    using System.Activities; 
    using System.Collections.Generic; 
    using System.Collections.ObjectModel;
    using System.Globalization; 
    using System.Runtime;
    using System.ServiceModel.Channels;
    using System.ServiceModel.Dispatcher;
    using SR2 = System.ServiceModel.Activities.SR; 

    class ToReply : NativeActivity 
    { 
        IDispatchMessageFormatter formatter;
        IDispatchFaultFormatter faultFormatter; 

        Collection parameters;

        public IDispatchMessageFormatter Formatter 
        {
            get 
            { 
                return this.formatter;
            } 
            set
            {
                this.formatter = value;
                ValidateFormatters(); 
            }
        } 
 
        public IDispatchFaultFormatter FaultFormatter
        { 
            get
            {
                return this.faultFormatter;
            } 
            set
            { 
                this.faultFormatter = value; 
                ValidateFormatters();
            } 
        }

        public bool IncludeExceptionDetailInFaults
        { 
            get;
            set; 
        } 

        public InArgument Result 
        {
            get;
            set;
        } 

        public Collection Parameters 
        { 
            get
            { 
                if (this.parameters == null)
                {
                    this.parameters = new Collection();
                } 
                return this.parameters;
            } 
        } 

        //CorrelationHandle is required to get the message version from the InternalReceivedMessage 
        public InArgument CorrelatesWith
        {
            get;
            set; 
        }
 
        public OutArgument Message 
        {
            get; 
            set;
        }

        protected override void CacheMetadata(NativeActivityMetadata metadata) 
        {
            if (this.Result != null) 
            { 
                RuntimeArgument resultArgument = new RuntimeArgument(Constants.Result, this.Result.ArgumentType, ArgumentDirection.In);
                metadata.Bind(this.Result, resultArgument); 
                metadata.AddArgument(resultArgument);
            }

            if (this.parameters != null) 
            {
                int count = 0; 
                foreach (InArgument parameter in this.parameters) 
                {
                    RuntimeArgument parameterArgument = new RuntimeArgument(Constants.Parameter + count++, parameter.ArgumentType, ArgumentDirection.In); 
                    metadata.Bind(parameter, parameterArgument);
                    metadata.AddArgument(parameterArgument);
                }
            } 

            RuntimeArgument messageArgument = new RuntimeArgument(Constants.Message, Constants.MessageType, ArgumentDirection.Out, true); 
            metadata.Bind(this.Message, messageArgument); 
            metadata.AddArgument(messageArgument);
 
            RuntimeArgument correlatesWithArgument = new RuntimeArgument(Constants.CorrelatesWith, Constants.CorrelationHandleType, ArgumentDirection.In);
            metadata.Bind(this.CorrelatesWith, correlatesWithArgument);
            metadata.AddArgument(correlatesWithArgument);
 
        }
 
        protected override void Execute(NativeActivityContext context) 
        {
            CorrelationHandle correlatesWith = (this.CorrelatesWith == null) ? null : this.CorrelatesWith.Get(context); 
            if (correlatesWith == null)
            {
                correlatesWith = context.Properties.Find(CorrelationHandle.StaticExecutionPropertyName) as CorrelationHandle;
            } 

            CorrelationResponseContext responseContext; 
            if (correlatesWith == null || !correlatesWith.TryAcquireResponseContext(context, out responseContext)) 
            {
                throw FxTrace.Exception.AsError(new InvalidOperationException(SR2.CorrelationResponseContextShouldNotBeNull)); 
            }

            //Register the ResponseContext so that InternalSendMessage can access it.
            if (!correlatesWith.TryRegisterResponseContext(context, responseContext)) 
            {
                throw FxTrace.Exception.AsError(new InvalidOperationException(SR2.ResponseContextIsNotNull)); 
            } 

            Fx.Assert((this.Formatter == null && this.FaultFormatter != null) || 
                    (this.Formatter != null && this.FaultFormatter == null),
                    "OperationFormatter and FaultFormatter cannot be both null or both set!");

            //Use the same MessageVersion as the incoming message that is retrieved using CorrelatonHandle 
            MessageVersion version = responseContext.MessageVersion;
 
            if (this.FaultFormatter != null) 
            {
                Fx.Assert(this.parameters.Count == 1, "Exception should be the only parameter!"); 
                Exception exception = this.parameters[0].Get(context) as Exception;
                Fx.Assert(exception != null, "InArgument must be an Exception!");

                MessageFault messageFault; 
                string action;
 
                FaultException faultException = exception as FaultException; 
                if (faultException != null)
                { 
                    // This is an expected fault
                    // Reproduce logic from ErrorBehavior.InitializeFault

                    messageFault = this.FaultFormatter.Serialize(faultException, out action); 
                    if (action == null)
                    { 
                        action = version.Addressing.DefaultFaultAction; 
                    }
                } 
                else
                {
                    // This is an unexpected fault
                    // Reproduce logic from ErrorBehavior.ProvideFaultOfLastResort 

                    FaultCode code = new FaultCode(FaultCodeConstants.Codes.InternalServiceFault, FaultCodeConstants.Namespaces.NetDispatch); 
                    code = FaultCode.CreateReceiverFaultCode(code); 

                    action = FaultCodeConstants.Actions.NetDispatcher; 

                    if (this.IncludeExceptionDetailInFaults)
                    {
                        messageFault = MessageFault.CreateFault(code, 
                            new FaultReason(new FaultReasonText(exception.Message, CultureInfo.CurrentCulture)),
                            new ExceptionDetail(exception)); 
                    } 
                    else
                    { 
                        messageFault = MessageFault.CreateFault(code,
                            new FaultReason(new FaultReasonText(SR2.InternalServerError, CultureInfo.CurrentCulture)));
                    }
                } 

                if (messageFault == null) 
                { 
                    throw FxTrace.Exception.AsError(new InvalidOperationException(SR2.CannotCreateMessageFault));
                } 
                else
                {
                    Message outMessage = System.ServiceModel.Channels.Message.CreateMessage(version, messageFault, action);
                    this.Message.Set(context, outMessage); 
                }
            } 
            else 
            {
                object[] inObjects; 
                if (this.parameters != null)
                {
                    inObjects = new object[this.parameters.Count];
                    for (int i = 0; i < this.parameters.Count; i++) 
                    {
                        Fx.Assert(this.Parameters[i] != null, "Parameter cannot be null"); 
                        inObjects[i] = this.parameters[i].Get(context); 
                    }
                } 
                else
                {
                    inObjects = Constants.EmptyArray;
                } 

                object returnValue = null; 
                if (this.Result != null) 
                {
                    returnValue = this.Result.Get(context); 
                }

                Message outMessage = this.Formatter.SerializeReply(version, inObjects, returnValue);
                this.Message.Set(context, outMessage); 
            }
        } 
 
        void ValidateFormatters()
        { 
            if (this.Formatter == null && this.FaultFormatter == null)
            {
                throw FxTrace.Exception.AsError(new ValidationException(SR2.OperationFormatterAndFaultFormatterNotSet));
            } 
            if (this.Formatter != null && this.FaultFormatter != null)
            { 
                throw FxTrace.Exception.AsError(new ValidationException(SR2.OperationFormatterAndFaultFormatterIncorrectlySet)); 
            }
        } 
    }
}

// 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