PageRenderTime 38ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/WCFWebApi/src/Microsoft.ApplicationServer.Http/Microsoft/ApplicationServer/Http/Channels/HttpMessageEncodingBindingElement.cs

#
C# | 155 lines | 89 code | 19 blank | 47 comment | 13 complexity | 7d2afa308335d7015ff609fe8ef5ee42 MD5 | raw file
Possible License(s): CC-BY-SA-3.0, Apache-2.0
  1. // <copyright>
  2. // Copyright (c) Microsoft Corporation. All rights reserved.
  3. // </copyright>
  4. namespace Microsoft.ApplicationServer.Http.Channels
  5. {
  6. using System;
  7. using System.Diagnostics.CodeAnalysis;
  8. using System.ServiceModel.Channels;
  9. using Microsoft.Server.Common;
  10. /// <summary>
  11. /// Provides an <see cref="HttpMessageEncoderFactory"/> that returns a <see cref="MessageEncoder"/>
  12. /// that is able to produce and consume <see cref="HttpMessage"/> instances.
  13. /// </summary>
  14. public sealed class HttpMessageEncodingBindingElement : MessageEncodingBindingElement
  15. {
  16. private static readonly Type IReplyChannelType = typeof(IReplyChannel);
  17. private static readonly Type httpMessageEncodingBindingElementType = typeof(HttpMessageEncodingBindingElement);
  18. private static readonly Type channelFactoryType = typeof(IChannelFactory);
  19. /// <summary>
  20. /// Gets or sets the message version that can be handled by the message encoders produced by the message encoder factory.
  21. /// </summary>
  22. /// <returns>The <see cref="MessageVersion"/> used by the encoders produced by the message encoder factory.</returns>
  23. public override MessageVersion MessageVersion
  24. {
  25. get
  26. {
  27. return MessageVersion.None;
  28. }
  29. set
  30. {
  31. if (value == null)
  32. {
  33. throw Fx.Exception.ArgumentNull("value");
  34. }
  35. if (value != MessageVersion.None)
  36. {
  37. throw Fx.Exception.AsError(
  38. new NotSupportedException(
  39. SR.OnlyMessageVersionNoneSupportedOnHttpMessageEncodingBindingElement(
  40. typeof(HttpMessageEncodingBindingElement))));
  41. }
  42. }
  43. }
  44. /// <summary>
  45. /// Returns a value that indicates whether the binding element can build a listener for a specific type of channel.
  46. /// </summary>
  47. /// <typeparam name="TChannel">The type of channel the listener accepts.</typeparam>
  48. /// <param name="context">The <see cref="BindingContext"/> that provides context for the binding element</param>
  49. /// <returns>true if the <see cref="IChannelListener&lt;TChannel&gt;"/> of type <see cref="IChannel"/> can be built by the binding element; otherwise, false.</returns>
  50. [SuppressMessage("Microsoft.Design", "CA1004:GenericMethodsShouldProvideTypeParameter", Justification = "Existing public API")]
  51. public override bool CanBuildChannelFactory<TChannel>(BindingContext context)
  52. {
  53. return false;
  54. }
  55. /// <summary>
  56. /// Returns a value that indicates whether the binding element can build a channel factory for a specific type of channel.
  57. /// </summary>
  58. /// <typeparam name="TChannel">The type of channel the channel factory produces.</typeparam>
  59. /// <param name="context">The <see cref="BindingContext"/> that provides context for the binding element</param>
  60. /// <returns>ALways false.</returns>
  61. [SuppressMessage("Microsoft.Design", "CA1004:GenericMethodsShouldProvideTypeParameter", Justification = "Existing public API")]
  62. public override IChannelFactory<TChannel> BuildChannelFactory<TChannel>(BindingContext context)
  63. {
  64. throw Fx.Exception.AsError(
  65. new NotSupportedException(
  66. SR.ChannelFactoryNotSupported(httpMessageEncodingBindingElementType.Name, channelFactoryType.Name)));
  67. }
  68. /// <summary>
  69. /// Returns a value that indicates whether the binding element can build a channel factory for a specific type of channel.
  70. /// </summary>
  71. /// <typeparam name="TChannel">The type of channel the channel factory produces.</typeparam>
  72. /// <param name="context">The <see cref="BindingContext"/> that provides context for the binding element</param>
  73. /// <returns>ALways false.</returns>
  74. [SuppressMessage("Microsoft.Design", "CA1004:GenericMethodsShouldProvideTypeParameter", Justification = "Existing public API")]
  75. public override bool CanBuildChannelListener<TChannel>(BindingContext context)
  76. {
  77. if (context == null)
  78. {
  79. throw Fx.Exception.ArgumentNull("context");
  80. }
  81. context.BindingParameters.Add(this);
  82. return IsChannelShapeSupported<TChannel>() && context.CanBuildInnerChannelListener<TChannel>();
  83. }
  84. /// <summary>
  85. /// Initializes a channel listener to accept channels of a specified type from the binding context.
  86. /// </summary>
  87. /// <typeparam name="TChannel">The type of channel the listener is built to accept.</typeparam>
  88. /// <param name="context">The <see cref="BindingContext"/> that provides context for the binding element</param>
  89. /// <returns>The <see cref="IChannelListener&lt;TChannel&gt;"/> of type <see cref="IChannel"/> initialized from the context.</returns>
  90. [SuppressMessage("Microsoft.Design", "CA1004:GenericMethodsShouldProvideTypeParameter", Justification = "Existing public API")]
  91. public override IChannelListener<TChannel> BuildChannelListener<TChannel>(BindingContext context)
  92. {
  93. if (context == null)
  94. {
  95. throw Fx.Exception.ArgumentNull("context");
  96. }
  97. if (!IsChannelShapeSupported<TChannel>())
  98. {
  99. throw Fx.Exception.AsError(
  100. new NotSupportedException(
  101. SR.ChannelShapeNotSupported(httpMessageEncodingBindingElementType.Name, typeof(TChannel).Name, typeof(IReplyChannel).Name)));
  102. }
  103. context.BindingParameters.Add(this);
  104. IChannelListener<IReplyChannel> innerListener = context.BuildInnerChannelListener<IReplyChannel>();
  105. if (innerListener == null)
  106. {
  107. return null;
  108. }
  109. return (IChannelListener<TChannel>)new HttpMessageEncodingChannelListener(context.Binding, innerListener);
  110. }
  111. /// <summary>
  112. /// Returns a copy of the binding element object.
  113. /// </summary>
  114. /// <returns>A <see cref="BindingElement"/> object that is a deep clone of the original.</returns>
  115. public override BindingElement Clone()
  116. {
  117. return new HttpMessageEncodingBindingElement();
  118. }
  119. /// <summary>
  120. /// Creates a factory for producing message encoders that are able to
  121. /// produce and consume <see cref="HttpMessage"/> instances.
  122. /// </summary>
  123. /// <returns>
  124. /// The <see cref="MessageEncoderFactory"/> used to produce message encoders that are able to
  125. /// produce and consume <see cref="HttpMessage"/> instances.
  126. /// </returns>
  127. public override MessageEncoderFactory CreateMessageEncoderFactory()
  128. {
  129. return new HttpMessageEncoderFactory();
  130. }
  131. private static bool IsChannelShapeSupported<TChannel>()
  132. {
  133. return typeof(TChannel) == IReplyChannelType;
  134. }
  135. }
  136. }