Merge pull request #4453 from lambdageek/bug-49721
[mono.git] / mcs / class / System.ServiceModel.Web / System.ServiceModel / WebHttpBinding.cs
index 447f6460d51994648e2df85e4352b2ce46c0f2af..2a71219697dbf668bb63282b8f33978c65c6437a 100644 (file)
 //
 using System;
 using System.ComponentModel;
+using System.Linq;
 using System.ServiceModel.Channels;
 using System.Text;
 using System.Xml;
+#if !MOBILE
+using System.Configuration;
+using System.ServiceModel.Configuration;
+#endif
 
 namespace System.ServiceModel
 {
-       public class WebHttpBinding
-#if NET_2_1
-        : Binding
-#else
-        : Binding, IBindingRuntimePreferences
-#endif
+       public class WebHttpBinding : Binding, IBindingRuntimePreferences
        {
                public WebHttpBinding ()
-                       : this (WebHttpSecurityMode.None)
+                       : this (String.Empty)
+               {
+               }
+
+               public WebHttpBinding (WebHttpSecurityMode securityMode)
+               {
+                       Initialize (securityMode);
+               }
+
+               public WebHttpBinding (string configurationName)
                {
+#if !MOBILE && !XAMMAC_4_5
+                       BindingsSection bindingsSection = ConfigUtil.BindingsSection;
+                       WebHttpBindingElement el = (WebHttpBindingElement) bindingsSection ["webHttpBinding"].ConfiguredBindings.FirstOrDefault (c => c.Name == configurationName);
+                       if (el != null) {
+                               Initialize (el.Security.Mode); // to initialize Transport correctly.
+                               el.ApplyConfiguration (this);
+                       }
+                       else if (!String.IsNullOrEmpty (configurationName))
+                               throw new ConfigurationException (String.Format ("Specified webHttpBinding configuration '{0}' was not found", configurationName));
+                       else
+                               Initialize (WebHttpSecurityMode.None);
+#else
+                       Initialize (WebHttpSecurityMode.None);
+#endif
                }
 
-               public WebHttpBinding (WebHttpSecurityMode mode)
+               void Initialize (WebHttpSecurityMode mode)
                {
                        security.Mode = mode;
                        // MSDN says that this security mode can be set only
@@ -55,40 +78,28 @@ namespace System.ServiceModel
                        t.ManualAddressing = true;
                }
 
-               [MonoTODO]
-               public WebHttpBinding (string configurationName)
-               {
-                       throw new NotImplementedException ();
-               }
-
                WebHttpSecurity security = new WebHttpSecurity ();
                HttpTransportBindingElement t;
                // This can be changed only using <synchronousReceive> configuration element.
-               bool receive_synchronously;
                WebMessageEncodingBindingElement msgenc = new WebMessageEncodingBindingElement ();
 
                public EnvelopeVersion EnvelopeVersion {
                        get { return EnvelopeVersion.None; }
                }
 
-#if !NET_2_1
-#if NET_4_0
+#if !MOBILE && !XAMMAC_4_5
                [DefaultValue (false)]
-#endif
                public bool AllowCookies {
                        get { return t.AllowCookies; }
                        set { t.AllowCookies = value; }
                }
 
-#if NET_4_0
                [DefaultValue (false)]
-#endif
                public bool BypassProxyOnLocal {
                        get { return t.BypassProxyOnLocal; }
                        set { t.BypassProxyOnLocal = value; }
                }
 
-#if NET_4_0
                [MonoTODO]
                public bool CrossDomainScriptAccessEnabled { get; set; }
 
@@ -96,60 +107,45 @@ namespace System.ServiceModel
                        get { return msgenc.ContentTypeMapper; }
                        set { msgenc.ContentTypeMapper = value; }
                }
-#endif
 
-#if NET_4_0
                [DefaultValue (HostNameComparisonMode.StrongWildcard)]
-#endif
                public HostNameComparisonMode HostNameComparisonMode {
                        get { return t.HostNameComparisonMode; }
                        set { t.HostNameComparisonMode = value; }
                }
 
-#if NET_4_0
                [DefaultValue (0x10000)]
-#endif
                public long MaxBufferPoolSize {
                        get { return t.MaxBufferPoolSize; }
                        set { t.MaxBufferPoolSize = value; }
                }
 
-#if NET_4_0
                [DefaultValue (TransferMode.Buffered)]
-#endif
                public TransferMode TransferMode {
                        get { return t.TransferMode; }
                        set { t.TransferMode = value; }
                }
 
-#if NET_4_0
                [DefaultValue (true)]
-#endif
                public bool UseDefaultWebProxy {
                        get { return t.UseDefaultWebProxy; }
                        set { t.UseDefaultWebProxy = value; }
                }
 
-#if NET_4_0
                [DefaultValue (null)]
-#endif
                public Uri ProxyAddress {
                        get { return t.ProxyAddress; }
                        set { t.ProxyAddress = value; }
                }
 #endif
 
-#if NET_4_0
                [DefaultValue (0x80000)]
-#endif
                public int MaxBufferSize {
                        get { return t.MaxBufferSize; }
                        set { t.MaxBufferSize = value; }
                }
 
-#if NET_4_0
                [DefaultValue (0x10000)]
-#endif
                public long MaxReceivedMessageSize {
                        get { return t.MaxReceivedMessageSize; }
                        set { t.MaxReceivedMessageSize = value; }
@@ -161,18 +157,16 @@ namespace System.ServiceModel
                }
 
                public override string Scheme {
-                       get { return Security.Mode != WebHttpSecurityMode.None ? Uri.UriSchemeHttps : Uri.UriSchemeHttp; }
+                       get { return Security.Mode == WebHttpSecurityMode.Transport ? Uri.UriSchemeHttps : Uri.UriSchemeHttp; }
                }
 
                public WebHttpSecurity Security {
                        get { return security; }
-#if NET_4_0
                        set {
                                if (value == null)
                                        throw new ArgumentNullException ("value");
                                security = value;
                        }
-#endif
                }
 
                public Encoding WriteEncoding {
@@ -189,13 +183,10 @@ namespace System.ServiceModel
                        return new BindingElementCollection (new BindingElement [] { msgenc, t.Clone () });
                }
 
-#if !NET_2_1
                bool IBindingRuntimePreferences.ReceiveSynchronously {
-                       get { return receive_synchronously; }
+                       get { return false; }
                }
-#endif
 
-#if NET_4_0
                [EditorBrowsable (EditorBrowsableState.Advanced)]
                public bool ShouldSerializeReaderQuotas ()
                {
@@ -213,6 +204,5 @@ namespace System.ServiceModel
                {
                        return false;
                }
-#endif
        }
 }