开发者

Query Orchard CMS from IShapeFactoryEvents

I am trying to insert shapes into my layout from my module once it has been enabled. I figured IShapeFactoryEvents would be perfect for this, but querying the CMS from here gives me a "TransactionScope nested incorrectly exception" if this occurs during a POST. Wondering if anyone had any words of wisdom for me again? See my code snippet below.

    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.C开发者_如何学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");
            }
        }
    }

Once again, thanks in advance. You guys are awesome.


See the blog post I did on this very topic here: http://chrisbower.com/2011/02/15/orchard-shape-wizardry/

From the blog post:

"One thing I need to note, and this took me an entire day to discover, is that you can't just inject your data service into your IShapeTableProvider implementation. If you do, it'll try to use a transaction out of scope and that will cause you all kinds of problems. After tearing my hair out for hours on end, I finally discovered what the Orchard team is doing inside of the CoreShapes class: The trick to resolve a service dependency within the function itself by using a property that loads the service per-request."

Try using your service like this instead:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜