Skip to content

Commit

Permalink
add SignumInitializeFilterAttribute
Browse files Browse the repository at this point in the history
  • Loading branch information
olmobrutall committed Sep 8, 2020
1 parent b0e49b3 commit 8af868d
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
1 change: 1 addition & 0 deletions Signum.React/Facades/SignumServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ public static MvcNewtonsoftJsonOptions AddSignumJsonConverters(this MvcNewtonsof

public static MvcOptions AddSignumGlobalFilters(this MvcOptions options)
{
options.Filters.Add(new SignumInitializeFilterAttribute());
options.Filters.Add(new SignumExceptionFilterAttribute());
options.Filters.Add(new CleanThreadContextAndAssertFilter());
options.Filters.Add(new SignumEnableBufferingFilter());
Expand Down
24 changes: 24 additions & 0 deletions Signum.React/Filters/SignumExceptionFilterAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -154,4 +154,28 @@ public HttpError(Exception e, bool includeErrorDetails = true)
public string? StackTrace;
public HttpError? InnerException;
}

public class SignumInitializeFilterAttribute : IAsyncResourceFilter
{
public static Action InitializeDatabase = () => throw new InvalidOperationException("SignumInitializeFilterAttribute.InitializeDatabase should be set in Startup");
static object lockKey = new object();
public bool Initialized = false;

public Task OnResourceExecutionAsync(ResourceExecutingContext context, ResourceExecutionDelegate next)
{
if (!Initialized)
{
lock (lockKey)
{
if (!Initialized)
{
InitializeDatabase();
Initialized = true;
}
}
}

return next();
}
}
}

2 comments on commit 8af868d

@olmobrutall
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adding SignumInitializeFilterAttribute

This small commit fixes an issue since .Net Core migration: If an application crashes on startup the next request won't try to start it again.

Here are some open issues about this, but doesn't look like there will be an alternative where you can use InProcess, get the exception message printed and still get retry functionality as it was possible before .Net Core.

dotnet/aspnetcore#9202
https://stackoverflow.com/questions/62169080/asp-net-core-3-1-inprocess-hosted-app-not-restarting-after-exception-on-startup

This is not a pressing issue for people using EntityFramework, because there the schema/context is created the first time access the database.

But in Sigunm Framework the starter needs to be called manually before you make any database access, and if the database is not available or is not a perfect match (not synchronized yet) you get an exception and you need to manually restart the application... till now.

With this change, all the request try to execute an Initialization lambda if not executed yet (or there was an exception). In the Starter, you need to delay all the code that requires a database access to this lambda... and that's it.

Check this commit to see how to migrate: signumsoftware/southwind@9e76bdc

Also take into account that maybe you need to stop using SqlServerVersionDetector.Detect / PostgresVersionDetector.Detect in the web application because it requires a database access.

Cheers!

@rezanos
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perfect!

Please sign in to comment.