Code:
/ 4.0 / 4.0 / untmp / DEVDIV_TFS / Dev10 / Releases / RTMRel / ndp / cdf / src / NetFx40 / System.ServiceModel.Routing / System / ServiceModel / Routing / RoutingBehavior.cs / 1305376 / RoutingBehavior.cs
//----------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
//---------------------------------------------------------------
namespace System.ServiceModel.Routing
{
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Runtime;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.ServiceModel.Channels;
using System.ServiceModel.Description;
using System.ServiceModel.Dispatcher;
[Fx.Tag.XamlVisible(false)]
public sealed class RoutingBehavior : IServiceBehavior
{
RoutingConfiguration configuration;
public RoutingBehavior(RoutingConfiguration routingConfiguration)
{
if (routingConfiguration == null)
{
throw FxTrace.Exception.ArgumentNull("routingConfiguration");
}
this.configuration = routingConfiguration;
}
void IServiceBehavior.AddBindingParameters(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase, Collection endpoints, BindingParameterCollection bindingParameters)
{
}
void IServiceBehavior.ApplyDispatchBehavior(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase)
{
RoutingExtension routingExtension = new RoutingExtension(this.configuration);
serviceHostBase.Extensions.Add(routingExtension);
for (int i = 0; i < serviceHostBase.ChannelDispatchers.Count; i++)
{
ChannelDispatcher channelDispatcher = serviceHostBase.ChannelDispatchers[i] as ChannelDispatcher;
if (channelDispatcher != null)
{
foreach (EndpointDispatcher endpointDispatcher in channelDispatcher.Endpoints)
{
if (!endpointDispatcher.IsSystemEndpoint &&
RoutingUtilities.IsRoutingServiceNamespace(endpointDispatcher.ContractNamespace))
{
DispatchRuntime dispatchRuntime = endpointDispatcher.DispatchRuntime;
//Since we use PerSession instancing this concurrency only applies to messages
//in the same session, also needed to maintain order.
dispatchRuntime.ConcurrencyMode = ConcurrencyMode.Single;
}
}
}
}
}
void IServiceBehavior.Validate(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase)
{
HashSet endpoints = new HashSet();
foreach (ServiceEndpoint endpoint in serviceDescription.Endpoints)
{
if (!endpoint.IsSystemEndpoint &&
RoutingUtilities.IsRoutingServiceNamespace(endpoint.Contract.Namespace))
{
endpoint.Behaviors.Add(new RoutingEndpointBehavior(endpoint));
endpoints.Add(endpoint.Name);
}
}
EndpointNameMessageFilter.Validate(this.configuration.InternalFilterTable.Keys, endpoints);
}
public static Type GetContractForDescription(ContractDescription description)
{
if (description == null)
{
throw FxTrace.Exception.ArgumentNull("description");
}
if (description.CallbackContractType != null)
{
return typeof(IDuplexSessionRouter);
}
bool allOneWay = true;
foreach (OperationDescription operation in description.Operations)
{
if (!operation.IsOneWay)
{
allOneWay = false;
break;
}
}
if (allOneWay)
{
if (description.SessionMode == SessionMode.Required)
{
return typeof(ISimplexSessionRouter);
}
else
{
return typeof(ISimplexDatagramRouter);
}
}
else
{
return typeof(IRequestReplyRouter);
}
}
internal class RoutingEndpointBehavior : IEndpointBehavior, IChannelInitializer, IInputSessionShutdown
{
public RoutingEndpointBehavior(ServiceEndpoint endpoint)
{
this.Endpoint = endpoint;
this.EndpointName = endpoint.Name;
}
internal ChannelDispatcher ChannelDispatcher
{
get;
private set;
}
internal ServiceEndpoint Endpoint
{
get;
private set;
}
internal string EndpointName
{
get;
private set;
}
internal bool ImpersonationRequired
{
get;
private set;
}
internal bool ReceiveContextEnabled
{
get;
private set;
}
internal bool TransactedReceiveEnabled
{
get;
private set;
}
public void AddBindingParameters(ServiceEndpoint endpoint, BindingParameterCollection bindingParameters)
{
//Turn on ReceiveContext here if supported
IReceiveContextSettings receiveContextSettings = endpoint.Binding.GetProperty(bindingParameters);
if (receiveContextSettings != null)
{
receiveContextSettings.Enabled = true;
}
}
public void ApplyClientBehavior(ServiceEndpoint endpoint, ClientRuntime clientRuntime)
{
}
public void ApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher)
{
this.ChannelDispatcher = endpointDispatcher.ChannelDispatcher;
this.ChannelDispatcher.ChannelInitializers.Add(this);
endpointDispatcher.DispatchRuntime.InputSessionShutdownHandlers.Add(this);
endpointDispatcher.DispatchRuntime.AutomaticInputSessionShutdown = false;
if (endpointDispatcher.DispatchRuntime.ImpersonateCallerForAllOperations)
{
this.ImpersonationRequired = true;
}
else if (AspNetEnvironment.Current.AspNetCompatibilityEnabled)
{
this.ImpersonationRequired = true;
}
BindingParameterCollection bindingParams = new BindingParameterCollection();
if (RoutingUtilities.IsTransactedReceive(endpoint.Binding, bindingParams))
{
foreach (OperationDescription operation in endpoint.Contract.Operations)
{
if (operation.Behaviors.Find() == null)
{
operation.Behaviors.Add(new TransactedReceiveOperationBehavior());
}
}
this.ChannelDispatcher.IsTransactedReceive = true;
endpointDispatcher.DispatchRuntime.TransactionAutoCompleteOnSessionClose = true;
this.TransactedReceiveEnabled = true;
}
IReceiveContextSettings rcSettings = endpoint.Binding.GetProperty(bindingParams);
if (rcSettings != null && rcSettings.Enabled)
{
foreach (OperationDescription operation in endpoint.Contract.Operations)
{
ReceiveContextEnabledAttribute rcEnabled = new ReceiveContextEnabledAttribute();
rcEnabled.ManualControl = true;
operation.Behaviors.Add(rcEnabled);
}
this.ReceiveContextEnabled = true;
//Switch TransactedReceive off, because we don't want the Dispatcher creating any Transaction
endpointDispatcher.ChannelDispatcher.IsTransactedReceive = false;
endpointDispatcher.DispatchRuntime.TransactionAutoCompleteOnSessionClose = false;
}
}
public void Validate(ServiceEndpoint endpoint)
{
}
void IChannelInitializer.Initialize(IClientChannel channel)
{
RoutingChannelExtension channelState = RoutingChannelExtension.Create(this);
channel.Extensions.Add(channelState);
}
void IInputSessionShutdown.ChannelFaulted(IDuplexContextChannel channel)
{
RoutingChannelExtension channelExtension = channel.Extensions.Find();
if (channelExtension != null)
{
channelExtension.Fault(new CommunicationObjectFaultedException());
}
else
{
RoutingUtilities.Abort(channel, channel.LocalAddress);
}
}
void IInputSessionShutdown.DoneReceiving(IDuplexContextChannel channel)
{
RoutingChannelExtension channelExtension = channel.Extensions.Find();
channelExtension.DoneReceiving(this.Endpoint.Binding.CloseTimeout);
}
}
class TransactedReceiveOperationBehavior : IOperationBehavior
{
public void AddBindingParameters(OperationDescription operationDescription, BindingParameterCollection bindingParameters)
{
}
public void ApplyClientBehavior(OperationDescription operationDescription, ClientOperation clientOperation)
{
}
public void ApplyDispatchBehavior(OperationDescription operationDescription, DispatchOperation dispatchOperation)
{
if (operationDescription.Behaviors.Find() == null)
{
dispatchOperation.TransactionRequired = true;
}
ContractDescription contract = operationDescription.DeclaringContract;
if (dispatchOperation.IsOneWay && contract.SessionMode == SessionMode.Required)
{
dispatchOperation.Parent.ConcurrencyMode = ConcurrencyMode.Single;
dispatchOperation.Parent.ReleaseServiceInstanceOnTransactionComplete = false;
dispatchOperation.TransactionAutoComplete = false;
}
}
public void Validate(OperationDescription operationDescription)
{
}
}
}
}
// 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
- KeyboardDevice.cs
- CollectionBuilder.cs
- ISAPIApplicationHost.cs
- CellRelation.cs
- RSACryptoServiceProvider.cs
- ExecutionEngineException.cs
- XPathDocumentNavigator.cs
- StylusPointCollection.cs
- RuleSetDialog.Designer.cs
- ApplicationServicesHostFactory.cs
- BaseParser.cs
- ListenerElementsCollection.cs
- DataSourceView.cs
- OneToOneMappingSerializer.cs
- HuffCodec.cs
- RuleSetReference.cs
- TextEffect.cs
- ContentControl.cs
- GcSettings.cs
- CodeDomExtensionMethods.cs
- XmlQuerySequence.cs
- CompositeControl.cs
- CollectionsUtil.cs
- TextRenderer.cs
- SecureStringHasher.cs
- TextStore.cs
- EntityDataSourceMemberPath.cs
- TextBoxAutomationPeer.cs
- ImmutableObjectAttribute.cs
- DataGrid.cs
- DataError.cs
- XmlSiteMapProvider.cs
- DataGridViewCellLinkedList.cs
- DataGridParentRows.cs
- ReachFixedPageSerializerAsync.cs
- WindowsNonControl.cs
- AsyncOperationLifetimeManager.cs
- _BaseOverlappedAsyncResult.cs
- BindingContext.cs
- OutputCacheSettings.cs
- ListItemCollection.cs
- HttpServerUtilityWrapper.cs
- SaveLedgerEntryRequest.cs
- WebPartUserCapability.cs
- X509Chain.cs
- RectAnimationUsingKeyFrames.cs
- RadioButtonAutomationPeer.cs
- Package.cs
- ControlEvent.cs
- SetterBaseCollection.cs
- NameValueSectionHandler.cs
- Matrix.cs
- InheritanceContextHelper.cs
- WebPartConnection.cs
- ReaderWriterLockSlim.cs
- HierarchicalDataBoundControl.cs
- RoutedEventConverter.cs
- RichListBox.cs
- DecimalAnimationBase.cs
- ClientProxyGenerator.cs
- DesignerOptionService.cs
- IOException.cs
- WebDescriptionAttribute.cs
- DetailsViewInsertEventArgs.cs
- ContextMenuService.cs
- TextParagraphView.cs
- FacetValues.cs
- DataBoundControlHelper.cs
- DataPager.cs
- ContextDataSourceView.cs
- DllHostedComPlusServiceHost.cs
- NativeMethods.cs
- SortDescription.cs
- XmlAttributeOverrides.cs
- ObjectStateEntryOriginalDbUpdatableDataRecord.cs
- _NativeSSPI.cs
- OutputCacheSection.cs
- MsmqIntegrationBindingElement.cs
- DrawingServices.cs
- TemplateColumn.cs
- TextServicesLoader.cs
- AssemblyNameProxy.cs
- ProxyWebPartManager.cs
- TraceUtils.cs
- Util.cs
- ObjectComplexPropertyMapping.cs
- InvokeProviderWrapper.cs
- TypeContext.cs
- KerberosRequestorSecurityToken.cs
- PersonalizationStateInfoCollection.cs
- MarkupObject.cs
- FileLogRecordStream.cs
- ExecutionEngineException.cs
- HtmlInputPassword.cs
- IApplicationTrustManager.cs
- Utils.cs
- JapaneseLunisolarCalendar.cs
- wgx_commands.cs
- WorkflowServiceAttributesTypeConverter.cs
- DelayDesigner.cs