0

I have an asp net core api.

I want to run a function only once after a publish will convert and resize all images to a more lightweight version.

The problem I have is I want this function to run only once. I can't find a way to do that.

Luc
  • 59
  • 7
  • Possibly related: https://stackoverflow.com/q/13781799/1220550. Some answers are quite old, others are more recent. And I have no idea if any of it would apply for .Net Core. – Peter B Oct 02 '18 at 21:46
  • Not what I was in mind. I want to call a function from my program only once after publish, or only the first time its run. It could be a way to access it from outside. What I've done to create a maintenance controller and calling it there.... But well, seems ugly – Luc Oct 03 '18 at 04:29

1 Answers1

0

If you want to call function from your program, it could not be achieved by cutsom publish process.

For a workaround, you may try implement the requirement like seeding data to database.

var host = BuildWebHost(args);

using (var scope = host.Services.CreateScope())
{
var services = scope.ServiceProvider;

try
{
    //check whether the images has been resized
    //if not, call function to resize.
}
catch (Exception ex)
{
    var logger = services.GetRequiredService<ILogger<Program>>();
    logger.LogError(ex, "An error occurred seeding the DB.");
}
}

host.Run();

Or, you could try implement a middleware to check whether the images has been resized.

Edward
  • 28,296
  • 11
  • 76
  • 121