Code:
/ 4.0 / 4.0 / DEVDIV_TFS / Dev10 / Releases / RTMRel / ndp / cdf / src / WCF / System.ServiceModel.Activation / System / ServiceModel / Activation / HostedHttpTransportManager.cs / 1305376 / HostedHttpTransportManager.cs
//---------------------------------------------------------------------------- // Copyright (c) Microsoft Corporation. All rights reserved. //--------------------------------------------------------------------------- namespace System.ServiceModel.Activation { using System.Diagnostics; using System.Globalization; using System.Runtime; using System.Runtime.Diagnostics; using System.Security; using System.Security.Permissions; using System.ServiceModel; using System.ServiceModel.Channels; using System.ServiceModel.Diagnostics; using System.Threading; using System.Web; class HostedHttpTransportManager : HttpTransportManager { string scheme; int port; string host; internal HostedHttpTransportManager(BaseUriWithWildcard baseAddress) : base(baseAddress.BaseAddress, baseAddress.HostNameComparisonMode) { base.IsHosted = true; } internal override bool IsCompatible(HttpChannelListener factory) { return true; } internal override void OnClose(TimeSpan timeout) { // empty } internal override void OnOpen() { // empty } internal override void OnAbort() { // empty } internal override string Scheme { get { return this.scheme ?? (this.scheme = this.ListenUri.Scheme); } } internal string Host { get { return this.host ?? (this.host = this.ListenUri.Host); } } internal int Port { get { return this.port == 0 ? (this.port = this.ListenUri.Port) : this.port; } } static bool canTraceConnectionInformation = true; public void TraceConnectionInformation(HostedHttpRequestAsyncResult result) { if (result != null && DiagnosticUtility.ShouldTraceInformation && canTraceConnectionInformation) { try { IServiceProvider provider = (IServiceProvider)result.Application.Context; HttpWorkerRequest workerRequest = (HttpWorkerRequest)provider.GetService(typeof(HttpWorkerRequest)); string localAddress = string.Format(CultureInfo.InvariantCulture, "{0}:{1}", workerRequest.GetLocalAddress(), workerRequest.GetLocalPort()); string remoteAddress = string.Format(CultureInfo.InvariantCulture, "{0}:{1}", workerRequest.GetRemoteAddress(), workerRequest.GetRemotePort()); TraceUtility.TraceHttpConnectionInformation(localAddress, remoteAddress, this); } catch (SecurityException e) { canTraceConnectionInformation = false; // not re-throwing on purpose if (DiagnosticUtility.ShouldTraceWarning) { DiagnosticUtility.ExceptionUtility.TraceHandledException(e, TraceEventType.Warning); } } } } [Fx.Tag.SecurityNote(Critical = "Calls getters with LinkDemands in ASP .NET objects.", Safe = "Only returns the activity, doesn't leak the ASP .NET objects.")] [SecuritySafeCritical] public ServiceModelActivity CreateReceiveBytesActivity(HostedHttpRequestAsyncResult result) { ServiceModelActivity retval = null; if (result != null) { TraceMessageReceived(result.RequestUri); if (DiagnosticUtility.ShouldUseActivity) { IServiceProvider provider = (IServiceProvider)result.Application.Context; retval = ServiceModelActivity.CreateBoundedActivity(GetRequestTraceIdentifier(provider)); StartReceiveBytesActivity(retval, result.RequestUri); } } return retval; } [Fx.Tag.SecurityNote(Critical = "Uses the HttpWorkerRequest to get the trace identifier, which Demands UnamangedCode.", Safe = "Only returns the trace id, doesn't leak the HttpWorkerRequest.")] [SecuritySafeCritical] [SecurityPermission(SecurityAction.Assert, UnmanagedCode = true)] static Guid GetRequestTraceIdentifier(IServiceProvider provider) { return ((HttpWorkerRequest)provider.GetService(typeof(HttpWorkerRequest))).RequestTraceIdentifier; } internal void HttpContextReceived(HostedHttpRequestAsyncResult result) { using (DiagnosticUtility.ShouldUseActivity ? ServiceModelActivity.BoundOperation(this.Activity) : null) { using (this.CreateReceiveBytesActivity(result)) { this.TraceConnectionInformation(result); HttpChannelListener listener; if (base.TryLookupUri(result.RequestUri, result.GetHttpMethod(), this.HostNameComparisonMode, out listener)) { HostedHttpContext hostedContext = new HostedHttpContext(listener, result); listener.HttpContextReceived(hostedContext, null); return; } if (DiagnosticUtility.ShouldTraceError) { TraceUtility.TraceEvent(TraceEventType.Error, TraceCode.HttpChannelMessageReceiveFailed, SR.TraceCodeHttpChannelMessageReceiveFailed, new StringTraceRecord("IsRecycling", ServiceHostingEnvironment.IsRecycling.ToString(CultureInfo.CurrentCulture)), this, null); } if (ServiceHostingEnvironment.IsRecycling) { throw FxTrace.Exception.AsError( new EndpointNotFoundException(SR.Hosting_ListenerNotFoundForActivationInRecycling(result.RequestUri.ToString()))); } else { throw FxTrace.Exception.AsError( new EndpointNotFoundException(SR.Hosting_ListenerNotFoundForActivation(result.RequestUri.ToString()))); } } } } } [Fx.Tag.SecurityNote(Critical = "Captures HttpContext.Current on construction, then can apply that state at a later time and reset on Dispose." + "Whole object is critical because where it was initially constructed can be used to control HttpContext.set_Current later and HttpContext.set_Current requires an elevation")] #pragma warning disable 618 // have not moved to the v4 security model yet [SecurityCritical(SecurityCriticalScope.Everything)] #pragma warning restore 618 class HostedThreadData { CultureInfo cultureInfo; CultureInfo uiCultureInfo; HttpContext httpContext; public HostedThreadData() { this.cultureInfo = CultureInfo.CurrentCulture; this.uiCultureInfo = CultureInfo.CurrentUICulture; this.httpContext = HttpContext.Current; } public IDisposable CreateContext() { return new HostedAspNetContext(this); } [SecurityPermission(SecurityAction.Assert, Unrestricted = true)] static void UnsafeApplyData(HostedThreadData data) { // We set the CallContext.HostContext directly instead of setting HttpContext.Current because // the latter uses a demand instead of a link demand, which is very expensive in partial trust. System.Runtime.Remoting.Messaging.CallContext.HostContext = data.httpContext; Thread currentThread = Thread.CurrentThread; if (currentThread.CurrentCulture != data.cultureInfo) { currentThread.CurrentCulture = data.cultureInfo; } if (currentThread.CurrentUICulture != data.uiCultureInfo) { currentThread.CurrentUICulture = data.uiCultureInfo; } } class HostedAspNetContext : IDisposable { HostedThreadData oldData; public HostedAspNetContext(HostedThreadData newData) { oldData = new HostedThreadData(); HostedThreadData.UnsafeApplyData(newData); } public void Dispose() { HostedThreadData.UnsafeApplyData(oldData); } } } } // File provided for Reference Use Only by Microsoft Corporation (c) 2007. //---------------------------------------------------------------------------- // Copyright (c) Microsoft Corporation. All rights reserved. //--------------------------------------------------------------------------- namespace System.ServiceModel.Activation { using System.Diagnostics; using System.Globalization; using System.Runtime; using System.Runtime.Diagnostics; using System.Security; using System.Security.Permissions; using System.ServiceModel; using System.ServiceModel.Channels; using System.ServiceModel.Diagnostics; using System.Threading; using System.Web; class HostedHttpTransportManager : HttpTransportManager { string scheme; int port; string host; internal HostedHttpTransportManager(BaseUriWithWildcard baseAddress) : base(baseAddress.BaseAddress, baseAddress.HostNameComparisonMode) { base.IsHosted = true; } internal override bool IsCompatible(HttpChannelListener factory) { return true; } internal override void OnClose(TimeSpan timeout) { // empty } internal override void OnOpen() { // empty } internal override void OnAbort() { // empty } internal override string Scheme { get { return this.scheme ?? (this.scheme = this.ListenUri.Scheme); } } internal string Host { get { return this.host ?? (this.host = this.ListenUri.Host); } } internal int Port { get { return this.port == 0 ? (this.port = this.ListenUri.Port) : this.port; } } static bool canTraceConnectionInformation = true; public void TraceConnectionInformation(HostedHttpRequestAsyncResult result) { if (result != null && DiagnosticUtility.ShouldTraceInformation && canTraceConnectionInformation) { try { IServiceProvider provider = (IServiceProvider)result.Application.Context; HttpWorkerRequest workerRequest = (HttpWorkerRequest)provider.GetService(typeof(HttpWorkerRequest)); string localAddress = string.Format(CultureInfo.InvariantCulture, "{0}:{1}", workerRequest.GetLocalAddress(), workerRequest.GetLocalPort()); string remoteAddress = string.Format(CultureInfo.InvariantCulture, "{0}:{1}", workerRequest.GetRemoteAddress(), workerRequest.GetRemotePort()); TraceUtility.TraceHttpConnectionInformation(localAddress, remoteAddress, this); } catch (SecurityException e) { canTraceConnectionInformation = false; // not re-throwing on purpose if (DiagnosticUtility.ShouldTraceWarning) { DiagnosticUtility.ExceptionUtility.TraceHandledException(e, TraceEventType.Warning); } } } } [Fx.Tag.SecurityNote(Critical = "Calls getters with LinkDemands in ASP .NET objects.", Safe = "Only returns the activity, doesn't leak the ASP .NET objects.")] [SecuritySafeCritical] public ServiceModelActivity CreateReceiveBytesActivity(HostedHttpRequestAsyncResult result) { ServiceModelActivity retval = null; if (result != null) { TraceMessageReceived(result.RequestUri); if (DiagnosticUtility.ShouldUseActivity) { IServiceProvider provider = (IServiceProvider)result.Application.Context; retval = ServiceModelActivity.CreateBoundedActivity(GetRequestTraceIdentifier(provider)); StartReceiveBytesActivity(retval, result.RequestUri); } } return retval; } [Fx.Tag.SecurityNote(Critical = "Uses the HttpWorkerRequest to get the trace identifier, which Demands UnamangedCode.", Safe = "Only returns the trace id, doesn't leak the HttpWorkerRequest.")] [SecuritySafeCritical] [SecurityPermission(SecurityAction.Assert, UnmanagedCode = true)] static Guid GetRequestTraceIdentifier(IServiceProvider provider) { return ((HttpWorkerRequest)provider.GetService(typeof(HttpWorkerRequest))).RequestTraceIdentifier; } internal void HttpContextReceived(HostedHttpRequestAsyncResult result) { using (DiagnosticUtility.ShouldUseActivity ? ServiceModelActivity.BoundOperation(this.Activity) : null) { using (this.CreateReceiveBytesActivity(result)) { this.TraceConnectionInformation(result); HttpChannelListener listener; if (base.TryLookupUri(result.RequestUri, result.GetHttpMethod(), this.HostNameComparisonMode, out listener)) { HostedHttpContext hostedContext = new HostedHttpContext(listener, result); listener.HttpContextReceived(hostedContext, null); return; } if (DiagnosticUtility.ShouldTraceError) { TraceUtility.TraceEvent(TraceEventType.Error, TraceCode.HttpChannelMessageReceiveFailed, SR.TraceCodeHttpChannelMessageReceiveFailed, new StringTraceRecord("IsRecycling", ServiceHostingEnvironment.IsRecycling.ToString(CultureInfo.CurrentCulture)), this, null); } if (ServiceHostingEnvironment.IsRecycling) { throw FxTrace.Exception.AsError( new EndpointNotFoundException(SR.Hosting_ListenerNotFoundForActivationInRecycling(result.RequestUri.ToString()))); } else { throw FxTrace.Exception.AsError( new EndpointNotFoundException(SR.Hosting_ListenerNotFoundForActivation(result.RequestUri.ToString()))); } } } } } [Fx.Tag.SecurityNote(Critical = "Captures HttpContext.Current on construction, then can apply that state at a later time and reset on Dispose." + "Whole object is critical because where it was initially constructed can be used to control HttpContext.set_Current later and HttpContext.set_Current requires an elevation")] #pragma warning disable 618 // have not moved to the v4 security model yet [SecurityCritical(SecurityCriticalScope.Everything)] #pragma warning restore 618 class HostedThreadData { CultureInfo cultureInfo; CultureInfo uiCultureInfo; HttpContext httpContext; public HostedThreadData() { this.cultureInfo = CultureInfo.CurrentCulture; this.uiCultureInfo = CultureInfo.CurrentUICulture; this.httpContext = HttpContext.Current; } public IDisposable CreateContext() { return new HostedAspNetContext(this); } [SecurityPermission(SecurityAction.Assert, Unrestricted = true)] static void UnsafeApplyData(HostedThreadData data) { // We set the CallContext.HostContext directly instead of setting HttpContext.Current because // the latter uses a demand instead of a link demand, which is very expensive in partial trust. System.Runtime.Remoting.Messaging.CallContext.HostContext = data.httpContext; Thread currentThread = Thread.CurrentThread; if (currentThread.CurrentCulture != data.cultureInfo) { currentThread.CurrentCulture = data.cultureInfo; } if (currentThread.CurrentUICulture != data.uiCultureInfo) { currentThread.CurrentUICulture = data.uiCultureInfo; } } class HostedAspNetContext : IDisposable { HostedThreadData oldData; public HostedAspNetContext(HostedThreadData newData) { oldData = new HostedThreadData(); HostedThreadData.UnsafeApplyData(newData); } public void Dispose() { HostedThreadData.UnsafeApplyData(oldData); } } } } // 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
- ADConnectionHelper.cs
- Lookup.cs
- XsltSettings.cs
- AbstractSvcMapFileLoader.cs
- Adorner.cs
- Blend.cs
- _NetRes.cs
- DESCryptoServiceProvider.cs
- HttpAsyncResult.cs
- CatalogZoneDesigner.cs
- DataObjectSettingDataEventArgs.cs
- HelpKeywordAttribute.cs
- PropertyEmitter.cs
- SqlDataRecord.cs
- CodeAttributeDeclarationCollection.cs
- SubpageParagraph.cs
- HtmlImage.cs
- AncillaryOps.cs
- InstanceDataCollection.cs
- ControlValuePropertyAttribute.cs
- WebPartAuthorizationEventArgs.cs
- TextParagraph.cs
- BaseParser.cs
- ProviderSettings.cs
- LoginCancelEventArgs.cs
- SqlTypesSchemaImporter.cs
- DesignSurface.cs
- FreezableCollection.cs
- XslTransform.cs
- DebugViewWriter.cs
- Executor.cs
- RenderContext.cs
- MultipleViewProviderWrapper.cs
- RegexFCD.cs
- GridViewPageEventArgs.cs
- ZoneMembershipCondition.cs
- SoapExtensionReflector.cs
- safemediahandle.cs
- ServiceDefaults.cs
- EditBehavior.cs
- EventListener.cs
- SqlLiftIndependentRowExpressions.cs
- NotSupportedException.cs
- PrePrepareMethodAttribute.cs
- StaticDataManager.cs
- PlacementWorkspace.cs
- WebBrowserPermission.cs
- _ChunkParse.cs
- UnsafeNativeMethods.cs
- COM2TypeInfoProcessor.cs
- HttpTransportBindingElement.cs
- Int64Converter.cs
- SelectionBorderGlyph.cs
- ClassHandlersStore.cs
- XmlConvert.cs
- XmlILCommand.cs
- DataGridViewHitTestInfo.cs
- ExceptionNotification.cs
- DirectoryNotFoundException.cs
- SimpleWorkerRequest.cs
- AspCompat.cs
- MultipartContentParser.cs
- ConfigurationLockCollection.cs
- Viewport3DAutomationPeer.cs
- CfgParser.cs
- XmlSchemaValidationException.cs
- HtmlFormParameterWriter.cs
- SmtpLoginAuthenticationModule.cs
- _StreamFramer.cs
- RuleProcessor.cs
- PopupRoot.cs
- DuplicateWaitObjectException.cs
- PresentationSource.cs
- SHA256.cs
- IProvider.cs
- DataGridLinkButton.cs
- DeclarativeCatalogPartDesigner.cs
- ListenerElementsCollection.cs
- AccessedThroughPropertyAttribute.cs
- TimeManager.cs
- ReadOnlyHierarchicalDataSource.cs
- ServiceDescriptions.cs
- Math.cs
- CheckableControlBaseAdapter.cs
- ParallelRangeManager.cs
- WebResourceAttribute.cs
- LineBreak.cs
- DocumentApplication.cs
- DataBindingCollection.cs
- GridViewDeletedEventArgs.cs
- ExternalCalls.cs
- TypeDelegator.cs
- contentDescriptor.cs
- RelationshipManager.cs
- VisualBasicSettings.cs
- XmlSchemaSimpleTypeRestriction.cs
- PlatformNotSupportedException.cs
- StreamReader.cs
- ModelTypeConverter.cs
- BasicViewGenerator.cs