Web.Config, Client.cfg

329 views
Skip to first unread message

Gabriel Medina

unread,
Sep 23, 2021, 3:30:43 PM9/23/21
to ggg (GeneXus Google Groups)
Desde el BLOG de Enrique Almeida


 Funcionalidades de GeneXus que conviene conocer: ConfigurationManager external object

El external object ConfigurationManager, viene nativo en el modulo GeneXus. 

Que se puede hacer con esto?

Es para poder recuperar un valor de una propiedad del client.cfg,  web.config o CloudServices.config
Por ejemplo, si queremos ver que formato de fecha se esa utilizando en el web.config, desde un programa, podemos hacer:

if ConfigurationManager.HasValue("DateFormat")
     Msg(ConfigurationManager.GetValue("DateFormat"),status)
endif


y la salida va a ser

DMY

pues en el web.config tengo la entrada

    <add key="DateFormat" value="DMY" />


Los métodos tienen un segundo parámetro opcional, que permite leer de un archivo de configuración especifico, por si queremos crearnos nuestro propio archivo de configuración. 

Es muy util para hacer objetos que dependan de características de los archivos de configuración. 

Usar este external object, evita el usar codigo java o .net para leer los archivos de configuración y hacen mucho mas facil  las futuras migraciones. 

Ademas la solucion GeneXus se puede pasar de .NET a Java sin tener que hacer adaptaciones, quedando mejor situada para ser multiplataforma. 
--
Saludos,
gab
@gxsoft

ConfigurationManager external object
Official Content
This documentation is valid for:

The ConfigurationManager external object allows you to read the application configuration at runtime.
This means that you have a way to access the configuration that is being used at runtime and has been set in environment variables (only the variables explained in this document) or configuration files (a.k.a. web.config, client.cfg., CloudServices.config).

pick1_2018727141336_1_pngpick2_201872714145_1_png

Methods

HasValue 

Returns true if the property has a value set.
The parameter filename is optional. If it is not set, a valid property is one that is set in the standard configuration files (web.config / client.cfg or CloudServices.config) or an environment variable.

Return valueBoolean data type
ParameterspropName:VarChar [  fileName:VarChar ]

GetValue

Returns the value set in the property.
The parameter filename is optional. If it is not set, the property must be one that is set in the standard configuration files (web.config / client.cfg or CloudServices.config) or an environment variable.

Return valueVarChar
ParameterspropName:VarChar [  fileName:VarChar ]

 

Notes

In both methods, the name of the property is instantiated as it is in the web.config or client.cfg. If you require a property of another file, either from the CloudServices.config file or from your own, the name of the property must be prefixed with the service type separated by a colon (:). eg: Notifications: APP_ID.

If a file is specified, the format of this file must be similar to the CloudServices.config and the way to choose the value of a property is the same as in the case of the CloudServices.config (<ServiceType>:<propName>)

As of GeneXus 16 Upgrade 7, services from the same type can appear more than once. That's the case of the Chatbot type where every property is saved under a Chatbot instance identified by the Conversational Flows name. So in that case you should get the property with the following format <ServiceType>:<ServiceName>:<propName>,

Sample Configuration file:

<?xml version="1.0" encoding="utf-8"?>
<Services>
  <Service>
    <Name>MyServiceName</Name>
    <Type>MyServiceType</Type>
    <ClassName>MyServiceClass</ClassName>
    <Properties>
      <Property>
        <Name>Property1</Name>
        <Value>Value of Property 1</Value>
      </Property>
      <Property>
        <Name>Property2</Name>
        <Value>Value of Property 2</Value>
      </Property>
    </Properties>
  </Service>
</Services>

To get the value of Property1, write the following:

&FileName = !"MyConfigFile.config"
&propName = !"MyServiceType:Property1"
if ConfigurationManager.HasValue(&propName, &FileName)
   msg(ConfigurationManager.GetValue(&propName, &FileName))
else
   msg("Property has not been set")
endif

Availability

Configuration Manager is available as of GeneXus 15 upgrade 12.

See also



Reply all
Reply to author
Forward
0 new messages