Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

178
Vistas
"mocking" a class when in development

I have this problem where I need to make an external API call, however when in production I have to use a certificate for the API call and in development I don't really need one. I was wondering how to implement a solution where I can have separation between production code and development code so I don't have to comment the part out and possible forget to change it. Any tips would be appreciated!

public async Task<GetPersonResponse> PostPerson(PersonDto dto) 
{
    try {
        var httpHandler = new HttpClientHandler();

        var certificate = new X509Certificate2(
            Path.Join(_certOptions.Path, _certOptions.Name),
            _certOptions.Password, 
            X509KeyStorageFlags.MachineKeySet);

        httpHandler.ClientCertificates.Add(certificate);

        var client = new HttpClient(httpHandler);

        var soapRequest = GenerateRequest(dto);

        var request = new HttpRequestMessage() {
            RequestUri = new Uri(_options.Value.URL),
            Method = HttpMethod.Post,
            Content = new StringContent(soapRequest.ToString(), Encoding.UTF8, "text/xml"),
        };

        var response = await client.SendAsync(request);
    }
}

over 4 years ago · Santiago Trujillo
3 Respuestas
Responde la pregunta

0

Well couple things would help. First you should look at HttpClientFactory which would let you pass in the httpclient and have it already configured correctly - right now you're leaking sockets with the code you showed.

For your more general question of how to have the code handle changing behavior based on environment there are two pretty common approaches.

First is to abstract out the behavior into one interface and implementations for each environment. Then change the implementation based on the environment. So with your web app you would register a specific implementation based on the runtime environment. Then your controller would get the interface in it's constructor.

The second would be to pass in a configuration element that had the runtime info and then conditionally execute the code. In that case the controller would take the configuration element in the constructor.

I would avoid using conditional compilation as that's much harder to test.

over 4 years ago · Santiago Trujillo Denunciar

0

You could use conditional preprocessors #if #else #endif like that:

#if DEBUG
    Console.WriteLine("Code will executed only in a debug configuration");
#else
    Console.WriteLine("Executed if not in a debug configuration");
#endif
over 4 years ago · Santiago Trujillo Denunciar

0

You already have conditions in your code:

_options.Value.URL

Would it be a problem to create a _options.Value.UseCertificate and put an if around your certificate code?

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda