2

我试图从我的模块插入形状到我的布局,一旦它已被启用。我认为IShapeFactoryEvents对于这一点来说是完美的,但是如果在POST期间发生这种情况,则从这里查询CMS会给我一个“TransactionScope嵌套错误异常”。想知道是否有人再次对我有任何智慧话语?看到我下面的代码片段。从IShapeFactoryEvents查询Orchard CMS

public void Created(ShapeCreatedContext context) 
    { 
     if (context.ShapeType == "Layout") 
     {     
      if (!AdminFilter.IsApplied(_services.WorkContext.HttpContext.Request.RequestContext)) 
      { 
       var route = RouteTable.Routes.GetRouteData(_services.WorkContext.HttpContext); 
       object location; 
       if (route.Values.TryGetValue("location", out location)) 
       { 
        var region = _services.ContentManager.Query("Region") 
         .Join<RoutePartRecord>() 
         .Where(x => x.Slug == (string)location) 
         .Slice(1).FirstOrDefault(); 

        context.Shape.Header.Add(context.New.CurrentRegion(Title: region.As<RoutePart>().Title), "10"); 
       } 
       context.Shape.Navigation.Add(context.New.RegionSelector(Regions: _services.ContentManager.Query(VersionOptions.Published, "Region").List()), "11"); 
      } 
     } 
    } 

再次,在此先感谢。你们真棒。

回答

1

查看博客文章中,我做了这个题目在这里:http://chrisbower.com/2011/02/15/orchard-shape-wizardry/

从博客文章:

“有一件事我需要注意了,这花了我一整天探索,就是你不能将你的数据服务注入到你的IShapeTableProvider实现中,如果你这样做,它会尝试使用一个超出范围的事务,并且会导致你所有的问题。 ,我终于发现了Orchard团队在CoreShapes类中所做的事情:通过使用属性来解决函数本身中的服务依赖关系的技巧加载每个请求的服务。“

尝试使用你的服务是这样,而不是:

private IOrchardServices Services  
{ 
    get 
    { 
     return _workContextAccessor.GetContext(_httpContextAccessor.Current()).Resolve<IOrchardServices>(); 
    } 
} 
+0

感谢@克里斯 - 鲍尔,工作一种享受。你的网站进展如何?它在生产吗? – Brendan

+0

@Brendan - 是的,我们在六月发布了它:[aclj.org](http://aclj.org) –

+0

@Chris另一种解决方案是将'Work '而不是'T'注入'IShapeTableProvider'实现构造函数。然后,您可以通过'Work '的'Value'属性访问底层对象。它返回适合当前上下文的实例。当您需要将每个请求依赖项注入单例依赖项时,这是有用的。 –