Search code examples
sitecoreglass-mapper

Sitecore Glass Mapper : "Duplicate type name within an assembly" Error


I'm having a problem fetching an item using the Glass Mapper SitecoreService. The item definitely exists and the type is correct. (It's being generated by TDS).

var language = Language.Parse("en-GB");
var service = new SitecoreService("master");                
var targetPath = String.Format("{0}-migrated", Constants.SourceProviderListPageItem);
var target = service.GetItem<IProvider_List_Page>(targetPath, language);

The above code causes the following exception when service.GetItem<T> is called.

Duplicate type name within an assembly.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.ArgumentException: Duplicate type name within an assembly.

[ArgumentException: Duplicate type name within an assembly.]
System.Reflection.Emit.ModuleBuilder.CheckTypeNameConflict(String strTypeName, Type enclosingType) +14373369
System.Reflection.Emit.AssemblyBuilderData.CheckTypeNameConflict(String strTypeName, TypeBuilder enclosingType) +91
System.Reflection.Emit.TypeBuilder.Init(String fullname, TypeAttributes attr, Type parent, Type[] interfaces, ModuleBuilder module, PackingSize iPackingSize, Int32 iTypeSize, TypeBuilder enclosingType) +161
System.Reflection.Emit.ModuleBuilder.DefineType(String name, TypeAttributes attr) +263
Castle.DynamicProxy.Generators.Emitters.ClassEmitter..ctor(ModuleScope modulescope, String name, Type baseType, IEnumerable`1 interfaces, TypeAttributes flags, Boolean forceUnsigned) +156
Castle.DynamicProxy.Generators.InvocationTypeGenerator.GetEmitter(ClassEmitter class, Type[] interfaces, INamingScope namingScope, MethodInfo methodInfo) +239
Castle.DynamicProxy.Generators.InvocationTypeGenerator.Generate(ClassEmitter class, ProxyGenerationOptions options, INamingScope namingScope) +124
Castle.DynamicProxy.Contributors.InterfaceProxyWithoutTargetContributor.GetInvocationType(MetaMethod method, ClassEmitter emitter, ProxyGenerationOptions options) +430
Castle.DynamicProxy.Contributors.InterfaceProxyWithoutTargetContributor.GetMethodGenerator(MetaMethod method, ClassEmitter class, ProxyGenerationOptions options, OverrideMethodDelegate overrideMethod) +100
Castle.DynamicProxy.Contributors.CompositeTypeContributor.ImplementMethod(MetaMethod method, ClassEmitter class, ProxyGenerationOptions options, OverrideMethodDelegate overrideMethod) +69
Castle.DynamicProxy.Contributors.CompositeTypeContributor.ImplementProperty(ClassEmitter emitter, MetaProperty property, ProxyGenerationOptions options) +128
Castle.DynamicProxy.Contributors.CompositeTypeContributor.Generate(ClassEmitter class, ProxyGenerationOptions options) +306
Castle.DynamicProxy.Generators.InterfaceProxyWithoutTargetGenerator.GenerateType(String typeName, Type proxyTargetType, Type[] interfaces, INamingScope namingScope) +499
Castle.DynamicProxy.Generators.<>c__DisplayClass1.<GenerateCode>b__0(String n, INamingScope s) +38
Castle.DynamicProxy.Generators.BaseProxyGenerator.ObtainProxyType(CacheKey cacheKey, Func`3 factory) +757
Castle.DynamicProxy.ProxyGenerator.CreateInterfaceProxyWithoutTarget(Type interfaceToProxy, Type[] additionalInterfacesToProxy, ProxyGenerationOptions options, IInterceptor[] interceptors) +159
Castle.DynamicProxy.ProxyGenerator.CreateInterfaceProxyWithoutTarget(Type interfaceToProxy, IInterceptor interceptor) +129
Glass.Mapper.Pipelines.ObjectConstruction.Tasks.CreateInterface.CreateInterfaceTask.Execute(ObjectConstructionArgs args) +381
Glass.Mapper.Pipelines.AbstractPipelineRunner`2.Run(T args) +616
Glass.Mapper.AbstractService.InstantiateObject(AbstractTypeCreationContext abstractTypeCreationContext) +646
Glass.Mapper.Sc.SitecoreService.CreateType(Type type, Item item, Boolean isLazy, Boolean inferType, Dictionary`2 parameters, Object[] constructorParameters) +721
Glass.Mapper.Sc.SitecoreService.CreateType(Item item, Boolean isLazy, Boolean inferType) +215
Glass.Mapper.Sc.SitecoreService.GetItem(String path, Language language, Boolean isLazy, Boolean inferType) +239
<Redacted>.Page_Load(Object sender, EventArgs e) +584
System.Web.UI.Control.LoadRecursive() +71
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +3178

Any idea what might be causing the problem?

Cheers


Solution

  • If you are using the auto-generated class files using the T4 code generation templates from Hedgehog then the generated C# class file will contain both interfaces and concrete classes.

    Since you are using an interface to get content from Sitecore, you are actually using Interfaces as Models. I haven't dug deep into Glass Mapper but my guess is the existence of both an Interface and concrete is causing issues here.

    Instead, use the concrete class:

    var target = service.GetItem<Provider_List_Page>(targetPath, language);
    

    You can replace the var type with IProvider_List_Page if you require.