Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

177
Visualizações
"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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda