Email Differentiating two environments in the system

Asked

Viewed 88 times

1

Personal I need a help, in my system I have two Environments, one of production and the other of homologation. So in my system, I have a part that sends email, to the user. Could someone help me so that when sending the email, the system idenficiar the environment and signal with the name [HOMOLOGATION] - [PRODUCTION] I will post below my controller.

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 in the message Control of Basics Fluids before the word Control turn up [HOMOLOGATION] Control of Basics fluids or [PRODUCTION] Control of Basics Fluids
I found an example on the internet only that I could not implement in my system.

var titulo = "zzzz";
if (Request.Url.Scheme.Contains("homolog"))
{
 titulo = "[HOMOLOGAÇÃO] " + titulo;
}

2 answers

2

A solution I often see for this type of problem is to put a key in the web.config.

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

Then you take it and use it however you want:

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

In the web.config you have flexibility to change the variable without needing DLL and there are ways to synchronize with production like Web.Debug.config and Web.Release.config

UPDATING

The System.Configuration.ConfigurationManager.AppSettings["ambiente"] returns the value of keyenvironment that is in web.config, then in the type approval environment that key up to a value, in the production value other than.

You’ll get her worth like this: var amb = System.Configuration.ConfigurationManager.AppSettings["ambiente"]

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

  • i kind of understood, could explain me again on how to use System.Configuration.Configurationmanager.Appsettings["environment"], where to use

  • I updated the answer, see if you understand cool now.

2

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

First of all, we need to create two new configurations: HOMOLOGATION and PRODUCTION as follows:

  • Click Build -> Configuration Manager
  • Click on New
  • Add Name (Homologation) 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 the application’s Web.Config and select Add Config Transform
  • A new Web file will be automatically created.Homologacao.Config

Repeat the same process above to create the PRODUCTION environment.

This way, we can create a web.config for every environment you’re working, all of this automatically.

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

Web.config

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

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

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;

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

Follow an example site for you to follow: http://www.leandroprado.com.br/2012/03/como-configurar-web-config-transform/

  • No if I mentioned in the topic the Request of an error 'The name 'Request' does not exist in the Current context.

  • You are calling Request in which file?

  • Basics.Project Basics.Infra Email.Cs

  • Is the project you’re calling a Classlibrary? Positive case you will get this error even a Vz you are not in any web context to get the value of Request!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.