Step 8: Add a custom configuration
In this step, we will add a custom configuration setting for the tutorial host to control the default credit balance returned in the getdataobject service operation. Again, this is just an example. The configuration setting is application.ProgramThey can also use their specific settings. For example, they can implement the settings in the <etettings> Configuration section of web.configor .exe. config. In this example, the setting will:
·Stored in the configuration storage database.
·Database-based Centralized Management ensures that all running nodes and newly added nodes have the same set value.
·You can configure the website configweb for updates.
·You do not need to redeploy the configuration file or stop/restart the host process on the running node to achieve dynamic updates. These operations are required if the configuration settings to be updated are stored in the configuration file.
Add settings
1. Open the Visual Studio tutorialhost solution and open the settings. CS file in the tutorialsettings project of the solution.
2. You will add a new setting, default_credit_balance, using the following lineCode:
Public Static Decimal Default_credit_balance;
3. Save the file. The file you have completed looks as follows:
4. recompile and press F5 to start your host application.
5. log on to your host Program in the configuration website (localadmin, password YYY), and configure the service endpoint address http: // localhost: 8001/tutorial/config.
6. Select the edit configuration link marked in the red circle above.
7. Click the define/edit keys button marked in the red circle above.
8. Click Add key.
9. Enter the following values for the newly added keywords:
Configuration key display name:Default customer credit balance (you can enter any value you want)
Settings class field name:Default_credit_balance (must match the field name added to the settings class)
Settings group name:Settings group 1 (it can be any value you want; deselect the select from existing groups option box and you can create your own group name)
Data Type:Decimal
Key description:The default customer credit balance
Key valid values:Any decimal
Key level:Basic
Read only:False
Initial key value:50.25
Display order:0
10. Click Add.
11. Click the configuration menu button in the main menu navigation bar.
You can see that the new setting group settings group 1 has been added, and you can adjust these newly added settings through the edit configuration link of this group.
By configuring the link of the define/edit keys button of the website, you can add other settings in the same way or define the new setting group you want. You can also reassign the membership groups and levels of these configuration settings.
At the same time, when setting the specified values for these configurations, the system will verify the default type based on the original type in the settings class. Of course, you can also add custom verification logic (these verification logic will be executed before being updated to the configuration repository). Later, if the set value is modified, the system can execute the custom verification logic (executed across all peer nodes ). You can use the configurationactions class to add the verification and Configuration action Action logic you need. For details, see the Configuration Service 2.0 technical guide. In this example, the basic verification logic is sufficient, so we do not need to add other logic when modifying it. When the modification happens, the configuration service automatically updates the value stored in the memory of all online running nodes, and updates the value in the configuration storage database. Therefore, no additional special settings are required.
Use this setting in the tutorialserviceimplementation Project
Currently, we have a setting definition, so we will add the logic to use this setting in the tutorialserviceimplementation project.
1. Open the tutorialserviceimplementation project and open the tutorialservice. CS file in the Visual Studio editor.
2. Add a reference to the tutorialsettings project in the project.
3. Add the Configservice. configurationservicebase. dll assembly to the project. The Assembly is under the [stocktrader setup Directory] \ sharedlibraries \ configuration directory.
4. Modify the tutorialservice implementation class as follows. The newly added or modified code is displayed in bold:
Using System;
Using System. Collections. Generic;
Using System. text;
Using Tutorial. datacontract;
Using Tutorial. servicecontract;
Using
Tutorial. settings;
NamespaceTutorial. serviceimplementation
{
Public ClassTutorialservice: itutorialservice
{
Public VoidIsonline ()
{
Return;
}
Public Tutorialdataobject getdataobject ( Int Customerid)
{
Tutorialdataobject myobject = New Tutorialdataobject (
Customerid,
" CUSTOMER NAME " ,
Settings. settings. default_credit_balance);
Return Myobject;
}
}
}
Now, you do not need to use a hard-coded value. When the client accesses the getdataobject method, the current default_credit_balance value is returned.
5. Re-compile the project and it will be OK.