locked
How to use new version of DbContext assembly after old version is loaded RRS feed

  • Question

  • User-1262787652 posted

    Entity Framework Core NpgSql MVC application uses  Scaffolded DbContext to access data.

    DbContext is stored in separate assembly containing only this type.

    If database structure is changed , new DbContext assembly is created in runtime using https://github.com/jdtcn/RuntimeEfCore as in-memory assemly.

    This assembly can written to application home directory to replace existing DbContext.dll file.

    After file is replacing application is still using old dbContext.dll file loaded at startup. How to force application to use new DbContext.dll file ?

    How to force application to reload DbContext assembly so that new version is used?

    ASP.NET 4.8 MVC re-loads application automatically if source dll file is replace . Is there similar feature it .NET 5 ?

    Appilcation is running as Service in Debian Linux with Apache.

    Monday, February 8, 2021 2:23 PM

All replies

  • User1120430333 posted

    The Service would need to be recycled I would think to pick up using the new DLL.

    Monday, February 8, 2021 7:21 PM
  • User-1262787652 posted

    Debian /etc/systemd/system/kestrel-store.service contains

    # https://docs.microsoft.com/en-us/aspnet/core/host-and-deploy/linux-apache?view=aspnetcore-5.0
    [Unit]
    Description=My store5
    
    [Service]
    WorkingDirectory=/var/www/store5
    ExecStart=/var/www/store5/Store
    Restart=always
    # Restart service after 5 seconds if the dotnet service crashes:
    RestartSec=5
    KillSignal=SIGINT
    SyslogIdentifier=kestrel-store
    User=www-data
    Environment=ASPNETCORE_ENVIRONMENT=Development
    #Environment=ASPNETCORE_ENVIRONMENT=Production
    
    [Install]
    WantedBy=multi-user.target

    It looks like service is re-started automatically. 

    Shold all threads aborted so that Linux will re-start service or is there better way, e.q unloading AppDomain ?

    Which command should used to terminate all threads or unload appdomain in ASP.NET Core MVC controller ?

    The following code is used used to replace assembly:

    peStream = new MemoryStream();
    bool enableLazyLoading = false;
    var result = GenerateCode(sourceFiles, enableLazyLoading).Emit(peStream);
    var assemblyLoadContext = new AssemblyLoadContext("DynamicContext", isCollectible: !enableLazyLoading);
    peStream.Seek(0, SeekOrigin.Begin);
    var fn = AppContext.BaseDirectory + "DynamicContext.dll";
    File.Delete(fn);
    File.WriteAllBytes(fn, peStream.ToArray());
    peStream.Close();
    

    When running from Visual Studio,

    File.Delete(fn);

    throws error

    System.UnauthorizedAccessException
      HResult=0x80070005
      Message=Access to the path 'C:\MYApp\Store\bin\Debug\net5.0\DynammicContext.dll' is denied.
      Source=System.IO.FileSystem
      StackTrace:
       at System.IO.FileSystem.DeleteFile(String fullPath)
       at System.IO.File.Delete(String path)
    ..
    

    How to make it working when application is running in development mode from Visual Studio ?

    In Linux it replaces assembly properly.

    Monday, February 8, 2021 9:56 PM