我在使用Glass Mapper SitecoreService获取项目时遇到问题。该项目肯定存在且类型正确。 (它是由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);
当service.GetItem&lt; T&gt;时,上述代码会导致以下异常。叫做。
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
知道可能导致问题的原因是什么?
干杯
答案 0 :(得分:1)
如果您使用来自Hedgehog的T4 code generation templates自动生成的类文件,则生成的C#类文件将包含接口和具体类。
由于您使用界面从Sitecore获取内容,因此您实际上正在使用Interfaces as Models。我还没有深入挖掘Glass Mapper,但我的猜测是接口和具体的存在导致了问题。
相反,使用具体类:
var target = service.GetItem<Provider_List_Page>(targetPath, language);
如果需要,您可以将var
类型替换为IProvider_List_Page
。