namespace Pelebyte.ServiceModel.Generic { using System.IO; using System.ServiceModel.Channels; using System.Xml; /// /// Allows an arbitrary to be serialized. /// Use in conjunction with the overloads of the /// .CreateMessage method. /// public sealed class BodyStreamProviderWriter : BodyWriter, IStreamProvider { private readonly Stream innerStream; public BodyStreamProviderWriter(Stream stream) : base(false) { this.innerStream = stream; } protected override void OnWriteBodyContents(XmlDictionaryWriter writer) { writer.WriteStartElement("Binary"); writer.WriteValue((IStreamProvider)this); writer.WriteEndElement(); } Stream IStreamProvider.GetStream() { return this.innerStream; } void IStreamProvider.ReleaseStream(Stream stream) { if (stream != null) { stream.Dispose(); } } } }