Code:
/ 4.0 / 4.0 / DEVDIV_TFS / Dev10 / Releases / RTMRel / ndp / fx / src / Services / Messaging / System / Messaging / BinaryMessageFormatter.cs / 1305376 / BinaryMessageFormatter.cs
//------------------------------------------------------------------------------
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
//-----------------------------------------------------------------------------
namespace System.Messaging {
using System;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters;
using System.Runtime.Serialization.Formatters.Binary;
using System.Diagnostics;
using System.IO;
using System.ComponentModel;
///
///
/// Formatter class that serializes and deserializes objects into
/// and from MessageQueue messages using binary format.
///
public class BinaryMessageFormatter : IMessageFormatter {
private BinaryFormatter formatter;
internal const short VT_BINARY_OBJECT = 0x300;
///
///
/// Creates a new Binary message formatter object.
///
public BinaryMessageFormatter() {
this.formatter = new BinaryFormatter();
}
///
///
/// Creates a new Binary message formatter object
/// with the given properties.
///
public BinaryMessageFormatter(FormatterAssemblyStyle topObjectFormat, FormatterTypeStyle typeFormat) {
this.formatter = new BinaryFormatter();
this.formatter.AssemblyFormat = topObjectFormat;
this.formatter.TypeFormat = typeFormat;
}
///
///
/// Determines how the top (root) object of a graph
/// is laid out in the serialized stream.
///
[MessagingDescription(Res.MsgTopObjectFormat), DefaultValueAttribute(FormatterAssemblyStyle.Simple)]
public FormatterAssemblyStyle TopObjectFormat {
get {
return this.formatter.AssemblyFormat;
}
set {
this.formatter.AssemblyFormat = value;
}
}
///
///
/// Determines how type descriptions are laid out in the
/// serialized stream.
///
[MessagingDescription(Res.MsgTypeFormat), DefaultValueAttribute(FormatterTypeStyle.TypesWhenNeeded)]
public FormatterTypeStyle TypeFormat {
get {
return this.formatter.TypeFormat;
}
set {
this.formatter.TypeFormat = value;
}
}
///
///
/// When this method is called, the formatter will attempt to determine
/// if the contents of the message are something the formatter can deal with.
///
public bool CanRead(Message message) {
if (message == null)
throw new ArgumentNullException("message");
int variantType = message.BodyType;
if (variantType != VT_BINARY_OBJECT)
return false;
return true;
}
///
///
/// This method is needed to improve scalability on Receive and ReceiveAsync scenarios. Not requiring
/// thread safety on read and write.
///
public object Clone() {
return new BinaryMessageFormatter(TopObjectFormat, TypeFormat);
}
///
///
/// This method is used to read the contents from the given message
/// and create an object.
///
public object Read(Message message) {
if (message == null)
throw new ArgumentNullException("message");
int variantType = message.BodyType;
if (variantType == VT_BINARY_OBJECT) {
Stream stream = message.BodyStream;
return formatter.Deserialize(stream);
}
throw new InvalidOperationException(Res.GetString(Res.InvalidTypeDeserialization));
}
///
///
/// This method is used to write the given object into the given message.
/// If the formatter cannot understand the given object, an exception is thrown.
///
public void Write(Message message, object obj) {
if (message == null)
throw new ArgumentNullException("message");
Stream stream = new MemoryStream();
formatter.Serialize(stream, obj);
message.BodyType = VT_BINARY_OBJECT;
message.BodyStream = stream;
}
}
}
// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
//------------------------------------------------------------------------------
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
//-----------------------------------------------------------------------------
namespace System.Messaging {
using System;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters;
using System.Runtime.Serialization.Formatters.Binary;
using System.Diagnostics;
using System.IO;
using System.ComponentModel;
///
///
/// Formatter class that serializes and deserializes objects into
/// and from MessageQueue messages using binary format.
///
public class BinaryMessageFormatter : IMessageFormatter {
private BinaryFormatter formatter;
internal const short VT_BINARY_OBJECT = 0x300;
///
///
/// Creates a new Binary message formatter object.
///
public BinaryMessageFormatter() {
this.formatter = new BinaryFormatter();
}
///
///
/// Creates a new Binary message formatter object
/// with the given properties.
///
public BinaryMessageFormatter(FormatterAssemblyStyle topObjectFormat, FormatterTypeStyle typeFormat) {
this.formatter = new BinaryFormatter();
this.formatter.AssemblyFormat = topObjectFormat;
this.formatter.TypeFormat = typeFormat;
}
///
///
/// Determines how the top (root) object of a graph
/// is laid out in the serialized stream.
///
[MessagingDescription(Res.MsgTopObjectFormat), DefaultValueAttribute(FormatterAssemblyStyle.Simple)]
public FormatterAssemblyStyle TopObjectFormat {
get {
return this.formatter.AssemblyFormat;
}
set {
this.formatter.AssemblyFormat = value;
}
}
///
///
/// Determines how type descriptions are laid out in the
/// serialized stream.
///
[MessagingDescription(Res.MsgTypeFormat), DefaultValueAttribute(FormatterTypeStyle.TypesWhenNeeded)]
public FormatterTypeStyle TypeFormat {
get {
return this.formatter.TypeFormat;
}
set {
this.formatter.TypeFormat = value;
}
}
///
///
/// When this method is called, the formatter will attempt to determine
/// if the contents of the message are something the formatter can deal with.
///
public bool CanRead(Message message) {
if (message == null)
throw new ArgumentNullException("message");
int variantType = message.BodyType;
if (variantType != VT_BINARY_OBJECT)
return false;
return true;
}
///
///
/// This method is needed to improve scalability on Receive and ReceiveAsync scenarios. Not requiring
/// thread safety on read and write.
///
public object Clone() {
return new BinaryMessageFormatter(TopObjectFormat, TypeFormat);
}
///
///
/// This method is used to read the contents from the given message
/// and create an object.
///
public object Read(Message message) {
if (message == null)
throw new ArgumentNullException("message");
int variantType = message.BodyType;
if (variantType == VT_BINARY_OBJECT) {
Stream stream = message.BodyStream;
return formatter.Deserialize(stream);
}
throw new InvalidOperationException(Res.GetString(Res.InvalidTypeDeserialization));
}
///
///
/// This method is used to write the given object into the given message.
/// If the formatter cannot understand the given object, an exception is thrown.
///
public void Write(Message message, object obj) {
if (message == null)
throw new ArgumentNullException("message");
Stream stream = new MemoryStream();
formatter.Serialize(stream, obj);
message.BodyType = VT_BINARY_OBJECT;
message.BodyStream = stream;
}
}
}
// 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
- ListBox.cs
- Int16Converter.cs
- Utility.cs
- DeleteStoreRequest.cs
- StandardMenuStripVerb.cs
- XmlCharacterData.cs
- Duration.cs
- XPathAxisIterator.cs
- CodePageEncoding.cs
- AuthenticationSection.cs
- StylusTip.cs
- CreateSequenceResponse.cs
- UIElement.cs
- Memoizer.cs
- ToolStripItemCollection.cs
- DataViewManagerListItemTypeDescriptor.cs
- DataQuery.cs
- ParameterModifier.cs
- CollectionViewGroup.cs
- TextParaLineResult.cs
- GeometryCombineModeValidation.cs
- SqlConnectionString.cs
- SecurityTokenResolver.cs
- TransformerInfoCollection.cs
- HashAlgorithm.cs
- Timer.cs
- SymbolMethod.cs
- RegularExpressionValidator.cs
- SliderAutomationPeer.cs
- wgx_exports.cs
- LambdaCompiler.cs
- panel.cs
- CommandHelpers.cs
- safemediahandle.cs
- WCFServiceClientProxyGenerator.cs
- EntityPropertyMappingAttribute.cs
- ExpressionConverter.cs
- ProfileManager.cs
- WinEventWrap.cs
- DataRowCollection.cs
- StatusBar.cs
- WebBaseEventKeyComparer.cs
- LinqDataSourceHelper.cs
- StyleHelper.cs
- RangeEnumerable.cs
- ItemTypeToolStripMenuItem.cs
- MultipartContentParser.cs
- Registry.cs
- StylusCollection.cs
- SQLMoneyStorage.cs
- ResourceDescriptionAttribute.cs
- CollectionChangedEventManager.cs
- DelegateSerializationHolder.cs
- ExceptionUtility.cs
- SqlDataSourceAdvancedOptionsForm.cs
- CounterSampleCalculator.cs
- FlowchartStart.xaml.cs
- ReliabilityContractAttribute.cs
- CryptoConfig.cs
- PathStreamGeometryContext.cs
- EventSetterHandlerConverter.cs
- WinInetCache.cs
- ExtenderProvidedPropertyAttribute.cs
- Translator.cs
- SoapFormatterSinks.cs
- WizardSideBarListControlItem.cs
- XmlLangPropertyAttribute.cs
- PkcsMisc.cs
- DesignerSerializerAttribute.cs
- SspiNegotiationTokenProviderState.cs
- WebPartsPersonalizationAuthorization.cs
- ToolStripKeyboardHandlingService.cs
- ComponentChangedEvent.cs
- HtmlFormWrapper.cs
- AutoResetEvent.cs
- ManipulationDeltaEventArgs.cs
- ValidatorUtils.cs
- StreamAsIStream.cs
- ManagedFilter.cs
- TextBox.cs
- NullRuntimeConfig.cs
- LazyTextWriterCreator.cs
- Number.cs
- SettingsPropertyNotFoundException.cs
- XmlHelper.cs
- EventMappingSettings.cs
- BinHexDecoder.cs
- Stylesheet.cs
- hresults.cs
- CompiledIdentityConstraint.cs
- ChangeInterceptorAttribute.cs
- DesignerActionList.cs
- EpmHelper.cs
- AsyncCodeActivity.cs
- AppManager.cs
- InheritedPropertyChangedEventArgs.cs
- ReflectTypeDescriptionProvider.cs
- Propagator.cs
- EtwTrace.cs
- InlineUIContainer.cs