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; Collectionparameters; 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
This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- ConnectAlgorithms.cs
- CurrencyWrapper.cs
- SplitterCancelEvent.cs
- TextSpan.cs
- DataGridViewImageColumn.cs
- WmpBitmapEncoder.cs
- DocumentPaginator.cs
- GiveFeedbackEvent.cs
- UnionCqlBlock.cs
- DataStorage.cs
- StrongNameKeyPair.cs
- PromptStyle.cs
- AspNetHostingPermission.cs
- DesignerActionPropertyItem.cs
- ParenthesizePropertyNameAttribute.cs
- BooleanProjectedSlot.cs
- TargetPerspective.cs
- WebControlAdapter.cs
- TextEffectCollection.cs
- ValidatedControlConverter.cs
- InstanceLockTracking.cs
- DataGridTextBoxColumn.cs
- HttpApplicationStateBase.cs
- ConstrainedDataObject.cs
- ITreeGenerator.cs
- ExpressionQuoter.cs
- ViewPort3D.cs
- WebScriptEndpointElement.cs
- EdmScalarPropertyAttribute.cs
- PenThread.cs
- EventMetadata.cs
- AsyncPostBackErrorEventArgs.cs
- CompModSwitches.cs
- TableColumn.cs
- DataContractSet.cs
- Util.cs
- PathSegment.cs
- SymbolDocumentInfo.cs
- InputScopeAttribute.cs
- XamlSerializationHelper.cs
- StrokeDescriptor.cs
- ToolStripDropTargetManager.cs
- SqlTypesSchemaImporter.cs
- DecoderFallback.cs
- AppDomainUnloadedException.cs
- XmlDigitalSignatureProcessor.cs
- ValidationHelper.cs
- GridEntryCollection.cs
- DiscriminatorMap.cs
- SqlDataSourceQueryConverter.cs
- WebPartDescription.cs
- Run.cs
- IconHelper.cs
- AsymmetricKeyExchangeDeformatter.cs
- SystemUdpStatistics.cs
- MenuItemStyleCollectionEditor.cs
- ObjectViewEntityCollectionData.cs
- FlagsAttribute.cs
- AsymmetricAlgorithm.cs
- LinqDataSourceEditData.cs
- HealthMonitoringSection.cs
- HttpCapabilitiesSectionHandler.cs
- EmbeddedMailObject.cs
- XmlCharacterData.cs
- SpeechDetectedEventArgs.cs
- TabControl.cs
- IntegerCollectionEditor.cs
- WmfPlaceableFileHeader.cs
- TextAutomationPeer.cs
- SqlReferenceCollection.cs
- BamlTreeMap.cs
- LinkButton.cs
- RegexRunner.cs
- InvokeHandlers.cs
- XmlException.cs
- KeyBinding.cs
- Mapping.cs
- MembershipValidatePasswordEventArgs.cs
- ActivityDesignerAccessibleObject.cs
- PassportAuthenticationModule.cs
- DataControlLinkButton.cs
- Config.cs
- Tuple.cs
- XamlInt32CollectionSerializer.cs
- DecimalKeyFrameCollection.cs
- RepeaterCommandEventArgs.cs
- KeyGestureConverter.cs
- AnonymousIdentificationModule.cs
- WindowsAltTab.cs
- XmlSchemaParticle.cs
- XmlnsDefinitionAttribute.cs
- ThrowHelper.cs
- SqlTypesSchemaImporter.cs
- SoundPlayer.cs
- Rotation3D.cs
- altserialization.cs
- ScrollProperties.cs
- Message.cs
- ColorBlend.cs
- LogSwitch.cs