Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

175
Views
"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 answers
Answer question

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 Report

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!