Reputation: 3365
I'm running Orchard Core and have created a custom IRule
that needs to query the Content Items. (Obviously querying the DB is time-consuming, so we only do this if something happened and we need to rebuild our redirects cache.) To do so, I am getting an IOrchardHelper
from the service provider, and using that helper, I'm executing
var listCi = await orchardHelper.GetContentItemByAliasAsync("redirects-list", false);
However, this is throwing a NullReferenceException
, because GetService<ISession>()
returns null
, but AliasPartContentHandleHelper.QueryAliasIndex(session, alias)
assumes that session is non-null.
Is there anyway to adjust things so that GetService<ISession>()
returns a session?
Upvotes: 0
Views: 47
Reputation: 617
You can resolve the current tenants scoped service provider by using the accessor
var session = ShellScope.Current.ServiceProvider.GetRequiredService<ISession>();
and query your content item from there (you probably won't be able to use the IOrchardCoreHelper
methods), so make an ISession
query directly. Something like
var content = _session.Query<ContentItem, AliasPartIndex>(x => x.Alias == "youralias");
However, be a little wary, as this returns the current tenants shell scope, so will vary by tenant, and you will have to make sure your rewrite middleware is added after the orchard core middleware, so you are in the correct scope.
Upvotes: 0