SPRNET-1360

Improve exception messages in the case where the DynamicProxyManager attempts to register a Proxy using an already-registered name but pointing to a DIFFERENT type (this is usually a configuration error associated with attempting to export two or more services from two or more config sources that accidentally share a single common name)
This commit is contained in:
sbohlen
2010-08-23 17:15:43 +00:00
parent 63f81b6749
commit f9ec394370
3 changed files with 46 additions and 13 deletions

View File

@@ -30,9 +30,9 @@ using Spring.Util;
namespace Spring.Proxy
{
/// <summary>
/// <summary>
/// Allows easy access to existing and creation of new dynamic proxies.
/// </summary>
/// </summary>
/// <author>Aleksandar Seovic</author>
/// <author>Bruno Baia</author>
public sealed class DynamicProxyManager
@@ -43,7 +43,7 @@ namespace Spring.Proxy
/// The name of the assembly that defines proxy types created.
/// </summary>
public const string ASSEMBLY_NAME = "Spring.Proxy";
/// <summary>
/// The attributes of the proxy type to generate.
/// </summary>
@@ -62,14 +62,30 @@ namespace Spring.Proxy
public static TypeBuilder CreateTypeBuilder(string typeName, Type baseType)
{
ModuleBuilder module = DynamicCodeManager.GetModuleBuilder(ASSEMBLY_NAME);
if (baseType == null)
try
{
return module.DefineType(typeName, TYPE_ATTRIBUTES);
if (baseType == null)
{
return module.DefineType(typeName, TYPE_ATTRIBUTES);
}
else
{
return module.DefineType(typeName, TYPE_ATTRIBUTES, baseType);
}
}
else
catch (ArgumentException ex)
{
return module.DefineType(typeName, TYPE_ATTRIBUTES, baseType);
Type alreadyRegisteredType = module.GetType(typeName, true);
string msg;
if (alreadyRegisteredType != null)
msg = "Proxy already registered for \"{0}\" as Type \"{1}\".";
else
msg = "Proxy already registered for \"{0}\".";
throw new ArgumentException(string.Format(msg, typeName, alreadyRegisteredType.FullName), ex);
}
}
@@ -80,7 +96,7 @@ namespace Spring.Proxy
[Conditional("DEBUG_DYNAMIC")]
public static void SaveAssembly()
{
DynamicCodeManager.SaveAssembly( ASSEMBLY_NAME );
DynamicCodeManager.SaveAssembly(ASSEMBLY_NAME);
}
#endregion

View File

@@ -24,6 +24,7 @@ using System;
using System.Reflection.Emit;
using System.Threading;
using NUnit.Framework;
using System.Reflection;
#endregion
@@ -82,11 +83,19 @@ namespace Spring.Proxy
#endregion WorkerThread Class
[Test]
[ExpectedException(typeof(ArgumentException))]
public void CreateTypeBuilderMustNotBeCalledTwiceWithSameArguments()
{
TypeBuilder tb1 = DynamicProxyManager.CreateTypeBuilder("testtypename", null);
TypeBuilder tb2 = DynamicProxyManager.CreateTypeBuilder("testtypename", typeof(AbstractProxyTypeBuilder) );
try
{
TypeBuilder tb2 = DynamicProxyManager.CreateTypeBuilder("testtypename", typeof(AbstractProxyTypeBuilder));
Assert.Fail("Did not throw expected ArgumentException.");
}
catch (ArgumentException)
{
}
}
[Test]

View File

@@ -54,11 +54,19 @@ namespace Spring.EnterpriseServices
}
[Test]
[ExpectedException(typeof(ArgumentException))]
public void BailsWhenNotConfigured()
{
ServicedComponentExporter exp = new ServicedComponentExporter();
exp.AfterPropertiesSet();
try
{
exp.AfterPropertiesSet();
Assert.Fail("Did not throw expected ArgumentException!");
}
catch (ArgumentException)
{
}
}
[Test]