// Copyright (c) Microsoft Corporation. All rights reserved. See License.txt in the project root for license information. using System.Diagnostics.CodeAnalysis; using System.ServiceModel.Channels; using System.Web.Http.SelfHost.Properties; namespace System.Web.Http.SelfHost.Channels { /// /// Provides an that returns a /// that is able to produce and consume instances. /// internal sealed class HttpMessageEncodingBindingElement : MessageEncodingBindingElement { private static readonly Type _replyChannelType = typeof(IReplyChannel); /// /// Gets or sets the message version that can be handled by the message encoders produced by the message encoder factory. /// /// The used by the encoders produced by the message encoder factory. public override MessageVersion MessageVersion { get { return MessageVersion.None; } set { if (value == null) { throw Error.ArgumentNull("value"); } if (value != MessageVersion.None) { throw Error.NotSupported(SRResources.OnlyMessageVersionNoneSupportedOnHttpMessageEncodingBindingElement, typeof(HttpMessageEncodingBindingElement).Name); } } } /// /// Returns a value that indicates whether the binding element can build a listener for a specific type of channel. /// /// The type of channel the listener accepts. /// The that provides context for the binding element /// true if the of type can be built by the binding element; otherwise, false. [SuppressMessage("Microsoft.Design", "CA1004:GenericMethodsShouldProvideTypeParameter", Justification = "Existing public API")] public override bool CanBuildChannelFactory(BindingContext context) { return false; } /// /// Returns a value that indicates whether the binding element can build a channel factory for a specific type of channel. /// /// The type of channel the channel factory produces. /// The that provides context for the binding element /// ALways false. [SuppressMessage("Microsoft.Design", "CA1004:GenericMethodsShouldProvideTypeParameter", Justification = "Existing public API")] public override IChannelFactory BuildChannelFactory(BindingContext context) { throw Error.NotSupported(SRResources.ChannelFactoryNotSupported, typeof(HttpMessageEncodingBindingElement).Name, typeof(IChannelFactory).Name); } /// /// Returns a value that indicates whether the binding element can build a channel factory for a specific type of channel. /// /// The type of channel the channel factory produces. /// The that provides context for the binding element /// ALways false. [SuppressMessage("Microsoft.Design", "CA1004:GenericMethodsShouldProvideTypeParameter", Justification = "Existing public API")] public override bool CanBuildChannelListener(BindingContext context) { if (context == null) { throw Error.ArgumentNull("context"); } context.BindingParameters.Add(this); return IsChannelShapeSupported() && context.CanBuildInnerChannelListener(); } /// /// Initializes a channel listener to accept channels of a specified type from the binding context. /// /// The type of channel the listener is built to accept. /// The that provides context for the binding element /// The of type initialized from the context. [SuppressMessage("Microsoft.Design", "CA1004:GenericMethodsShouldProvideTypeParameter", Justification = "Existing public API")] public override IChannelListener BuildChannelListener(BindingContext context) { if (context == null) { throw Error.ArgumentNull("context"); } if (!IsChannelShapeSupported()) { throw Error.NotSupported(SRResources.ChannelShapeNotSupported, typeof(HttpMessageEncodingBindingElement).Name, typeof(IReplyChannel).Name); } context.BindingParameters.Add(this); IChannelListener innerListener = context.BuildInnerChannelListener(); if (innerListener == null) { return null; } return (IChannelListener)new HttpMessageEncodingChannelListener(context.Binding, innerListener); } /// /// Returns a copy of the binding element object. /// /// A object that is a deep clone of the original. public override BindingElement Clone() { return new HttpMessageEncodingBindingElement(); } /// /// Creates a factory for producing message encoders that are able to /// produce and consume instances. /// /// /// The used to produce message encoders that are able to /// produce and consume instances. /// public override MessageEncoderFactory CreateMessageEncoderFactory() { return new HttpMessageEncoderFactory(); } private static bool IsChannelShapeSupported() { return typeof(TChannel) == _replyChannelType; } } }