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
- PersonalizationProvider.cs
- ConfigXmlCDataSection.cs
- DuplexChannelBinder.cs
- CacheHelper.cs
- SqlParameterizer.cs
- FlowDocument.cs
- XPathEmptyIterator.cs
- CompModSwitches.cs
- XmlTextReader.cs
- UnmanagedMarshal.cs
- CertificateManager.cs
- RoutedPropertyChangedEventArgs.cs
- KeyValueSerializer.cs
- XmlSchemaDatatype.cs
- WebPartCloseVerb.cs
- wgx_sdk_version.cs
- CodeTypeParameterCollection.cs
- LassoSelectionBehavior.cs
- MappingException.cs
- CodeVariableDeclarationStatement.cs
- DataGridBoolColumn.cs
- ToolboxService.cs
- ObjectItemLoadingSessionData.cs
- SerialErrors.cs
- FixedPosition.cs
- DomainLiteralReader.cs
- RemotingException.cs
- DynamicActivity.cs
- CounterSetInstanceCounterDataSet.cs
- XmlEventCache.cs
- ExtendedTransformFactory.cs
- BitmapEffectCollection.cs
- FloaterBaseParaClient.cs
- UpdateTracker.cs
- GlobalItem.cs
- XmlSchemaAttributeGroupRef.cs
- PaginationProgressEventArgs.cs
- CharacterShapingProperties.cs
- ImpersonateTokenRef.cs
- TextCollapsingProperties.cs
- ServiceOperationHelpers.cs
- MembershipPasswordException.cs
- PaperSource.cs
- GroupItemAutomationPeer.cs
- LinqTreeNodeEvaluator.cs
- Rotation3D.cs
- PropertyDescriptor.cs
- CachedTypeface.cs
- METAHEADER.cs
- OrderedParallelQuery.cs
- MatrixUtil.cs
- ComboBox.cs
- HuffModule.cs
- XmlSchemaProviderAttribute.cs
- ScriptReference.cs
- DataGridCommandEventArgs.cs
- Opcode.cs
- ByteAnimationBase.cs
- DataBindingExpressionBuilder.cs
- DataReaderContainer.cs
- SubMenuStyle.cs
- CodeNamespaceCollection.cs
- UserControlBuildProvider.cs
- ActivityExecutionContextCollection.cs
- CalendarTable.cs
- SafeCryptoHandles.cs
- CommandBindingCollection.cs
- SoapMessage.cs
- TableCell.cs
- Type.cs
- ErrorWrapper.cs
- TableCell.cs
- GridViewRowPresenterBase.cs
- DataGridViewColumn.cs
- DeferredRunTextReference.cs
- SerializationSectionGroup.cs
- ScriptRegistrationManager.cs
- EndPoint.cs
- PtsHost.cs
- Util.cs
- ImportCatalogPart.cs
- TriggerActionCollection.cs
- OdbcCommandBuilder.cs
- SingleObjectCollection.cs
- LogicalExpr.cs
- HttpApplication.cs
- CheckBoxAutomationPeer.cs
- StrongNameKeyPair.cs
- HttpRequestCacheValidator.cs
- TextComposition.cs
- DiscoveryDocumentLinksPattern.cs
- DbConnectionStringBuilder.cs
- BitHelper.cs
- ToolboxControl.cs
- SpAudioStreamWrapper.cs
- TrackBar.cs
- BindingMemberInfo.cs
- PerformanceCounterPermission.cs
- Underline.cs
- PublisherMembershipCondition.cs