Code:
/ Net / Net / 3.5.50727.3053 / DEVDIV / depot / DevDiv / releases / Orcas / SP / ndp / fx / src / DataWeb / Server / System / Data / Services / DelegateBodyWriter.cs / 2 / DelegateBodyWriter.cs
//----------------------------------------------------------------------
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
//
// Provides a BodyWriter that invokes a callback on writing.
//
//
// @owner [....]
//---------------------------------------------------------------------
namespace System.Data.Services
{
#region Namespaces.
using System;
using System.Data.Services.Providers;
using System.Diagnostics;
using System.IO;
using System.ServiceModel.Channels;
using System.Xml;
#endregion Namespaces.
/// Use this class to handle writing body contents using a callback.
internal class DelegateBodyWriter : BodyWriter
{
#region Fields.
/// Service to dispose data source from once the response is written.
private readonly IDataService service;
/// Callback.
private readonly Action writerAction;
#endregion Fields.
/// Initializes a new instance.
/// Callback for writing.
/// Service to dispose data source from once the response is written.
internal DelegateBodyWriter(Action writer, IDataService service)
: base(false)
{
Debug.Assert(service != null, "service != null");
Debug.Assert(writer != null, "writer != null");
this.writerAction = writer;
this.service = service;
}
/// Called when the message body is written to an XML file.
///
/// An that is used to write this
/// message body to an XML file.
///
protected override void OnWriteBodyContents(XmlDictionaryWriter writer)
{
Debug.Assert(writer != null, "writer != null");
try
{
writer.WriteStartElement(XmlConstants.WcfBinaryElementName);
using (XmlWriterStream stream = new XmlWriterStream(writer))
{
this.writerAction(stream);
}
writer.WriteEndElement();
}
finally
{
if (this.service != null)
{
this.service.DisposeDataSource();
HttpContextServiceHost host = this.service.Host as HttpContextServiceHost;
if (host != null)
{
if (host.ErrorFound)
{
var ctx = System.ServiceModel.OperationContext.Current;
if (ctx != null)
{
ctx.Channel.Abort();
}
}
}
}
}
}
#region Inner types.
/// Use this class to write to an .
internal class XmlWriterStream : Stream
{
/// Target writer.
private XmlDictionaryWriter innerWriter;
/// Initializes a new instance.
/// Target writer.
internal XmlWriterStream(XmlDictionaryWriter xmlWriter)
{
Debug.Assert(xmlWriter != null, "xmlWriter != null");
this.innerWriter = xmlWriter;
}
/// Gets a value indicating whether the current stream supports reading.
public override bool CanRead
{
get { return false; }
}
/// Gets a value indicating whether the current stream supports seeking.
public override bool CanSeek
{
get { return false; }
}
/// Gets a value indicating whether the current stream supports writing.
public override bool CanWrite
{
get { return true; }
}
/// Gets the length in bytes of the stream.
public override long Length
{
get { throw Error.NotSupported(); }
}
/// Gets or sets the position within the current stream.
public override long Position
{
get { throw Error.NotSupported(); }
set { throw Error.NotSupported(); }
}
///
/// Clears all buffers for this stream and causes any buffered
/// data to be written to the underlying device.
///
public override void Flush()
{
this.innerWriter.Flush();
}
///
/// Reads a sequence of bytes from the current stream and
/// advances the position within the stream by the number of bytes read.
///
///
/// An array of bytes. When this method returns, the buffer contains
/// the specified byte array with the values between
/// and ( + - 1) replaced
/// by the bytes read from the current source.
///
///
/// The zero-based byte offset in at which to
/// begin storing the data read from the current stream.
///
///
/// The maximum number of bytes to be read from the current stream.
///
/// The total number of bytes read into the buffer.
public override int Read(byte[] buffer, int offset, int count)
{
throw Error.NotSupported();
}
/// Sets the position within the current stream.
///
/// A byte offset relative to the parameter.
///
///
/// A value of type indicating the reference
/// point used to obtain the new position.
///
/// The new position within the current stream.
public override long Seek(long offset, SeekOrigin origin)
{
throw Error.NotSupported();
}
/// Sets the length of the current stream.
/// New value for length.
public override void SetLength(long value)
{
throw Error.NotSupported();
}
///
/// Writes a sequence of bytes to the current stream and advances
/// the current position within this stream by the number of
/// bytes written.
///
///
/// An array of bytes. This method copies
/// bytes from to the current stream.
///
///
/// The zero-based byte offset in buffer at which to begin copying
/// bytes to the current stream.
///
///
/// The number of bytes to be written to the current stream.
///
public override void Write(byte[] buffer, int offset, int count)
{
this.innerWriter.WriteBase64(buffer, offset, count);
}
}
#endregion Inner types.
}
}
// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
//----------------------------------------------------------------------
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
//
// Provides a BodyWriter that invokes a callback on writing.
//
//
// @owner [....]
//---------------------------------------------------------------------
namespace System.Data.Services
{
#region Namespaces.
using System;
using System.Data.Services.Providers;
using System.Diagnostics;
using System.IO;
using System.ServiceModel.Channels;
using System.Xml;
#endregion Namespaces.
/// Use this class to handle writing body contents using a callback.
internal class DelegateBodyWriter : BodyWriter
{
#region Fields.
/// Service to dispose data source from once the response is written.
private readonly IDataService service;
/// Callback.
private readonly Action writerAction;
#endregion Fields.
/// Initializes a new instance.
/// Callback for writing.
/// Service to dispose data source from once the response is written.
internal DelegateBodyWriter(Action writer, IDataService service)
: base(false)
{
Debug.Assert(service != null, "service != null");
Debug.Assert(writer != null, "writer != null");
this.writerAction = writer;
this.service = service;
}
/// Called when the message body is written to an XML file.
///
/// An that is used to write this
/// message body to an XML file.
///
protected override void OnWriteBodyContents(XmlDictionaryWriter writer)
{
Debug.Assert(writer != null, "writer != null");
try
{
writer.WriteStartElement(XmlConstants.WcfBinaryElementName);
using (XmlWriterStream stream = new XmlWriterStream(writer))
{
this.writerAction(stream);
}
writer.WriteEndElement();
}
finally
{
if (this.service != null)
{
this.service.DisposeDataSource();
HttpContextServiceHost host = this.service.Host as HttpContextServiceHost;
if (host != null)
{
if (host.ErrorFound)
{
var ctx = System.ServiceModel.OperationContext.Current;
if (ctx != null)
{
ctx.Channel.Abort();
}
}
}
}
}
}
#region Inner types.
/// Use this class to write to an .
internal class XmlWriterStream : Stream
{
/// Target writer.
private XmlDictionaryWriter innerWriter;
/// Initializes a new instance.
/// Target writer.
internal XmlWriterStream(XmlDictionaryWriter xmlWriter)
{
Debug.Assert(xmlWriter != null, "xmlWriter != null");
this.innerWriter = xmlWriter;
}
/// Gets a value indicating whether the current stream supports reading.
public override bool CanRead
{
get { return false; }
}
/// Gets a value indicating whether the current stream supports seeking.
public override bool CanSeek
{
get { return false; }
}
/// Gets a value indicating whether the current stream supports writing.
public override bool CanWrite
{
get { return true; }
}
/// Gets the length in bytes of the stream.
public override long Length
{
get { throw Error.NotSupported(); }
}
/// Gets or sets the position within the current stream.
public override long Position
{
get { throw Error.NotSupported(); }
set { throw Error.NotSupported(); }
}
///
/// Clears all buffers for this stream and causes any buffered
/// data to be written to the underlying device.
///
public override void Flush()
{
this.innerWriter.Flush();
}
///
/// Reads a sequence of bytes from the current stream and
/// advances the position within the stream by the number of bytes read.
///
///
/// An array of bytes. When this method returns, the buffer contains
/// the specified byte array with the values between
/// and ( + - 1) replaced
/// by the bytes read from the current source.
///
///
/// The zero-based byte offset in at which to
/// begin storing the data read from the current stream.
///
///
/// The maximum number of bytes to be read from the current stream.
///
/// The total number of bytes read into the buffer.
public override int Read(byte[] buffer, int offset, int count)
{
throw Error.NotSupported();
}
/// Sets the position within the current stream.
///
/// A byte offset relative to the parameter.
///
///
/// A value of type indicating the reference
/// point used to obtain the new position.
///
/// The new position within the current stream.
public override long Seek(long offset, SeekOrigin origin)
{
throw Error.NotSupported();
}
/// Sets the length of the current stream.
/// New value for length.
public override void SetLength(long value)
{
throw Error.NotSupported();
}
///
/// Writes a sequence of bytes to the current stream and advances
/// the current position within this stream by the number of
/// bytes written.
///
///
/// An array of bytes. This method copies
/// bytes from to the current stream.
///
///
/// The zero-based byte offset in buffer at which to begin copying
/// bytes to the current stream.
///
///
/// The number of bytes to be written to the current stream.
///
public override void Write(byte[] buffer, int offset, int count)
{
this.innerWriter.WriteBase64(buffer, offset, count);
}
}
#endregion Inner types.
}
}
// 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
- backend.cs
- FormViewCommandEventArgs.cs
- TrackingStringDictionary.cs
- SettingsPropertyNotFoundException.cs
- PtsPage.cs
- DtrList.cs
- RawStylusSystemGestureInputReport.cs
- OleStrCAMarshaler.cs
- PageEventArgs.cs
- MenuItem.cs
- EdmItemError.cs
- WebServiceFaultDesigner.cs
- Function.cs
- SchemaSetCompiler.cs
- CompilationUtil.cs
- StateRuntime.cs
- PrinterResolution.cs
- WindowsStatusBar.cs
- SettingsPropertyWrongTypeException.cs
- PathFigureCollectionValueSerializer.cs
- GridViewPageEventArgs.cs
- Selector.cs
- SspiSafeHandles.cs
- UnsafeNativeMethodsPenimc.cs
- PlatformNotSupportedException.cs
- OrderedDictionaryStateHelper.cs
- AsyncResult.cs
- UserValidatedEventArgs.cs
- XmlSchemaAny.cs
- CompressedStack.cs
- WebPartMenuStyle.cs
- XmlSchemaAny.cs
- DataServiceResponse.cs
- ValueUtilsSmi.cs
- AbsoluteQuery.cs
- CoreSwitches.cs
- CapabilitiesAssignment.cs
- TextViewBase.cs
- RelatedPropertyManager.cs
- Symbol.cs
- ManipulationStartedEventArgs.cs
- StoryFragments.cs
- PropertyGroupDescription.cs
- BooleanAnimationBase.cs
- UITypeEditor.cs
- WebControl.cs
- StringAnimationBase.cs
- _HeaderInfoTable.cs
- Root.cs
- Assert.cs
- DbInsertCommandTree.cs
- XsltLibrary.cs
- GeneralTransformCollection.cs
- SearchForVirtualItemEventArgs.cs
- PolicyLevel.cs
- TimeSpanValidatorAttribute.cs
- HtmlInputControl.cs
- Pkcs7Recipient.cs
- ObjectCache.cs
- ResourceLoader.cs
- GifBitmapDecoder.cs
- RightsManagementPermission.cs
- SystemPens.cs
- PointAnimationBase.cs
- TemplateField.cs
- FormsIdentity.cs
- SystemBrushes.cs
- RegexCompilationInfo.cs
- PersonalizableTypeEntry.cs
- IntSecurity.cs
- SqlConnectionManager.cs
- MD5CryptoServiceProvider.cs
- SizeKeyFrameCollection.cs
- TextBoxLine.cs
- InfoCardRSAOAEPKeyExchangeFormatter.cs
- SqlConnectionFactory.cs
- RichTextBox.cs
- SlipBehavior.cs
- Brush.cs
- BinaryReader.cs
- HostProtectionException.cs
- DataControlFieldCollection.cs
- TransformedBitmap.cs
- IgnoreFlushAndCloseStream.cs
- StrokeCollection2.cs
- InputScopeManager.cs
- List.cs
- TypeForwardedToAttribute.cs
- UnmanagedHandle.cs
- WhitespaceRule.cs
- StylusEditingBehavior.cs
- BamlRecordHelper.cs
- ClientTargetSection.cs
- HttpModule.cs
- DLinqTableProvider.cs
- PeerNameRegistration.cs
- WinCategoryAttribute.cs
- InputBindingCollection.cs
- FamilyMapCollection.cs
- ContentControl.cs