2016-11-18 52 views
2

我有一个Sitecore 8.1 CD实例。我也有一些代码需要在Master数据库中创建一个内容项。 (我知道这是一个禁忌,但我现在只需要弄清楚)当我的代码试图使用Glass Mapper创建一个内容项时,我得到一个错误。这里是代码片段和错误消息。我只是想了解错误的含义。我有一个感觉,这只是一个配置问题。此代码在我们的Sitecore CM服务器上正常工作。所以我希望通过简单地调整我们的CD服务器上的配置,我可以得到这个工作。到目前为止,我已经在ConnectionStrings.config和Sitecore.config中重新启用了Master条目。但是这并没有解决这个问题。如何修复Glass Mapper错误 - 无法找到父项类型的配置?

SitecoreService service = new SitecoreService("master"); 
SimpleAes aes = new SimpleAes(); 

using (new SecurityDisabler()) 
{ 
    Item parentItem = Factory.GetDatabase("master").GetItem("/sitecore/content/Non Page Content/Account Information/Shipping Addresses"); 
    newAddress = service.Create(parentItem, newAddress);  //THIS IS WHERE THE CODE FAILS 
    user.Addresses.Add(newAddress); 
    Utility.PublishItem(service.ResolveItem(newAddress)); 
    id = aes.EncryptToString(newAddress.Id.ToString()); 
    user.Addresses = user.Addresses; 
    user.Save(); 
} 

错误消息:

Glass.Mapper.MapperException:未能找到父 项目类型的配置Sitecore.Data.Items.Item ---> System.NullReferenceException: 对象引用不设置为一个对象的实例。在在 Glass.Mapper.Context.GetTypeConfiguration [T](对象OBJ,布尔 doNotLoad,布尔checkBase) System.Object.GetType()在 Glass.Mapper.Sc.SitecoreService.Create [T,TK](TK parent,T newItem, Boolean updateStatistics,Boolean silent)---内部异常结束 堆栈跟踪---在Glass.Mapper.Sc.SitecoreService.Create [T,TK](TK parent,T newItem,Boolean updateStatistics布尔无声)

回答

0

它未能在这一行

Item parentItem = Factory.GetDatabase("master").GetItem("/sitecore/content/Non Page Content/Account Information/Shipping Addresses"); 

如果你把它周围的支票说

if (parentItem != null) { // your code } 

然后代码将工作通过,你不会得到例外,但如果parentItem为null,什么都不会发生为好。

快速修复解决方案将在您的CD服务器上提供一个'主'数据库连接字符串(这是您所说的禁止)。更好的解决方案是通过Sitecore项目API或您的自定义API公开主数据库,通过身份验证保护它,然后通过API从CD服务器调用此代码。

0

我不确定你是否仍在寻找如何解决这个问题,但是当我今天面对它时,我发现你的问题。

问题是您的parentItem有型号Item。它在Glass里面引起问题。 您可以使用任何类型作为父项,但限制是它不应该继承自Sitecore Item类。 试试这个:

var parentItem = Factory.GetDatabase("master").GetItem("/sitecore/content/Non Page Content/Account Information/Shipping Addresses").GlassCast<BaseSitecoreItem>(); 
newAddress = service.Create(parentItem, newAddress); 

其中BaseSitecoreItem是你的一些玻璃模型。

它帮助了我,希望能帮助你。

相关问题