Email Differentiating two environments in the system

1

Personal I need a help, in my system I have two Environments, being one of production and another of homologation. So being on my system, I have a part that sends email, to the user. Could someone help me in order to send the email, the system to identify the environment and signal with the name [APPROVAL] - [PRODUCTION] I'll post my controller below.

public void EnviaAlertaBaixoEstoque(Centro hospital, int tipo)
        {
            var fluidos = new List<string> { "", "A", "B", "C", "D", "E", "F" };
            var objEmail = FactoryMailMessage(MailPriority.High);

            objEmail.To.Add("Leonardo Macedo <[email protected]>");

            objEmail.Subject = $"Controle de Fluídos BaSICS - Aviso de estoque mínimo de fluídos {fluidos[tipo]} no {hospital.CentroId} - {hospital.Nome}";
            var fluido = hospital.EstoqueA;

            switch (tipo)
            {
                case 2:
                    fluido = hospital.EstoqueB;
                    break;

                case 3:
                    fluido = hospital.EstoqueC;
                    break;

                case 4:
                    fluido = hospital.EstoqueD;
                    break;

                case 5:
                    fluido = hospital.EstoqueE;
                    break;

                case 6:
                    fluido = hospital.EstoqueF;
                    break;
            }

            objEmail.Body = $"O {hospital.CentroId} - {hospital.Nome} está com estoque de fluídos {fluidos[tipo]} em {fluido} litro(s).";
            EnviarEmail(objEmail);
        }


Summarizing the BaSICS Fluid Control message before Control appears [APPROVAL] BaSICS Fluid Control or [PRODUCTION] > Fluid Control BaSICS
I found an example on the internet only that I could not implement on my system.

var titulo = "zzzz";
if (Request.Url.Scheme.Contains("homolog"))
{
 titulo = "[HOMOLOGAÇÃO] " + titulo;
}
    
asked by anonymous 04.08.2017 / 16:56

2 answers

2

The best option for you to solve your problem is to work with Web Config Transformations, that is, you will have a Web.Config file - > Web.Producao.config - > Web.Homologa.config.

First of all, we need to create two new settings: HOMOLOGACAO and PRODUCAO as follows:

  • Click Build - > Configuration Manager
  • Click New
  • Add the Name and select Copy Settings from to Empty and click OK
  • Now we have to create the Web.Config for our new configuration, right click on Web.Config of the application and select Add Config Transform
  • Automatically create a new Web.Homologacao.Config file

Repeat the same process above to create the PRODUCAO environment.

In this way, we were able to create a web.config for each environment that you are working on, all of this automatically.

Within the Web.config file you create a section to include global parameters that can be used on your system, eg

Web.config

  <appSettings>
    <add key="Ambiente" value="DESENVOLVIMENTO" />
  </appSettings>

When you do the Deploy / Publish of your application, theoretically you will define the way in which you will do the operation in your case we will have HOMOLOGACAO and PRODUCAO. Automatically when you do this, the Web.config file will have the parameters changed as per our Web.Producao.config or Web.Homologacao.config files, Ex:

Web.Producao.config

<?xml version="1.0" encoding="utf-8"?>
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
  <appSettings>
    <add key="Ambiente" value="PRODUCAO"  xdt:Transform="Replace" xdt:Locator="Match(key)"/>    
  </appSettings> 
  <system.web>
    <compilation xdt:Transform="RemoveAttributes(debug)" />
  </system.web>
</configuration>

Web.Homologacao.config

<?xml version="1.0" encoding="utf-8"?>
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
  <appSettings>
    <add key="Ambiente" value="HOMOLOGACAO"  xdt:Transform="Replace" xdt:Locator="Match(key)"/>    
  </appSettings> 
  <system.web>
    <compilation xdt:Transform="RemoveAttributes(debug)" />
  </system.web>
</configuration>

Within your code, you will call the environment as follows:

 titulo = System.Configuration.ConfigurationManager.AppSettings["Ambiente"] + titulo;

In this way you will have a Web.config for each environment with its specific parameters.

Here is a sample website for you to follow: link

    
04.08.2017 / 17:42
2

One solution I usually see for this kind of problem is to put a key in web.config .

<appSettings> <add key="ambiente" value="HOMOLOGAÇÃO" /> ... </appSettings>

Then you get and use whatever you want:

System.Configuration.ConfigurationManager.AppSettings["ambiente"]

No web.config you have the flexibility to change the variable without requiring a DLL and there are ways to synchronize with production as Web.Debug.config and Web.Release.config

UPDATING

The System.Configuration.ConfigurationManager.AppSettings["ambiente"] returns the value of the key environment that is in web.config , then in the environment of homologation this key up to a value, in the production of another value.

You'll get the value of it like this: var amb = System.Configuration.ConfigurationManager.AppSettings["ambiente"]

The variable amb will have the value that key environment has in web.config , from there you can implement the rule that you think is best.

    
04.08.2017 / 17:08