routing based on blog module BlogPathConstraint / BlogPathConstraintUpdator

Topics: Core, Writing modules
Jul 24, 2013 at 1:06 PM
Hi all,

I've been pulling my hair out for the last week trying to understand how I can copy the BlogPathConstraint functionality to perform routing in my own module and I'd really appreciate some help.

The crux of my issue revolves around the IOrchardShellEvents.Activated implementation in BlogPathConstraintUpdator which I'm attempting to copy. I've exactly duplicated the code in the blog module so I'll not copy my code here unless asked. My issue is that whilst the call to the Refresh method works perfectly when called from IBackgroundTask.Sweep it does not work when called from IOrchardShellEvents.Activated and the error I receive is copied at the end of this message. The result of this is that my paths do not get registered until the first background sweep occurs, rather than at application startup as desired. This error I'm faced with appears to have been previously reported here: https://orchard.codeplex.com/workitem/19082 although I'm still getting this error. The steps to reproduce the issue are the same as listed.

My understanding of Autofac is probably not complete, as I'm trying to work around this by doing something like this:

using (var scope = _workContextAccessor.CreateWorkContextScope())
{
var transactionManager = scope.Resolve<ITransactionManager>();
transactionManager.Demand();
_contentManager = scope.Resolve<IContentManager>();
Refresh();
}
I've based this on my reading of this thread: http://orchard.codeplex.com/discussions/351919
but this code throws a GenericAdoException error "could not execute query", I'll post the second error next as this is getting too long.

Can anyone help me to understand what I need to do in order to access IContentManager within the IOrchardShellEvents.Activated implementation please?

Any help or pointers would be appreciated.

Thanks,
Jon.


2013-07-24 12:17:48,356 [6] Orchard.Exceptions.DefaultExceptionPolicy - An unexpected exception was caught
System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> Autofac.Core.DependencyResolutionException: No scope with a Tag matching 'work' is visible from the scope in which the instance was requested. This generally indicates that a component registered as per-HTTP request is being reqested by a SingleInstance() component (or a similar scenario.) Under the web integration always request dependencies from the DependencyResolver.Current or ILifetimeScopeProvider.RequestLifetime, never from the container itself.
at Autofac.Core.Lifetime.MatchingScopeLifetime.FindScope(ISharingLifetimeScope mostNestedVisibleScope)
at Autofac.Core.Resolving.InstanceLookup..ctor(IComponentRegistration registration, IResolveOperation context, ISharingLifetimeScope mostNestedVisibleScope, IEnumerable1 parameters)
at Autofac.Core.Resolving.ResolveOperation.GetOrCreateInstance(ISharingLifetimeScope currentOperationScope, IComponentRegistration registration, IEnumerable
1 parameters)
at Autofac.Core.Activators.Reflection.ConstructorParameterBinding.Instantiate()
at Autofac.Core.Activators.Reflection.ReflectionActivator.ActivateInstance(IComponentContext context, IEnumerable1 parameters)
at Autofac.Core.Resolving.InstanceLookup.Activate(IEnumerable
1 parameters)
at Autofac.Core.Lifetime.LifetimeScope.GetOrCreateAndShare(Guid id, Func1 creator)
at Autofac.Core.Resolving.InstanceLookup.Execute()
at Autofac.Core.Resolving.ResolveOperation.GetOrCreateInstance(ISharingLifetimeScope currentOperationScope, IComponentRegistration registration, IEnumerable
1 parameters)
at System.Linq.Enumerable.WhereSelectArrayIterator2.MoveNext()
at System.Linq.Buffer
1..ctor(IEnumerable1 source)
at System.Linq.Enumerable.ToArray[TSource](IEnumerable
1 source)
at Autofac.Features.Collections.CollectionRegistrationSource.<>c__DisplayClass4.<RegistrationsFor>b__0(IComponentContext c, IEnumerable1 p)
at Autofac.Core.Activators.Delegate.DelegateActivator.ActivateInstance(IComponentContext context, IEnumerable
1 parameters)
at Autofac.Core.Resolving.InstanceLookup.Activate(IEnumerable1 parameters)
at Autofac.Core.Resolving.InstanceLookup.Execute()
at Autofac.Core.Resolving.ResolveOperation.GetOrCreateInstance(ISharingLifetimeScope currentOperationScope, IComponentRegistration registration, IEnumerable
1 parameters)
at Autofac.Builder.RegistrationBuilder.<>c__DisplayClass11.<ForDelegate>b__0(IComponentContext c, IEnumerable1 p)
at Autofac.Core.Activators.Delegate.DelegateActivator.ActivateInstance(IComponentContext context, IEnumerable1 parameters)
at Autofac.Core.Resolving.InstanceLookup.Activate(IEnumerable
1 parameters)
at Autofac.Core.Resolving.InstanceLookup.Execute()
at Autofac.Core.Resolving.ResolveOperation.GetOrCreateInstance(ISharingLifetimeScope currentOperationScope, IComponentRegistration registration, IEnumerable1 parameters)
at Autofac.Core.Activators.Reflection.ConstructorParameterBinding.Instantiate()
at Autofac.Core.Activators.Reflection.ReflectionActivator.ActivateInstance(IComponentContext context, IEnumerable
1 parameters)
at Autofac.Core.Resolving.InstanceLookup.Activate(IEnumerable1 parameters)
at Autofac.Core.Lifetime.LifetimeScope.GetOrCreateAndShare(Guid id, Func
1 creator)
at Autofac.Core.Resolving.InstanceLookup.Execute()
at Autofac.Core.Resolving.ResolveOperation.GetOrCreateInstance(ISharingLifetimeScope currentOperationScope, IComponentRegistration registration, IEnumerable1 parameters)
at System.Linq.Enumerable.WhereSelectArrayIterator
2.MoveNext()
at System.Linq.Buffer1..ctor(IEnumerable1 source)
at System.Linq.Enumerable.ToArray[TSource](IEnumerable1 source)
at Autofac.Features.Collections.CollectionRegistrationSource.<>c__DisplayClass4.<RegistrationsFor>b__0(IComponentContext c, IEnumerable
1 p)
at Autofac.Core.Activators.Delegate.DelegateActivator.ActivateInstance(IComponentContext context, IEnumerable1 parameters)
at Autofac.Core.Resolving.InstanceLookup.Activate(IEnumerable
1 parameters)
at Autofac.Core.Resolving.InstanceLookup.Execute()
at Autofac.Core.Resolving.ResolveOperation.GetOrCreateInstance(ISharingLifetimeScope currentOperationScope, IComponentRegistration registration, IEnumerable1 parameters)
at Autofac.Builder.RegistrationBuilder.<>c__DisplayClass1
1.<ForDelegate>b__0(IComponentContext c, IEnumerable1 p)
at Autofac.Core.Activators.Delegate.DelegateActivator.ActivateInstance(IComponentContext context, IEnumerable
1 parameters)
at Autofac.Core.Resolving.InstanceLookup.Activate(IEnumerable1 parameters)
at Autofac.Core.Resolving.InstanceLookup.Execute()
at Autofac.Core.Resolving.ResolveOperation.GetOrCreateInstance(ISharingLifetimeScope currentOperationScope, IComponentRegistration registration, IEnumerable
1 parameters)
at Autofac.Core.Resolving.ResolveOperation.Execute(IComponentRegistration registration, IEnumerable1 parameters)
at Autofac.Core.Lifetime.LifetimeScope.ResolveComponent(IComponentRegistration registration, IEnumerable
1 parameters)
at Autofac.Features.LazyDependencies.LazyRegistrationSource.<>c__DisplayClass51.<>c__DisplayClass7.<CreateLazyRegistration>b__4()
at System.Lazy
1.CreateValue()
at System.Lazy1.LazyInitValue()
at Orchard.ContentManagement.DefaultContentManager.New(String contentType) in d:\~ web ~\Orchard CMS\1.6.1\src\Orchard\ContentManagement\DefaultContentManager.cs:line 85
at Orchard.ContentManagement.DefaultContentManager.Get(Int32 id, VersionOptions options, QueryHints hints) in d:\~ web ~\Orchard CMS\1.6.1\src\Orchard\ContentManagement\DefaultContentManager.cs:line 199
at System.Linq.Enumerable.WhereSelectListIterator
2.MoveNext()
at System.Collections.Generic.List1..ctor(IEnumerable1 collection)
at System.Linq.Enumerable.ToList[TSource](IEnumerable1 source)
at Orchard.ContentManagement.DefaultContentQuery.ContentQuery
1.Orchard.ContentManagement.IContentQuery<T>.List() in d:\~ web ~\Orchard CMS\1.6.1\src\Orchard\ContentManagement\DefaultContentQuery.cs:line 191
at Orchard.Blogs.Services.BlogService.Get(VersionOptions versionOptions)
at Orchard.Blogs.Routing.BlogPathConstraintUpdator.Refresh()
--- End of inner exception stack trace ---
at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor)
at System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj, Object[] parameters, Object[] arguments)
at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
at Orchard.Events.DefaultOrchardEventBus.TryInvokeMethod(IEventHandler eventHandler, Type interfaceType, String methodName, IDictionary2 arguments, IEnumerable& returnValue) in d:\~ web ~\Orchard CMS\1.6.1\src\Orchard\Events\DefaultOrchardEventBus.cs:line 84
at Orchard.Events.DefaultOrchardEventBus.TryInvoke(IEventHandler eventHandler, String interfaceName, String methodName, IDictionary
2 arguments, IEnumerable& returnValue) in d:\~ web ~\Orchard CMS\1.6.1\src\Orchard\Events\DefaultOrchardEventBus.cs:line 73
at Orchard.Events.DefaultOrchardEventBus.TryNotifyHandler(IEventHandler eventHandler, String messageName, String interfaceName, String methodName, IDictionary`2 eventData, IEnumerable& returnValue) in d:\~ web ~\Orchard CMS\1.6.1\src\Orchard\Events\DefaultOrchardEventBus.cs:line 52
Jul 24, 2013 at 1:06 PM
second exception:

2013-07-24 12:37:51,679 [6] Orchard.Exceptions.DefaultExceptionPolicy - An unexpected exception was caught
System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> NHibernate.Exceptions.GenericADOException: could not execute query
[ SELECT this_.Id as Id400_2_, this_.Number as Number400_2_, this_.Published as Published400_2_, this_.Latest as Latest400_2_, this_.Data as Data400_2_, this_.ContentItemRecord_id as ContentI6_400_2_, contentite2_.Id as Id399_0_, contentite2_.Data as Data399_0_, contentite2_.ContentType_id as ContentT3_399_0_, contenttyp3_.Id as Id401_1_, contenttyp3_.Name as Name401_1_ FROM Orchard_Framework_ContentItemVersionRecord this_ left outer join Orchard_Framework_ContentItemRecord contentite2_ on this_.ContentItemRecord_id=contentite2_.Id left outer join Orchard_Framework_ContentTypeRecord contenttyp3_ on contentite2_.ContentType_id=contenttyp3_.Id WHERE this_.Published = ? ]
Name:cp0 - Value:True
[SQL: SELECT this_.Id as Id400_2_, this_.Number as Number400_2_, this_.Published as Published400_2_, this_.Latest as Latest400_2_, this_.Data as Data400_2_, this_.ContentItemRecord_id as ContentI6_400_2_, contentite2_.Id as Id399_0_, contentite2_.Data as Data399_0_, contentite2_.ContentType_id as ContentT3_399_0_, contenttyp3_.Id as Id401_1_, contenttyp3_.Name as Name401_1_ FROM Orchard_Framework_ContentItemVersionRecord this_ left outer join Orchard_Framework_ContentItemRecord contentite2_ on this_.ContentItemRecord_id=contentite2_.Id left outer join Orchard_Framework_ContentTypeRecord contenttyp3_ on contentite2_.ContentType_id=contenttyp3_.Id WHERE this_.Published = ?] ---> System.Data.SqlClient.SqlException: MSDTC on server 'QUADKOREKILLA\SQLEXPRESS' is unavailable.
at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action1 wrapCloseInAction)
at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action
1 wrapCloseInAction)
at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose)
at System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady)
at System.Data.SqlClient.SqlDataReader.TryConsumeMetaData()
at System.Data.SqlClient.SqlDataReader.get_MetaData()
at System.Data.SqlClient.TdsParser.TdsExecuteTransactionManagerRequest(Byte[] buffer, TransactionManagerRequestType request, String transactionName, TransactionManagerIsolationLevel isoLevel, Int32 timeout, SqlInternalTransaction transaction, TdsParserStateObject stateObj, Boolean isDelegateControlRequest)
at System.Data.SqlClient.TdsParser.GetDTCAddress(Int32 timeout, TdsParserStateObject stateObj)
at System.Data.SqlClient.SqlInternalConnectionTds.GetDTCAddress()
at System.Data.SqlClient.SqlInternalConnection.EnlistNonNull(Transaction tx)
at System.Data.SqlClient.SqlInternalConnection.Enlist(Transaction tx)
at System.Data.SqlClient.SqlInternalConnectionTds.Activate(Transaction transaction)
at System.Data.ProviderBase.DbConnectionInternal.ActivateConnection(Transaction transaction)
at System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, UInt32 waitForMultipleObjectsTimeout, Boolean allowCreate, Boolean onlyOneCheckConnection, DbConnectionOptions userOptions, DbConnectionInternal& connection)
at System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, TaskCompletionSource1 retry, DbConnectionOptions userOptions, DbConnectionInternal& connection)
at System.Data.ProviderBase.DbConnectionFactory.TryGetConnection(DbConnection owningConnection, TaskCompletionSource
1 retry, DbConnectionOptions userOptions, DbConnectionInternal& connection)
at System.Data.ProviderBase.DbConnectionClosed.TryOpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource1 retry, DbConnectionOptions userOptions)
at System.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource
1 retry)
at System.Data.SqlClient.SqlConnection.Open()
at NHibernate.Connection.DriverConnectionProvider.GetConnection()
at NHibernate.AdoNet.ConnectionManager.GetConnection()
at NHibernate.AdoNet.AbstractBatcher.Prepare(IDbCommand cmd)
at NHibernate.AdoNet.AbstractBatcher.ExecuteReader(IDbCommand cmd)
at NHibernate.Loader.Loader.GetResultSet(IDbCommand st, Boolean autoDiscoverTypes, Boolean callable, RowSelection selection, ISessionImplementor session)
at NHibernate.Loader.Loader.DoQuery(ISessionImplementor session, QueryParameters queryParameters, Boolean returnProxies)
at NHibernate.Loader.Loader.DoQueryAndInitializeNonLazyCollections(ISessionImplementor session, QueryParameters queryParameters, Boolean returnProxies)
at NHibernate.Loader.Loader.DoList(ISessionImplementor session, QueryParameters queryParameters)
--- End of inner exception stack trace ---
at NHibernate.Loader.Loader.DoList(ISessionImplementor session, QueryParameters queryParameters)
at NHibernate.Loader.Loader.ListIgnoreQueryCache(ISessionImplementor session, QueryParameters queryParameters)
at NHibernate.Loader.Loader.List(ISessionImplementor session, QueryParameters queryParameters, ISet1 querySpaces, IType[] resultTypes)
at NHibernate.Loader.Criteria.CriteriaLoader.List(ISessionImplementor session)
at NHibernate.Impl.SessionImpl.List(CriteriaImpl criteria, IList results)
at NHibernate.Impl.CriteriaImpl.List(IList results)
at NHibernate.Impl.CriteriaImpl.List[T]()
at Orchard.ContentManagement.DefaultContentQuery.Slice(Int32 skip, Int32 count) in d:\~ web ~\Orchard CMS\1.6.1\src\Orchard\ContentManagement\DefaultContentQuery.cs:line 146
at Orchard.ContentManagement.DefaultContentQuery.ContentQuery
1.Orchard.ContentManagement.IContentQuery<T>.List() in d:\~ web ~\Orchard CMS\1.6.1\src\Orchard\ContentManagement\DefaultContentQuery.cs:line 191
at Origin.Professionals.Routing.ProfessionalsDocumentCategoryConstraintUpdator.Refresh()
at Origin.Professionals.Routing.ProfessionalsDocumentCategoryConstraintUpdator.Orchard.Environment.IOrchardShellEvents.Activated()
--- End of inner exception stack trace ---
at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor)
at System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj, Object[] parameters, Object[] arguments)
at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
at System.Reflection.MethodBase.Invoke(Object obj, Object[] parameters)
at Orchard.Events.DefaultOrchardEventBus.TryInvokeMethod(IEventHandler eventHandler, Type interfaceType, String methodName, IDictionary2 arguments, IEnumerable& returnValue) in d:\~ web ~\Orchard CMS\1.6.1\src\Orchard\Events\DefaultOrchardEventBus.cs:line 83
at Orchard.Events.DefaultOrchardEventBus.TryInvoke(IEventHandler eventHandler, String interfaceName, String methodName, IDictionary
2 arguments, IEnumerable& returnValue) in d:\~ web ~\Orchard CMS\1.6.1\src\Orchard\Events\DefaultOrchardEventBus.cs:line 68
at Orchard.Events.DefaultOrchardEventBus.TryNotifyHandler(IEventHandler eventHandler, String messageName, String interfaceName, String methodName, IDictionary`2 eventData, IEnumerable& returnValue) in d:\~ web ~\Orchard CMS\1.6.1\src\Orchard\Events\DefaultOrchardEventBus.cs:line 52