From 852b83a4c693196cd73b67e1ed4200ba8dab09ee Mon Sep 17 00:00:00 2001 From: bbaia Date: Tue, 5 Aug 2008 13:16:21 +0000 Subject: [PATCH] WebServiceExporter should generate WebServiceBinding attribute with WSI basic profile 1.1 by default. [SPRNET-869] --- .../Web/Services/WebServiceExporter.cs | 76 ++++++++++++++++++- .../Web/Services/WebServiceExporterTests.cs | 54 +++++++++++++ 2 files changed, 126 insertions(+), 4 deletions(-) diff --git a/src/Spring/Spring.Web/Web/Services/WebServiceExporter.cs b/src/Spring/Spring.Web/Web/Services/WebServiceExporter.cs index e7ccb027..ed433050 100644 --- a/src/Spring/Spring.Web/Web/Services/WebServiceExporter.cs +++ b/src/Spring/Spring.Web/Web/Services/WebServiceExporter.cs @@ -57,10 +57,14 @@ namespace Spring.Web.Services ///

/// /// Aleksandar Seovic + [WebServiceBinding(ConformsTo=WsiProfiles.BasicProfile1_1)] public class WebServiceExporter : IInitializingObject, IObjectFactoryAware, IFactoryObject, IObjectNameAware { #region Fields +#if NET_2_0 + private WsiProfiles _wsiProfile = WsiProfiles.BasicProfile1_1; +#endif private Type _webServiceBaseType = typeof(WebService); private string _targetName; private string _description; @@ -100,6 +104,21 @@ namespace Spring.Web.Services #region Properties +#if NET_2_0 + /// + /// Gets or Sets the Web Services Interoperability (WSI) specification + /// to which the Web Service claims to conform. + /// + /// + /// Default is + /// + public WsiProfiles WsiProfile + { + get { return _wsiProfile; } + set { _wsiProfile = value; } + } +#endif + /// /// Gets or sets the base type that web service should inherit. /// @@ -346,7 +365,11 @@ namespace Spring.Web.Services /// protected virtual void GenerateProxy() { +#if NET_2_0 + IProxyTypeBuilder builder = new WebServiceProxyTypeBuilder(TargetName, Description, Name, Namespace, WsiProfile); +#else IProxyTypeBuilder builder = new WebServiceProxyTypeBuilder(TargetName, Description, Name, Namespace); +#endif builder.Name = WebUtils.GetPageName(objectName); builder.BaseType = WebServiceBaseType; builder.TargetType = objectFactory.GetType(TargetName); @@ -376,6 +399,9 @@ namespace Spring.Web.Services private string targetName; private CustomAttributeBuilder webServiceAttribute; +#if NET_2_0 + private CustomAttributeBuilder webServiceBindingAttribute; +#endif #endregion @@ -389,6 +415,30 @@ namespace Spring.Web.Services // Creates a WebServiceAttribute from configuration info this.webServiceAttribute = CreateWebServiceAttribute(description, name, ns); } +#if NET_2_0 + public WebServiceProxyTypeBuilder( + string targetName, string description, string name, string ns, WsiProfiles wsiProfile) + { + this.targetName = targetName; + + // Creates a WebServiceAttribute from configuration info + this.webServiceAttribute = CreateWebServiceAttribute(description, name, ns); + + // Creates a WebServiceAttribute from configuration info + this.webServiceBindingAttribute = CreateWebServiceBindingAttribute(wsiProfile); + } + + private static CustomAttributeBuilder CreateWebServiceBindingAttribute(WsiProfiles wsiProfile) + { + ReflectionUtils.CustomAttributeBuilderBuilder cabb = + new ReflectionUtils.CustomAttributeBuilderBuilder(typeof(WebServiceBindingAttribute)); + if (wsiProfile == WsiProfiles.BasicProfile1_1) + { + cabb.AddPropertyValue("ConformsTo", wsiProfile); + } + return cabb.Build(); + } +#endif private static CustomAttributeBuilder CreateWebServiceAttribute(string description, string name, string ns) { @@ -449,19 +499,37 @@ namespace Spring.Web.Services { IList attrs = base.GetTypeAttributes(type); + bool containsWebServiceAttribute = false; + bool containsWebServiceBindingAttribute = false; for (int i = 0; i < attrs.Count; i++) { if (IsAttributeMatchingType(attrs[i], typeof(WebServiceAttribute))) { // override existing WebServiceAttribute + containsWebServiceAttribute = true; attrs[i] = webServiceAttribute; - - return attrs; + } +#if NET_2_0 + else if (IsAttributeMatchingType(attrs[i], typeof(WebServiceBindingAttribute))) + { + containsWebServiceBindingAttribute = true; } +#endif } - // add missing WebServiceAttribute - attrs.Add(webServiceAttribute); + // Add missing WebServiceAttribute + if (!containsWebServiceAttribute) + { + attrs.Add(webServiceAttribute); + } + +#if NET_2_0 + // Add missing WebServiceBindingAttribute + if (!containsWebServiceBindingAttribute) + { + attrs.Add(webServiceBindingAttribute); + } +#endif return attrs; } diff --git a/test/Spring/Spring.Web.Tests/Web/Services/WebServiceExporterTests.cs b/test/Spring/Spring.Web.Tests/Web/Services/WebServiceExporterTests.cs index 4d88532f..c156288c 100644 --- a/test/Spring/Spring.Web.Tests/Web/Services/WebServiceExporterTests.cs +++ b/test/Spring/Spring.Web.Tests/Web/Services/WebServiceExporterTests.cs @@ -121,6 +121,57 @@ namespace Spring.Web.Services Assert.AreEqual(wse.Namespace, wsa.Namespace); } +#if NET_2_0 + [Test] + public void CreatesDefaultWebServiceBindingAttribute() + { + wse.ObjectName = "CreatesDefaultWebServiceBindingAttribute"; + wse.TargetName = "noDecoratedService"; + wse.AfterPropertiesSet(); + + Type proxyType = wse.ObjectType; + object[] attrs = proxyType.GetCustomAttributes(typeof(WebServiceBindingAttribute), true); + Assert.IsNotEmpty(attrs); + Assert.AreEqual(1, attrs.Length); + + WebServiceBindingAttribute wsba = attrs[0] as WebServiceBindingAttribute; + Assert.AreEqual(WsiProfiles.BasicProfile1_1, wsba.ConformsTo); + } + + [Test] + public void CreatesWebServiceBindingAttributeWithNoWsiProfile() + { + wse.ObjectName = "CreatesWebServiceBindingAttributeWithNoWsiProfile"; + wse.TargetName = "noDecoratedService"; + wse.WsiProfile = WsiProfiles.None; + wse.AfterPropertiesSet(); + + Type proxyType = wse.ObjectType; + object[] attrs = proxyType.GetCustomAttributes(typeof(WebServiceBindingAttribute), true); + Assert.IsNotEmpty(attrs); + Assert.AreEqual(1, attrs.Length); + + WebServiceBindingAttribute wsba = attrs[0] as WebServiceBindingAttribute; + Assert.AreEqual(WsiProfiles.None, wsba.ConformsTo); + } + + [Test] + public void UsesExistingWebServiceBindingAttributeWithDecoratedClass() + { + wse.ObjectName = "UsesExistingWebServiceBindingAttributeWithDecoratedClass"; + wse.TargetName = "decoratedService"; + wse.AfterPropertiesSet(); + + Type proxyType = wse.ObjectType; + object[] attrs = proxyType.GetCustomAttributes(typeof(WebServiceBindingAttribute), true); + Assert.IsNotEmpty(attrs); + Assert.AreEqual(1, attrs.Length); + + WebServiceBindingAttribute wsba = attrs[0] as WebServiceBindingAttribute; + Assert.AreEqual(WsiProfiles.None, wsba.ConformsTo); + } +#endif + [Test] public void CreatesDefaultWebMethodAttributeWithNoDecoratedMethod() { @@ -264,6 +315,9 @@ namespace Spring.Web.Services } [WebService(Name = "Decorated service")] +#if NET_2_0 + [WebServiceBinding(ConformsTo=WsiProfiles.None)] +#endif public class DecoratedService : IService { [WebMethod(Description="SomeMethod description")]