Upgrading to 1.7 RC: various error

Topics: General, Installing Orchard
Jul 25, 2013 at 8:52 AM
Edited Jul 25, 2013 at 8:52 AM
I am trying to upgrade my website (intranet) to 1.7 RC.

1 issue is with existing modules that depend on Contrib.Taxonomies and Orchard.Media.

My deployment method was to deploy the built package only, so I only included those that I thought were necessary, which means those modules were removed from the package. My module, which is depended on both of these modules, was modified to refer to the new ones. Orchard (or more specific, autofac) throws the following error:
Orchard.Environment.DefaultOrchardHost - A tenant could not be started: Default
Autofac.Core.DependencyResolutionException: None of the constructors found with 'Orchard.Environment.AutofacUtil.DynamicProxy2.ConstructorFinderWrapper' on type 'NineSeas.Migrations' can be invoked with the available services and parameters:
Cannot resolve parameter 'Orchard.Taxonomies.Services.ITaxonomyService taxonomyService' of constructor 'Void .ctor(Orchard.IOrchardServices, Orchard.Services.IClock, Orchard.Data.ISessionLocator, Orchard.Data.IRepository`1[NineSeas.Models.AuthorProfilePartRecord], Orchard.Data.IRepository`1[NineSeas.Models.BookPartRecord], Orchard.Data.IRepository`1[Orchard.Core.Common.Models.CommonPartRecord], Orchard.Data.IRepository`1[Orchard.Core.Common.Models.CommonPartVersionRecord], Orchard.Taxonomies.Services.ITaxonomyService, NineSeas.Handlers.IWeekCalculateTaskHandler, NineSeas.Services.ITaxonomyImportService)'.
Orchard.Taxonomies was not enabled in this case (because I am upgrading), so Orchard don't 'know' about it. I managed to make it work again by modifying Settings_ShellFeatureRecord and Settings_ShellFeatureStateRecord tables, and the file cache.dat. But then the second issue appeared.

2 issue: Session is closed!

Object name: 'ISession'.

2013-07-25 14:39:13,540 [15] Orchard.Data.SessionLocator - Error while disposing the transaction.
System.ObjectDisposedException: Cannot access a disposed object.
Object name: 'AdoTransaction'.
   at NHibernate.Transaction.AdoTransaction.Rollback()
   at Orchard.Data.SessionLocator.Dispose() in e:\Projects\bellight\Codeplex\Orchard1.7\src\Orchard\Data\SessionLocator.cs:line 84
Seems like the transaction or session mechanism in Orchard was changed in 1.7. The above error shows up very frequently in many different areas. I had to attach to the process of Orchard to find out this. A piece of code like this (taken from Q42.DbTranslations module) worked before, but now it breaks:
var _sessionLocator = wc.Resolve<ISessionLocator>();
        using (var session = _sessionLocator.For(typeof(TranslationRecord)))
          // do something

        foreach (var cult in _cultureManager.ListCultures())
          if (!model.TranslationStates.ContainsKey(cult))
            model.TranslationStates.Add(cult, new CultureIndexViewModel.CultureTranslationState { NumberOfTranslatedStrings = 0 });
the code breaks right after it gets out of the inner using block. I'm guessing it's because the using block try to dispose the session object. But this is only one place and is easily fixable, I also have tons of errors like this in other places that don't know how to fix.

My question is, is there a guide on migrating the old code like this?
Jul 25, 2013 at 1:51 PM
Edited Jul 25, 2013 at 1:57 PM
  1. Enable the new Orchard.Taxonomies feature via your module migrations. Inject IFeatureManager and call _featureManager.EnableFeatures(new[] { "Orchard.Taxonomies" }, true); inside migrations.
  2. This code is wrong. I don't quite get how it managed to work previously. First - do not dispose ISession prematurely - it will be disposed correctly at the end of the request. Second - use IRepository<TranslationRecord> instead.
Jul 25, 2013 at 6:12 PM
Yes your solution is good about #1, but please note that the log mentioned about the migration file, which used the service from the taxonomies module So this dependency must be taken care of before anything else can run. Anyway, I could get around this by modify the database and the cache.dat file.

About the number 2, as I said, it was taken from Q42.DbTranslations that I could find in the gallery. But it wasn't the only thing that crashed. In fact, I could easily fix that. I also have many other places that have the same issue. After digging deep into the code of Orchard, I could find the following line that seems to caused the issue (line number 100 of the 'Orchard.Data.SessionLocator' class):
private void EnsureSession() {
    if (_session != null) {

    var sessionFactory = _sessionFactoryHolder.GetSessionFactory();
    Logger.Information("Opening database session");
    _session = sessionFactory.OpenSession(new OrchardSessionInterceptor(_interceptors.ToArray(), Logger));
If the session was disposed, or by any reason that it is not open and is not null, then the issue will occur. I could make the issue go away by changing the second line into 'if (_session != null && _session.IsOpen && _session.IsConnected) {'. Is this the correct way? Actually I have almost no knowledge about nHibernate.