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

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
6 years ago
Well v4.0 was not same easy.

How to do it for windows forms project:

1. Add projects to your solution:
Nop.Core
Nop.Data
Nop.Services
Nop.Web.Framework

Add them as build dependencies for your project.

2. Add this class:

using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;

namespace yournamespace
{
    public static class EngineContextInitializer
    {
        public static void Run()
        {
            var args = new string[] { };
            var host = WebHost.CreateDefaultBuilder(args)
            .UseKestrel(options => options.AddServerHeader = false)
            .UseStartup<Startup>()
            .Build();
        }
    }
}

Add Startup.cs file from nopcommerce source to your project (change its namespace to yours).
Add appsettings.json file to your project.
Add App_Data folder.
Add dataSettings.json file into it and make it as Content copy if newer.
Also your release/debug folder must contain these folders:
Plugins
Themes
wwwroot\db_backups
These folders just need to be exist. Can be clean. To not forget to create them - add them to project and place dummy content files inside. No need for Plugins (it will be created automatically).

Now add somewhere in your program (like in Form1 constructor):

EngineContextInitializer.Run();

And finally the funniest part.
You need to add all referenced needed for WebHost...Build() to execute.
Technically you just try to build/run a program. Look inside nopcommerce source what dll is used for unknown reference. Look exceptions for needed references.

All files are inside:
c:\program files\dotnet\sdk\NuGetFallbackFolder\ DLL NAME \ VERSION \ LIB ... dll file

In my case these were needed (from csproj):

    <Reference Include="Microsoft.AspNetCore, Version=2.0.1.0, Culture=neutral, PublicKeyToken=adb9793829ddae60, processorArchitecture=MSIL">
      <SpecificVersion>False</SpecificVersion>
      <HintPath>..\..\..\Program Files\dotnet\sdk\NuGetFallbackFolder\microsoft.aspnetcore\2.0.1\lib\netstandard2.0\Microsoft.AspNetCore.dll</HintPath>
    </Reference>
    <Reference Include="Microsoft.AspNetCore.Hosting">
      <HintPath>..\..\..\Program Files\dotnet\sdk\NuGetFallbackFolder\microsoft.aspnetcore.hosting\2.0.1\lib\netstandard2.0\Microsoft.AspNetCore.Hosting.dll</HintPath>
    </Reference>
    <Reference Include="Microsoft.AspNetCore.Hosting.Abstractions, Version=2.0.1.0, Culture=neutral, PublicKeyToken=adb9793829ddae60" />
    <Reference Include="Microsoft.AspNetCore.Hosting.Server.Abstractions">
      <HintPath>..\..\..\Program Files\dotnet\sdk\NuGetFallbackFolder\microsoft.aspnetcore.hosting.server.abstractions\2.0.1\lib\netstandard2.0\Microsoft.AspNetCore.Hosting.Server.Abstractions.dll</HintPath>
    </Reference>
    <Reference Include="Microsoft.AspNetCore.Http.Abstractions, Version=2.0.1.0, Culture=neutral, PublicKeyToken=adb9793829ddae60, processorArchitecture=MSIL">
      <SpecificVersion>False</SpecificVersion>
      <HintPath>..\..\..\Program Files\dotnet\sdk\NuGetFallbackFolder\microsoft.aspnetcore.http.abstractions\2.0.1\lib\netstandard2.0\Microsoft.AspNetCore.Http.Abstractions.dll</HintPath>
    </Reference>
    <Reference Include="Microsoft.AspNetCore.Server.IISIntegration">
      <HintPath>..\..\..\Program Files\dotnet\sdk\NuGetFallbackFolder\microsoft.aspnetcore.server.iisintegration\2.0.1\lib\netstandard2.0\Microsoft.AspNetCore.Server.IISIntegration.dll</HintPath>
    </Reference>
    <Reference Include="Microsoft.AspNetCore.Server.Kestrel">
      <HintPath>..\..\..\Program Files\dotnet\sdk\NuGetFallbackFolder\microsoft.aspnetcore.server.kestrel\2.0.1\lib\netstandard2.0\Microsoft.AspNetCore.Server.Kestrel.dll</HintPath>
    </Reference>
    <Reference Include="Microsoft.AspNetCore.Server.Kestrel.Core">
      <HintPath>..\..\..\Program Files\dotnet\sdk\NuGetFallbackFolder\microsoft.aspnetcore.server.kestrel.core\2.0.1\lib\netstandard2.0\Microsoft.AspNetCore.Server.Kestrel.Core.dll</HintPath>
    </Reference>
    <Reference Include="Microsoft.AspNetCore.Server.Kestrel.Transport.Abstractions">
      <HintPath>..\..\..\Program Files\dotnet\sdk\NuGetFallbackFolder\microsoft.aspnetcore.server.kestrel.transport.abstractions\2.0.1\lib\netstandard2.0\Microsoft.AspNetCore.Server.Kestrel.Transport.Abstractions.dll</HintPath>
    </Reference>
    <Reference Include="Microsoft.AspNetCore.Server.Kestrel.Transport.Libuv">
      <HintPath>..\..\..\Program Files\dotnet\sdk\NuGetFallbackFolder\microsoft.aspnetcore.server.kestrel.transport.libuv\2.0.1\lib\netstandard2.0\Microsoft.AspNetCore.Server.Kestrel.Transport.Libuv.dll</HintPath>
    </Reference>
    <Reference Include="Microsoft.Extensions.Configuration">
      <HintPath>..\..\..\Program Files\dotnet\sdk\NuGetFallbackFolder\microsoft.extensions.configuration\2.0.0\lib\netstandard2.0\Microsoft.Extensions.Configuration.dll</HintPath>
    </Reference>
    <Reference Include="Microsoft.Extensions.Configuration.Abstractions, Version=2.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60, processorArchitecture=MSIL">
      <SpecificVersion>False</SpecificVersion>
      <HintPath>..\..\..\Program Files\dotnet\sdk\NuGetFallbackFolder\microsoft.extensions.configuration.abstractions\2.0.0\lib\netstandard2.0\Microsoft.Extensions.Configuration.Abstractions.dll</HintPath>
    </Reference>
    <Reference Include="Microsoft.Extensions.Configuration.CommandLine">
      <HintPath>..\..\..\Program Files\dotnet\sdk\NuGetFallbackFolder\microsoft.extensions.configuration.commandline\2.0.0\lib\netstandard2.0\Microsoft.Extensions.Configuration.CommandLine.dll</HintPath>
    </Reference>
    <Reference Include="Microsoft.Extensions.Configuration.EnvironmentVariables">
      <HintPath>..\..\..\Program Files\dotnet\sdk\NuGetFallbackFolder\microsoft.extensions.configuration.environmentvariables\2.0.0\lib\netstandard2.0\Microsoft.Extensions.Configuration.EnvironmentVariables.dll</HintPath>
    </Reference>
    <Reference Include="Microsoft.Extensions.Configuration.FileExtensions">
      <HintPath>..\..\..\Program Files\dotnet\sdk\NuGetFallbackFolder\microsoft.extensions.configuration.fileextensions\2.0.0\lib\netstandard2.0\Microsoft.Extensions.Configuration.FileExtensions.dll</HintPath>
    </Reference>
    <Reference Include="Microsoft.Extensions.Configuration.Json">
      <HintPath>..\..\..\Program Files\dotnet\sdk\NuGetFallbackFolder\microsoft.extensions.configuration.json\2.0.0\lib\netstandard2.0\Microsoft.Extensions.Configuration.Json.dll</HintPath>
    </Reference>
    <Reference Include="Microsoft.Extensions.Configuration.UserSecrets">
      <HintPath>..\..\..\Program Files\dotnet\sdk\NuGetFallbackFolder\microsoft.extensions.configuration.usersecrets\2.0.0\lib\netstandard2.0\Microsoft.Extensions.Configuration.UserSecrets.dll</HintPath>
    </Reference>
    <Reference Include="Microsoft.Extensions.DependencyInjection, Version=2.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60, processorArchitecture=MSIL">
      <SpecificVersion>False</SpecificVersion>
      <HintPath>..\..\..\Program Files\dotnet\sdk\NuGetFallbackFolder\microsoft.extensions.dependencyinjection\2.0.0\lib\netstandard2.0\Microsoft.Extensions.DependencyInjection.dll</HintPath>
    </Reference>
    <Reference Include="Microsoft.Extensions.DependencyInjection.Abstractions, Version=2.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60, processorArchitecture=MSIL">
      <SpecificVersion>False</SpecificVersion>
      <HintPath>..\..\..\Program Files\dotnet\sdk\NuGetFallbackFolder\microsoft.extensions.dependencyinjection.abstractions\2.0.0\lib\netstandard2.0\Microsoft.Extensions.DependencyInjection.Abstractions.dll</HintPath>
    </Reference>
    <Reference Include="Microsoft.Extensions.Logging">
      <HintPath>..\..\..\Program Files\dotnet\sdk\NuGetFallbackFolder\microsoft.extensions.logging\2.0.0\lib\netstandard2.0\Microsoft.Extensions.Logging.dll</HintPath>
    </Reference>
    <Reference Include="Microsoft.Extensions.Logging.Configuration">
      <HintPath>..\..\..\Program Files\dotnet\sdk\NuGetFallbackFolder\microsoft.extensions.logging.configuration\2.0.0\lib\netstandard2.0\Microsoft.Extensions.Logging.Configuration.dll</HintPath>
    </Reference>
    <Reference Include="Microsoft.Extensions.Logging.Console">
      <HintPath>..\..\..\Program Files\dotnet\sdk\NuGetFallbackFolder\microsoft.extensions.logging.console\2.0.0\lib\netstandard2.0\Microsoft.Extensions.Logging.Console.dll</HintPath>
    </Reference>
    <Reference Include="Microsoft.Extensions.Logging.Debug">
      <HintPath>..\..\..\Program Files\dotnet\sdk\NuGetFallbackFolder\microsoft.extensions.logging.debug\2.0.0\lib\netstandard2.0\Microsoft.Extensions.Logging.Debug.dll</HintPath>
    </Reference>
    <Reference Include="Microsoft.Extensions.Options.ConfigurationExtensions">
      <HintPath>..\..\..\Program Files\dotnet\sdk\NuGetFallbackFolder\microsoft.extensions.options.configurationextensions\2.0.0\lib\netstandard2.0\Microsoft.Extensions.Options.ConfigurationExtensions.dll</HintPath>
    </Reference>
    <Reference Include="System" />
    <Reference Include="System.ComponentModel.Annotations">
      <HintPath>..\..\..\Program Files\dotnet\sdk\NuGetFallbackFolder\system.componentmodel.annotations\4.4.0\lib\net461\System.ComponentModel.Annotations.dll</HintPath>
    </Reference>
6 years ago
Thank you very much! I will try this out and let you know how it goes! Mine's for a console app, so if anything is significantly different, I'll share my findings.
6 years ago
Very nice! Thanks for this! I have a Data Load nightly and sometimes the schedule task in Nop fails
6 years ago
Any way you could create a "startup" program with all this already in it? I'm having a hard time following how to do it.
6 years ago
Got it to run! Now trying to figure out how to use it LOL. can I use my plugins?
6 years ago
Here is an error I get on this line of code when I run the console app


var orderServiceService = container.Resolve<IOrderService>();


{"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:\r\nCannot 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.Core.Data.IRepository`1[Nop.Core.Domain.Orders.ReturnRequest], Nop.Services.Events.IEventPublisher)'."}


        static void Main(string[] args)
        {

            EngineContext.Initialize(false);

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

            //Resolving inteface with autofac
            var container = builder.Build();
            var orderServiceService = container.Resolve<IOrderService>();
        }
6 years ago
Here is an error I get on this line of code when I run the console app


var orderServiceService = container.Resolve<IOrderService>();


"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:\r\nCannot 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.Core.Data.IRepository`1[Nop.Core.Domain.Orders.ReturnRequest], Nop.Services.Events.IEventPublisher)'."


        static void Main(string[] args)
        {

            EngineContext.Initialize(false);

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

            //Resolving inteface with autofac
            var container = builder.Build();
            var orderServiceService = container.Resolve<IOrderService>();
        }
6 years ago
treesap wrote:
Thank you very much! I will try this out and let you know how it goes! Mine's for a console app, so if anything is significantly different, I'll share my findings.


Hello everyone! Did you get it to work?

Just joined the discussion. Trying to implement EngineContextInitializer in a console app as well.

I got it to compile by adding the extensions but now I am getting the following exception:

System.InvalidOperationException: 'Unable to resolve service for type 'Microsoft.AspNetCore.Hosting.Internal.HostingEnvironment' while attempting to activate 'SpaniasNopSync.Startup'.'

When trying to execute this line:
var host = WebHost.CreateDefaultBuilder(args)
            .UseKestrel(options => options.AddServerHeader = false)
            .UseStartup<Startup>()
            .Build();

Any ideas what I may be going wrong?

Startup.cs has been copied from Nop.Web and moved to my namespace.
6 years ago
Nevermind!

I found the problem... Somewhere in my modifying I changed my Startup.cs and instead if IHostingEnvironment I was using HostingEnvironment which lives in Microsoft.AspNetCore.Hosting.Internal.
5 years ago
Anyone have a sample winform project ?
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.