Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Johan.B 4 posts 74 karma points
    Sep 25, 2018 @ 09:01
    Johan.B
    0

    Run Tea Commerce in a different database than Umbraco

    Hello,

    I'm trying to run Tea Commerce in a different database than Umbraco. The reason is to make it easier to update the production environment.

    I have succesfully copied all TeaCommerce_ tables to a new database and now I need to be able to use this new database.

    I have not managed to find any examples on how to do this. This thread touches on it:

    https://our.umbraco.com/packages/website-utilities/tea-commerce/tea-commerce-support/57053-Is-it-possible-to-use-separate-database-for-TeaCommerce

    However I don't fully understand how to do it and what needs to be done. I have done some overrides previously on shipping calculator, but this seems to be different.

    Can you please help me out and provide me with some example code?

    What I have currently written is the following, but I'm not even sure I'm remotely close.

    [SuppressDependency("TeaCommerce.Api.DatabaseFactory.IDatabaseFactory", "TeaCommerce.Api")]
            public class CustomDataBase : IDatabaseFactory2
            {
                public UmbracoDatabase CreateNewDatabase()
                {
    
                    var connectionString = ConfigurationManager.ConnectionStrings["teaCommerceDbDSN"].ConnectionString;
                    var teaCommerceDb = new UmbracoDatabase(connectionString);
                    return teaCommerceDb;
                }
    
                public UmbracoDatabase CreateDatabase()
                {
                    throw new NotImplementedException();
                }
    
                public void Dispose()
                {
                    throw new NotImplementedException();
                }
            }
    

    Some quick help would be much appreciated as I need this done before friday.

    Regards, Johan

  • Rune Grønkjær 1294 posts 2856 karma points
    Sep 26, 2018 @ 07:03
    Rune Grønkjær
    0

    Hi Johan,

    There might be a couple of things you could try. First I think you should ude IDatabaseFactory instead of IDatabaseFactory2.

    Second the method your should be adding is the Get() method of that interface.

    I think you also might be missing the below registration of the dependencies. Note that I have registered two different dependencies. You should register CustomDataBase instead of IDatabaseFactory

    using Autofac;
    using TeaCommerce.Api.Common;
    using TeaCommerce.Api.InformationExtractors;
    using TeaCommerce.Api.Repositories;
    using Website.Extensions.Ecommerce.TeaCommerceExtensions;
    
    namespace Website.Extensions.Ecommerce.Configuration.AutofacModules {
      public class InformationExtractorsConfig : Module {
    
        protected override void Load( ContainerBuilder builder ) {
          builder.MustNotBeNull( "builder" );
    
          builder.RegisterType<ProductRepository>().As<IProductRepository>().InstancePerLifetimeScope();
          builder.RegisterType<SearchResultProductInformationExtractor>().As<IProductInformationExtractor>().InstancePerLifetimeScope();
        }
    
      }
    }
    

    Hope it makes sence and you can get it to work.

    /Rune

  • Johan.B 4 posts 74 karma points
    Sep 26, 2018 @ 09:03
    Johan.B
    0

    Hello,

    Thank you for your reply.

    I had not registered the dependency so great pointer.

    It says IDatabaseFactory is obsolete and IDatabaseFactory2 should be used instead, anyway I don't mind using an obsolete function for the time being if that is the way to go.

    The IDatabaseFactory only contains one method CreateDatabase() not a Get() method.

    I found this thread about the original implementation and tried according to this pattern but it didn't work.

    Can you please provide me with how this function should be written? I feel like i'm fumbling in the dark since i'm not able to find any documentation or any examples of how this should be done.

    Regards, Johan

  • Rune Grønkjær 1294 posts 2856 karma points
    Sep 27, 2018 @ 07:00
    Rune Grønkjær
    0

    Hi Johan

    I believe you still just need the autofac part. Here's how we do that in Tea Commerce for Umbraco: https://github.com/TeaCommerce/Tea-Commerce-for-Umbraco/blob/770705bdea8f20fd9e146c27724c9a830454c8e0/Source/TeaCommerce.Umbraco.Configuration/AutofacModules/PersistenceConfig.cs

    And then you will need the suppress dependency part as well: [SuppressDependency("TeaCommerce.Api.DatabaseFactory.IDatabaseFactory", "TeaCommerce.Api")]

  • Johan.B 4 posts 74 karma points
    Sep 27, 2018 @ 09:38
    Johan.B
    0

    Hello,

    Thank you for your answer once again.

    Ok so this is what I have done.

    namespace TeaCommerce.Umbraco.Configuration.AutofacModules
    {
        public class PersistenceConfig : Module
        {
            protected override void Load(ContainerBuilder builder)
            {
                builder.MustNotBeNull("builder");
    
                builder.RegisterType<CustomDatabaseFactory>().As<IDatabaseFactory>().PreserveExistingDefaults().InstancePerLifetimeScope();
            }
        }
    }
    
    namespace TeaCommerce.Umbraco.Configuration.Persistence
    {
        [SuppressDependency("TeaCommerce.Api.DatabaseFactory.IDatabaseFactory", "TeaCommerce.Api")]
        public class CustomDatabaseFactory : IDatabaseFactory
        {
            public UmbracoDatabase CreateDatabase()
            {
                var connectionString = ConfigurationManager.ConnectionStrings["teaCommerceDbDSN"].ConnectionString;
                var teaCommerceDb = new UmbracoDatabase(connectionString);
                return teaCommerceDb;
            }
            public void Dispose()
            {
                throw new NotImplementedException();
            }
        }
    }
    

    And it does not work. The SuppressDependency does something, because when I use the annotation I get below error message when I try to run the website. However when I set a breakpoint in my CustomDatabaseFactory it seems like the code isn't executed because it doesn't break. Do you have any suggestions?

    [ArgumentNullException: Value cannot be null. Parameter name: context] Autofac.ResolutionExtensions.ResolveService(IComponentContext context, Service service, IEnumerable1 parameters) +116 Autofac.ResolutionExtensions.Resolve(IComponentContext context, IEnumerable1 parameters) +118 Autofac.ResolutionExtensions.Resolve(IComponentContext context) +75 TeaCommerce.Umbraco.Configuration.ApplicationStartup.ApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext) +136 Umbraco.Core.ApplicationEventHandler.OnApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext) +37 Umbraco.Core.CoreBootManager.

    [HttpException (0x80004005): Value cannot be null. Parameter name: context] System.Web.HttpApplicationFactory.EnsureAppStartCalledForIntegratedMode(HttpContext context, HttpApplication app) +10061317 System.Web.HttpApplication.RegisterEventSubscriptionsWithIIS(IntPtr appContext, HttpContext context, MethodInfo[] handlers) +118 System.Web.HttpApplication.InitSpecial(HttpApplicationState state, MethodInfo[] handlers, IntPtr appContext, HttpContext context) +173 System.Web.HttpApplicationFactory.GetSpecialApplicationInstance(IntPtr appContext, HttpContext context) +336 System.Web.Hosting.PipelineRuntime.InitializeApplication(IntPtr appContext) +296

    [HttpException (0x80004005): Value cannot be null. Parameter name: context] System.Web.HttpRuntime.FirstRequestInit(HttpContext context) +10042604 System.Web.HttpRuntime.EnsureFirstRequestInit(HttpContext context) +95 System.Web.HttpRuntime.ProcessRequestNotificationPrivate(IIS7WorkerRequest wr, HttpContext context) +254

  • Anders Burla Johansen 2560 posts 8255 karma points
    Sep 30, 2018 @ 16:06
    Anders Burla Johansen
    0

    Hi Johan

    You need to suppress the DatabaseFactory to do this. Its the one that does the DB creation from a specific connection string name.

    See this here: https://github.com/TeaCommerce/Tea-Commerce-for-Umbraco/blob/master/Source/TeaCommerce.Umbraco.Configuration/Persistence/DatabaseFactory.cs

    This is the one you need to override using the SuppressDependency concept of Tea Commerce. https://docs.teacommerce.net/v3.0.0/reference#override-default-providers

    So no need to IDatabase and low level PetaPoco stuff.

    Kind regards

    Anders

Please Sign in or register to post replies

Write your reply to:

Draft