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

264
Views
How to make an existing code block async in .net?

I have an existing code block with Linq to SQL queries and HTTP requests, that I'd like to make async for the purpose of using less threads.

Will it suffice to put the code block in an async method, like so?

    public async Task<Customer> ProcessACustomer()
    {
        return await GetCustomer();
    }

    public Task<Customer> GetCustomer()
    {
        // Linq to SQL query here

        // HTTP request here

        Customer customer;
        return Task.FromResult<Customer>(customer);
    }

...or do I have to make every piece of logic in GetCustomer() async to accomplish this?

My hurdle here is that I have a ton of logic in that method (oversimplified above), so time will be an issue. Also, I can't seem to convert my Linq To SQL queries to async, as the async extension methods are not available for some reason (System.Data.Linq.Table does not contain a definition for FirstOrDefaultAsync() f.ex.).

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Will it suffice to put the code block in an async method, like so?

No. The GetCustomer method is not asynchronous. It synchronously performs its work and then returns a completed task. This will not save any threads.

do I have to make every piece of logic in GetCustomer() async to accomplish this?

If you want to convert to async, then you should start at the opposite end. Don't start with the goal of making ProcessACustomer (or GetCustomer) asynchronous. Instead, start with the lowest-level API. Whatever your db access method is, make that asynchronous first and then let the async grow out from there.

My hurdle here is that I have a ton of logic in that method (oversimplified above), so time will be an issue.

This is a classic tradeoff. It may be worthwhile to convert to async, or it may be worthwhile to buy a few more servers instead.

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!