0

I am currently trying to make a request to a third party library in my ASP.NET Core 6 Web API and work with this data (it really has to run over a Web API template).

That means I'm importing data from another API.

Unfortunately I don't know how to tell my application to make a call. I would have expected the Task to run automatically when the application starts. Unfortunately this is not the case.

I first wanted to test whether this works at all when the application is started. Later, I would build in a scheduler, which sends requests accordingly.

It should be possible, right?

It would be great if someone could tell me as well if it's possible to put the URL "localhost:xxx/" in the constructor somehow, but still not get any dependency injection errors with AddScoped.

I use Flurl.Http to make Http Requests.

If it is important. My program.cs is in a console application and DataImport in an empty project

Unfortunately, I am relatively new to the ASP.NET world and I hope that the question is not too unprofessional. Otherwise I apologize. It's kind of hard to google for a problem like this and find something

using Flurl;
using Flurl.Http;

public class DataImport
{
    private readonly Service service;

    public DataImport(Servie service)
    {
        _service = service;
    }

    public async Task<IEnumerable<Data>> ImportData()
    {
        var data = await "localhost:xxx/".AppendPathSegment("data").GetJsonAsync<DataDto[]>();
        return _service.Add(data.Select(it => new DatoDtoToData(it)));
    }
}

program.cs:

builder
.Services
... Service Injections
....
.AddScoped<DataImport>()
1
  • Comments are not for extended discussion; this conversation has been moved to chat. Commented Jul 22, 2022 at 18:10

1 Answer 1

1

I would encourage you to look into Azure Functions.

Have a look at Microsoft's Introduction to Azure Functions. In the Scenarios section we can read.

The following are a common, but by no means exhaustive, set of scenarios for Azure Functions.

If you want to... then...
Build a web API Implement an endpoint for your web applications using the HTTP trigger
...
Build a serverless workflow Chain a series of functions together using durable functions
...
Run scheduled tasks Execute code on pre-defined timed intervals
...

I really think this could work well for you because:

  1. You can run/host it locally or host it in Azure.
  2. It's very easy to start with. Microsoft's tutorials are:
  3. It supports multiple ways of triggering your functions. There are over 20 tiggers; with 2 of the most relevant for you should be:

Sign up to request clarification or add additional context in comments.

4 Comments

nice thank you I see for the first time. Could this also be used to solve my problem explicitly, or is this a completely different approach?
I'm not sure what you are referring to when you say "completely different approach" and "explicitly"?
I can use it i a web api project as well, or?
Can you use it in a web api project? It may be possible but you're swimming against the current then and there isn't a reason to do it like that. Normally you would have a separate projects for functions. Note that a function with a http trigger is an api endpoint.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.