Tutorial: How to use NopCommerce data, core, services, framework in your external program

1 年 前
Hello All,

I am trying to utilize nopCommerce project to my console application.

I followed all steps mentioned for nopCommerce version 4.0.
But still I am getting error


None of the constructors found with 'Autofac.Core.Activators.Reflection.DefaultConstructorFinder' on type 'Nop.Services.Orders.OrderService' can be invoked with the available services and parameters:
Cannot resolve parameter 'Nop.Core.Data.IRepository`1[Nop.Core.Domain.Orders.Order] orderRepository' of constructor 'Void .ctor(Nop.Core.Data.IRepository`1[Nop.Core.Domain.Orders.Order], Nop.Core.Data.IRepository`1[Nop.Core.Domain.Orders.OrderItem], Nop.Core.Data.IRepository`1[Nop.Core.Domain.Orders.OrderNote], Nop.Core.Data.IRepository`1[Nop.Core.Domain.Catalog.Product], Nop.Core.Data.IRepository`1[Nop.Core.Domain.Orders.RecurringPayment], Nop.Core.Data.IRepository`1[Nop.Core.Domain.Customers.Customer], Nop.Services.Events.IEventPublisher)'.



Here is my code snipped.

class Program
    {
        static void Main(string[] args)
        {

            try
            {
                EngineContextInitializer.Run();
                Console.WriteLine("Hello World!");

                var builder = new ContainerBuilder();
                builder.RegisterType<OrderService>().As<IOrderService>().SingleInstance();

                //Resolving inteface with autofac
                var container = builder.Build();
                var orderServiceService = container.Resolve<IOrderService>();

            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);

            }

            Console.ReadLine();
        }


Any help will be great help to me!
1 年 前
I'm not sure this is the "right" way to do it, but I was able to get past the DI problems in a console application. Here's what I did.

First, implement IDependencyRegistrar (I chose to do it in Program for simplicity):

    class Program : IDependencyRegistrar
    {


Next, create a member (static member, in my case) of type IContainer and a setter for said member:

        private static IContainer _container;

        static void SetContainer(IContainer container) {
            _container = container;
        }


Next, implement IDependencyRegistrar's methods. In the Register method register your SetContainer method as a build callback:

        public int Order => 0;

        public void Register(ContainerBuilder builder, ITypeFinder typeFinder, NopConfig config)
        {
            builder.RegisterBuildCallback(SetContainer);
        }


That's it. Now, after EngineContextInitializer.Run() has finished you should be able to call _container.Resolve to resolve the services:

        static void Main(string[] args)
        {
            EngineContextInitializer.Run();

            var customerService = _container.Resolve<ICustomerService>();
            var orderService = _container.Resolve<IOrderService>();
            var productService = _container.Resolve<IProductService>();
1 年 前
One minor update. If you're planning on your application being long-lived remember to mind the scope of the DbContext. If you don't it can grow and grow and grow. For example, if you're inserting a bunch of customers in a loop you'll have to scope the services to each iteration of the loop, otherwise the DbContext will grow and performance will degrade over time as more and more objects are tracked. e.t.:

            for (...)
            {
                try
                {
                    using (var scope = _container.BeginLifetimeScope())
                    {

                        var customerService = scope.Resolve<ICustomerService>();
                        var orderService = scope.Resolve<IOrderService>();
3 个月 前
Can you please provide sample source code?
1 个月 前
Hello all,

I currently use 4.3 nopcommerce version and builder.RegisterBuildCallback doesn't seem to work anymore.

EngineContextInitializer has a new method : getConnection (replace run ?)

Does anyone have a working solution to access 4.3 version ?