Code:
/ 4.0 / 4.0 / untmp / DEVDIV_TFS / Dev10 / Releases / RTMRel / ndp / cdf / src / NetFx35 / System.ServiceModel.Web / System / ServiceModel / Channels / HttpStreamMessage.cs / 1305376 / HttpStreamMessage.cs
//----------------------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
//---------------------------------------------------------------------------
#pragma warning disable 1634 // Stops compiler from warning about unknown warnings (for Presharp)
namespace System.ServiceModel.Channels
{
using System;
using System.IO;
using System.Runtime;
using System.ServiceModel;
using System.Xml;
using DiagnosticUtility = System.ServiceModel.DiagnosticUtility;
class HttpStreamMessage : Message
{
internal const string StreamElementName = "Binary";
BodyWriter bodyWriter;
MessageHeaders headers;
MessageProperties properties;
public HttpStreamMessage(Stream stream)
{
if (stream == null)
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("stream");
}
this.bodyWriter = new HttpStreamBodyWriter(stream);
this.headers = new MessageHeaders(MessageVersion.None, 1);
this.properties = new MessageProperties();
}
public HttpStreamMessage(BodyWriter writer)
{
if (writer == null)
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("writer");
}
this.bodyWriter = writer;
this.headers = new MessageHeaders(MessageVersion.None, 1);
this.properties = new MessageProperties();
}
public HttpStreamMessage(MessageHeaders headers, MessageProperties properties, BodyWriter bodyWriter)
{
if (bodyWriter == null)
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("bodyWriter");
}
this.headers = new MessageHeaders(headers);
this.properties = new MessageProperties(properties);
this.bodyWriter = bodyWriter;
}
public override MessageHeaders Headers
{
get
{
if (IsDisposed)
{
#pragma warning suppress 56503
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(CreateDisposedException());
}
return headers;
}
}
public override bool IsEmpty
{
get
{
return false;
}
}
public override bool IsFault
{
get { return false; }
}
public override MessageProperties Properties
{
get
{
if (IsDisposed)
{
#pragma warning suppress 56503
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(CreateDisposedException());
}
return properties;
}
}
public override MessageVersion Version
{
get
{
if (IsDisposed)
{
#pragma warning suppress 56503
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(CreateDisposedException());
}
return MessageVersion.None;
}
}
protected override void OnBodyToString(XmlDictionaryWriter writer)
{
if (this.bodyWriter.IsBuffered)
{
bodyWriter.WriteBodyContents(writer);
}
else
{
writer.WriteString(SR2.GetString(SR2.MessageBodyIsStream));
}
}
protected override void OnClose()
{
Exception ex = null;
try
{
base.OnClose();
}
catch (Exception e)
{
if (Fx.IsFatal(e))
{
throw;
}
ex = e;
}
try
{
if (properties != null)
{
properties.Dispose();
}
}
catch (Exception e)
{
if (Fx.IsFatal(e))
{
throw;
}
if (ex == null)
{
ex = e;
}
}
if (ex != null)
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(ex);
}
this.bodyWriter = null;
}
protected override MessageBuffer OnCreateBufferedCopy(int maxBufferSize)
{
BodyWriter bufferedBodyWriter;
if (this.bodyWriter.IsBuffered)
{
bufferedBodyWriter = this.bodyWriter;
}
else
{
bufferedBodyWriter = this.bodyWriter.CreateBufferedCopy(maxBufferSize);
}
return new HttpStreamMessageBuffer(this.Headers, new MessageProperties(this.Properties), bufferedBodyWriter);
}
protected override void OnWriteBodyContents(XmlDictionaryWriter writer)
{
this.bodyWriter.WriteBodyContents(writer);
}
Exception CreateDisposedException()
{
return new ObjectDisposedException("", SR2.GetString(SR2.MessageClosed));
}
class HttpStreamBodyWriter : BodyWriter
{
Stream stream;
Object thisLock;
public HttpStreamBodyWriter(Stream stream)
: base(false)
{
if (stream == null)
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("stream");
}
this.stream = stream;
thisLock = new Object();
}
object ThisLock
{
get { return thisLock; }
}
protected override BodyWriter OnCreateBufferedCopy(int maxBufferSize)
{
using (BufferManagerOutputStream bufferedStream = new BufferManagerOutputStream(SR2.MaxReceivedMessageSizeExceeded, maxBufferSize))
{
using (HttpStreamXmlDictionaryWriter writer = new HttpStreamXmlDictionaryWriter(bufferedStream))
{
OnWriteBodyContents(writer);
writer.Flush();
int size;
byte[] bytesArray = bufferedStream.ToArray(out size);
return new BufferedBytesBodyWriter(bytesArray, size);
}
}
}
protected override void OnWriteBodyContents(XmlDictionaryWriter writer)
{
lock (ThisLock)
{
writer.WriteStartElement(StreamElementName, string.Empty);
writer.WriteValue(new HttpStreamProvider(this.stream));
writer.WriteEndElement();
}
}
class HttpStreamProvider : IStreamProvider
{
Stream stream;
internal HttpStreamProvider(Stream stream)
{
this.stream = stream;
}
public Stream GetStream()
{
return stream;
}
public void ReleaseStream(Stream stream)
{
//Noop
}
}
}
class BufferedBytesBodyWriter : BodyWriter
{
byte[] array;
int size;
public BufferedBytesBodyWriter(byte[] array, int size)
: base(true)
{
this.array = array;
this.size = size;
}
protected override void OnWriteBodyContents(XmlDictionaryWriter writer)
{
writer.WriteStartElement(StreamElementName, string.Empty);
writer.WriteBase64(this.array, 0, size);
writer.WriteEndElement();
}
}
class HttpStreamMessageBuffer : MessageBuffer
{
BodyWriter bodyWriter;
bool closed;
MessageHeaders headers;
MessageProperties properties;
object thisLock = new object();
public HttpStreamMessageBuffer(MessageHeaders headers,
MessageProperties properties, BodyWriter bodyWriter)
: base()
{
this.bodyWriter = bodyWriter;
this.headers = headers;
this.properties = properties;
}
public override int BufferSize
{
get { return 0; }
}
object ThisLock
{
get { return thisLock; }
}
public override void Close()
{
lock (ThisLock)
{
if (!closed)
{
closed = true;
bodyWriter = null;
headers = null;
properties = null;
}
}
}
public override Message CreateMessage()
{
lock (ThisLock)
{
if (closed)
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(CreateDisposedException());
}
return new HttpStreamMessage(this.headers, this.properties, this.bodyWriter);
}
}
Exception CreateDisposedException()
{
return new ObjectDisposedException("", SR2.GetString(SR2.MessageBufferIsClosed));
}
}
}
}
// 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
- TimeSpanStorage.cs
- TextAdaptor.cs
- InteropAutomationProvider.cs
- UpdateCompiler.cs
- SurrogateDataContract.cs
- DetailsView.cs
- XmlSchemaSimpleContentRestriction.cs
- BuiltInExpr.cs
- DeclaredTypeElement.cs
- InlinedAggregationOperatorEnumerator.cs
- EdmEntityTypeAttribute.cs
- OverloadGroupAttribute.cs
- DbExpressionVisitor_TResultType.cs
- ThrowHelper.cs
- CodeIdentifiers.cs
- StreamUpdate.cs
- XmlSchemaInferenceException.cs
- WorkflowShape.cs
- COM2FontConverter.cs
- InvokeHandlers.cs
- ApplyTemplatesAction.cs
- DateBoldEvent.cs
- MemoryFailPoint.cs
- TimeoutConverter.cs
- DateTimeUtil.cs
- UndirectedGraph.cs
- DetailsViewInsertEventArgs.cs
- TypeToken.cs
- HttpListenerElement.cs
- BeginGetFileNameFromUserRequest.cs
- DataGridItemEventArgs.cs
- QueuePathDialog.cs
- LineMetrics.cs
- DiscriminatorMap.cs
- dbdatarecord.cs
- DbProviderFactory.cs
- HebrewNumber.cs
- OdbcConnectionStringbuilder.cs
- mansign.cs
- ipaddressinformationcollection.cs
- DefaultHttpHandler.cs
- TiffBitmapEncoder.cs
- FilterUserControlBase.cs
- GridViewDeletedEventArgs.cs
- HttpResponseWrapper.cs
- mactripleDES.cs
- IIS7WorkerRequest.cs
- ImageCodecInfo.cs
- DiscardableAttribute.cs
- Visual.cs
- SharedStatics.cs
- CodeObject.cs
- XmlWriter.cs
- WinEventHandler.cs
- RichTextBoxAutomationPeer.cs
- Light.cs
- ProgressiveCrcCalculatingStream.cs
- DataControlFieldHeaderCell.cs
- XD.cs
- InstanceDataCollectionCollection.cs
- FontSource.cs
- CorePropertiesFilter.cs
- OdbcPermission.cs
- GenerateTemporaryAssemblyTask.cs
- XPathSingletonIterator.cs
- ThreadStaticAttribute.cs
- VisualCollection.cs
- HttpContext.cs
- IriParsingElement.cs
- SynchronizationContext.cs
- InputElement.cs
- ColumnMapVisitor.cs
- ActivityDesignerHelper.cs
- HttpResponse.cs
- StringWriter.cs
- WebBrowserUriTypeConverter.cs
- DesignerVerb.cs
- EntityDescriptor.cs
- KeyEvent.cs
- Logging.cs
- ScriptingProfileServiceSection.cs
- Stacktrace.cs
- AsyncResult.cs
- AudioSignalProblemOccurredEventArgs.cs
- x509utils.cs
- UnsafeNativeMethodsCLR.cs
- UseAttributeSetsAction.cs
- AutoGeneratedFieldProperties.cs
- DesignerActionItemCollection.cs
- DataSourceComponent.cs
- DynamicMethod.cs
- ComponentResourceKey.cs
- DPAPIProtectedConfigurationProvider.cs
- NavigationPropertySingletonExpression.cs
- BuildProvider.cs
- DoubleSumAggregationOperator.cs
- DeclarationUpdate.cs
- EpmContentSerializer.cs
- Module.cs
- WindowsFormsHostAutomationPeer.cs