Need help accessing items that used to be in web.config

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
5 years ago
Hi all,

I am getting very frustrated and m trying to figure out how I can duplicate functionality in the V4.0 nopcommerce that I have in both V3.4 and V3.8.

I want to add information to a table I have added to the nopcommerce DB to store additional information, but I can't figure out how to get the connection string to the DB so I can execute the SQL statement to add the info.

Also, I want to be able to call an API to load data that was placed after an order, but again, don't know how to access the values I need for the API Address. I have both pieces of information stored within the Nop section of appsettings.json, but how do I get to them?

This is all new to me (.net Core 2.0) so any help would be greatly appreciated.

Thanks in advance.

Jon
5 years ago
How did you go  ?

Just having a quick look

"DisplayFullErrorStack": false is one of the settings
I searched for how it is used
var nopConfig = EngineContext.Current.Resolve<NopConfig>();
nopConfig.DisplayFullErrorStack

You'd need to do something similar to this to access a setting

Alternatively database settings are in dataSettings.json
"DataProvider": "sqlserver"
Use via dataSettings.DataProvider;

But to add new table in existing database I saw these posts
https://www.nopcommerce.com/boards/t/48078/add-new-table-to-database.aspx
or
http://mycodingerrors.blogspot.com/2012/06/how-to-add-new-tables-to-nopcommerce.html
5 years ago
Hi there,

thanks for the response,. I tried to use your logic as follows:

I have in my appsettings.json as follows:

{
  "AppSettings": {

    "ConnectionStrings": { // used to connect directly to the nop DB and to specify the api to call
      "CIInopDirectConnect": "data source=chi-ecom-stage.cnu35ta8uyfo.us-east-1.rds.amazonaws.com;initial catalog=nopCommerce;Persist Security Info=True;User ID=jgarniss;Password=Diamonds1",
      "CIInopDirectConnectTEST": "data source=chi-ecom-stage.cnu35ta8uyfo.us-east-1.rds.amazonaws.com;initial catalog=nopCommerce;Persist Security Info=True;User ID=jgarniss;Password=Diamonds1",
      "CIInopDirectConnectPRODUCTION": "data source=chi-ecommerce.cnu35ta8uyfo.us-east-1.rds.amazonaws.com;initial catalog=nopCommerce;Persist Security Info=True;User ID=jgarniss;Password=Jg2015"
    },
    "APIConnections": {
      "CIIRegistrationLOCAL": "http://localhost:59168/",
      "CIIRegistrationSTAGE": "https://services.knowledgefoundation.com/devChiExternalWebApi/api/",
      "CIIRegistrationPRODUCTION": "https://services.knowledgefoundation.com/ChiExternalWebApi/api/"
      }
      
    },

  "Hosting": {
.........


            string connectionString = "";
            var CIInopDirectConnectionString = EngineContext.Current.Resolve<AppSettings>();

            connectionString = CIInopDirectConnectionString.CIInopDirectConnect.ToString();

But when I get to the code I get the following issues:

{"The requested service 'Nop.Web.Controllers.CheckoutController+AppSettings' 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."}

I don't understand what the issue is? This should be so simple. Every article I see on being able to read from the appsettings.json file is different from nopCommerce's use of startup.js.

How do I get through this? I'm really confused.

Thanks in advance,
Jon
5 years ago
You can't .....Resolve<AppSettings>, because AppSettings is not a service nopCommerce provides.  It's an ASP.NET thing, and if you really want to store your settings there, then see this article on Configuration in ASP.NET Core.

Having said that, it's not the typical way settings are stored in nopCommerce.  You did not mention whether you made a plugin, or if you are modifying the core code.  If you look at the source code for most any out-of-the-box plugin in nopCommerce, you will see that settings are set up as a POCO, and the Settings service allows reading writing them.  They are store in the Settings table in the database.

Alternatively, you could always just create your own JSON style settings file, and use methods of Json.NET - Newtonsoft (which is included / used by the core nopCommerce), and deserialize the JSON file to a POCO.
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.