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

6 年 前
Here is how:

1. Create your project (example is for windows forms)
2. Install from nuget: Autofac, Autofac.Integration.Mvc, EntityFramework.
3. Add references to: Nop.Core, Nop.Data, Nop.Services, Nop.Web.Framework (look update at the bottom).
4. ASP.NET MVC, ASP.NET Razor, ASP.NET Web Pages, Microsoft.Web.Infrastructure should be installed also.
5. Then go to your program.cs file and add:

using Nop.Core.Infrastructure;

6. Add in Main():


7. Go to your App.config file.
<section name="NopConfig" type="Nop.Core.Configuration.NopConfig, Nop.Core" requirePermission="false" />
in <configSections>.
    <DynamicDiscovery Enabled="true" />
    <Engine Type="" />
    <Themes basePath="~/Themes/" />
after <configSections>.

Now EngineContext should initialize without exceptions. And so all services should register.

Then you use services like this anywhere in your program:

IProductService ps = EngineContext.Current.Resolve<IProductService>();

8. Forgot:
You should also create App_Data folder and move settings.txt there to have access to your database.

Also update: Better add existing projects Nop.Core, Nop.Data, Nop.Services, Nop.Web.Framework and references to them in your program than just references to already compiled dll libraries because their compilation will also cause needed packages for them to be copied to debug/release directory. Without them EngineContext will not initialize.
6 年 前
Thanks for the useful tips.
I'd add that if you want to use your own EF datacontext along with the NOP one you wont register it as you would with a plugin using RegisterPluginDataContext but :

//here is the registration for the NopObjectContext, this way you can avoid to rely on the settings.txt file and get the //connectionstring from somewhere else
builder.Register<IDbContext>(c => new NopObjectContext(nopConnectionString)).InstancePerLifetimeScope();

//here is your datacontext registration
builder.Register(c => new YourDataContext(nopConnectionString)).Named<IDbContext>("nop_object_context_your_name_here").InstancePerLifetimeScope();

//you can now register the repositories that will use your named datacontext
5 年 前
Having trouble getting this to work. I think I'm close. I don't think it is finding the connection string because I am getting the following error: The argument 'nameOrConnectionString' cannot be null, empty or contain only white space.

This is coming from
        static void Main()
            IManufacturerService ms = EngineContext.Current.Resolve<IManufacturerService>();
5 年 前
Figured it out. For the MapPath was looking for the App_Data folder in my \bin\Debug\ folder of my windows forms app. I copied that App_Data folder to that directory and it works (I hope).
5 年 前
I think I found out a couple of nagging issues that I have.  For one thing, you have to make sure your Windows Forms project is .Net 4.5.1.  Second, if you get the error below, you may have to update System.Web.Mvc through NUGet (see: http://stackoverflow.com/questions/19491860/could-not-load-file-or-assembly-system-web-http-4-0-0-after-update-from-2012-to)

Additional information: Could not load file or assembly 'System.Web.Mvc, Version=, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)
4 年 前
I am getting error on this line while following your procedure.
            INewStoreService ps = EngineContext.Current.Resolve<INewStoreService>();""

3 年 前
This is the error I am getting on line:


The requested service 'Nop.Core.Data.DataSettings' has not been registered. To avoid this exception, either register a component to provide the service, check for service registration using IsRegistered(), or use the ResolveOptional() method to resolve an optional dependency.
3 年 前
There are a few changes in the startup with nopcommerce 4.0

Does someone know how or have the same tutorial for nopcommerce 4.0?
3 年 前
Can this be used to to make a windows service?
3 年 前
wydy wrote:
There are a few changes in the startup with nopcommerce 4.0

Does someone know how or have the same tutorial for nopcommerce 4.0?

Did you ever figure this out? I'm currently in the process of trying to do this on version 4.00.

The first change I made was to call
instead of
as this seems to be the correct replacement method.

Specifically, resolving a service triggers an exception in EngineContext.
IProductService ps = EngineContext.Current.Resolve<IProductService>();

The exception is System.ArgumentNullException occurred: 'Value cannot be null. Parameter name: provider'.

I've also tried to call the resolve method via the return from EngineService.Create():
var _engineInstance = EngineContext.Create();
IProductService ps = _engineInstance.Resolve<IProductService>();

Still, though, the same exception occurs. Any ideas?