Starting with NopCommerce 4.0, when Nop moved to ASP.NET core, it seems like the Nop team gave up completely on keeping concerns separated. Web stuff is now baked into the core libraries. A big shame if you ask me.
Good luck writing a console app. I tried and gave up.
Instead what I did was stripped down the Nop.Web project to the absolute bare minimum and create my own controllers for basic tasks. This gives me the ability to programmatically add products, attributes, and things like that, which saves me quite a bit of time. But it's a dirty hack, and not something I'd put into production.
Also worth mentioning that, even if you were able to get the nob libraries working in a console app, certain things will never work since they rely on the filesystem (for example, product images).
The better way to do this would be to write a plugin to provide API endpoints, and then call those endpoints from your console app.
Sorry it's taken so long to reply to your message. Not had the chance to respond until now.
I agree though. It is a shame that some of the web functionality is becoming integrated into the service layer rather than being kept separate.
I did manage to get the console application working. You are right though about certain services requiring knowledge of the web file system. And it is these services that make it harder to setup the console application.
In a nutshell, to set up the console application to use the services and repositories I had to:
1. Create /appsettings.json
files in the console application.
2. Create an instance of IConfigurationRoot
3. Register my own instance of IHostingEnvironment
setting the ContentRootPath to the current directory and the WebRootPath to a dummy /wwwroot directory in my console application. The dummy /wwwroot path also had to have a placeholder.txt file so that it would be copied to the output directory as the service checks for the presence of this file. Nice!
4. Register a blank instance of IApplicationLifetime
which is now required in the core WebHelper
service since the release of version 4.20.
5. Call nopcommerce ServiceClassExtensions.ConfigureApplicationServices(IConfiguration, IHostingEnvironment)
to register the application services.
At this point the application is set up (at least for my usage).
6. Resolve an instance of the service you require in nopcommerce using:
var service = EngineContext.Current.Resolve<INOPCOMMERCESERVICE>();
If anyone is interested in the actual details of the code and structure of this console application then please let me know and I can create a post describing how I set it up. But as 'timmit' has already mentioned, certain things may not work (such as the file system).