Import on startup
How to import content and schema on startup and implement your own `IArtifactImportOnStartupProvider`
Default configuration
Implementing your own IArtifactImportOnStartupProvider
IArtifactImportOnStartupProviderusing Umbraco.Cms.Core;
using Umbraco.Cms.Core.Composing;
using Umbraco.Cms.Core.Extensions;
using Umbraco.Deploy.Core;
using Umbraco.Deploy.Core.OperationStatus;
using Umbraco.Deploy.Infrastructure.Extensions;
internal sealed class DeployImportOnStartupComposer : IComposer
{
public void Compose(IUmbracoBuilder builder)
=> builder.DeployArtifactImportOnStartupProviders()
.Append<PhysicalDirectoryArtifactImportOnStartupProvider>();
private sealed class PhysicalDirectoryArtifactImportOnStartupProvider : IArtifactImportOnStartupProvider
{
private readonly IArtifactImportExportService _artifactImportExportService;
private readonly ILogger _logger;
private readonly string _artifactsPath;
public PhysicalDirectoryArtifactImportOnStartupProvider(IArtifactImportExportService artifactImportExportService, ILogger<PhysicalDirectoryArtifactImportOnStartupProvider> logger, IHostEnvironment hostEnvironment)
{
_artifactImportExportService = artifactImportExportService;
_logger = logger;
_artifactsPath = hostEnvironment.MapPathContentRoot("~/umbraco/Deploy/ImportOnStartup");
}
public Task<bool> CanImportAsync(CancellationToken cancellationToken = default)
=> Task.FromResult(Directory.Exists(_artifactsPath));
public async Task<Attempt<ImportArtifactsOperationStatus>> ImportAsync(CancellationToken cancellationToken = default)
{
_logger.LogInformation("Importing Umbraco content and/or schema import at startup from directory {FilePath}.", _artifactsPath);
Attempt<ImportArtifactsOperationStatus> attempt = await _artifactImportExportService.ImportArtifactsAsync(_artifactsPath, default, null, cancellationToken);
_logger.LogInformation("Imported Umbraco content and/or schema import at startup from directory {FilePath} with status: {OperationStatus}.", _artifactsPath, attempt.Result);
if (attempt.Success)
{
Directory.Delete(_artifactsPath, true);
_logger.LogInformation("Deleted physical directory after successful import on startup {FilePath}.", _artifactsPath);
}
return attempt;
}
}
}Last updated
Was this helpful?