I've been working on an integration project between nopCommerce 2.4 and an ERP system. So far we've defined communication information between them, and we are going to start development soon.
I'd like to know the best approach to create a windows services / console application that is going to consume nopCommerce services (orderservice, customerservice, etc).
I had a problem to create a console application, the update method was not working at all, but the insert was working normally. So I made some changes to make it work.
File: Presentation\ISG.Web.Framework\DependencyRegistrar.cs Method: public virtual void Register(ContainerBuilder builder, ITypeFinder typeFinder)
bool isConsoleApplication = false;
if (ConfigurationManager.AppSettings["IsConsoleApplication"] != null) bool.TryParse(ConfigurationManager.AppSettings["IsConsoleApplication"], out isConsoleApplication);
if (dataProviderSettings != null && dataProviderSettings.IsValid()) { var efDataProviderManager = new EfDataProviderManager(dataSettingsManager.LoadSettings()); var dataProvider = (IEfDataProvider)efDataProviderManager.LoadDataProvider(); dataProvider.InitConnectionFactory();
if (isConsoleApplication) builder.Register<IDbContext>(c => new IsgObjectContext(dataProviderSettings.DataConnectionString)).SingleInstance(); else builder.Register<IDbContext>(c => new IsgObjectContext(dataProviderSettings.DataConnectionString)).InstancePerHttpRequest(); } else { if (isConsoleApplication) builder.Register<IDbContext>(c => new IsgObjectContext(dataSettingsManager.LoadSettings().DataConnectionString)).SingleInstance(); else builder.Register<IDbContext>(c => new IsgObjectContext(dataSettingsManager.LoadSettings().DataConnectionString)).InstancePerHttpRequest(); }
I also had the same problem with ITask Services too - because the HttpContext.Current is null.
My work around to make it work in threads was to add this peace of code to initialize the HttpContext:
HttpContext.Current = new HttpContext(new HttpRequest(string.Empty, "http://localhost/", string.Empty), new HttpResponse(new System.IO.StringWriter(CultureInfo.InvariantCulture))); System.Web.SessionState.SessionStateUtility.AddHttpSessionStateToContext(HttpContext.Current, new HttpSessionStateContainer(string.Empty, new SessionStateItemCollection(), new HttpStaticObjectsCollection(), 20000, true, HttpCookieMode.UseCookies, SessionStateMode.Off, false));
and then resolve the Service Classes manually:
var userService = EngineContext.Current.Resolve<IUserService>(); var customerService = EngineContext.Current.Resolve<ICustomerService>();
var user = userService.GetAllUsers()[0]; var owner = user.Customer;