Answer the question
In order to leave comments, you need to log in
How to use DB inside loaded assembly?
I've been exploring the possibilities of using assemblies in Asp.net core and came across a problem. When you try to call the entry point method in an application that uses a data context to communicate with the database, it throws an error. The context is framed as a service.
An unhandled exception occurred while processing the request.
InvalidOperationException: Unable to resolve service for type 'TestMVC1.Models.MobileContext' while attempting to activate 'TestMVC1.Controllers.HomeController'.
InvalidOperationException: Unable to resolve service for type 'TestMVC1.Models.MobileContext' while attempting to activate 'TestMVC1.Controllers.HomeController'.
Microsoft.Extensions.DependencyInjection.ActivatorUtilities.GetService(IServiceProvider sp, Type type, Type requiredBy,
lambda_method(Closure , IServiceProvider , object[] )
Microsoft.AspNetCore.Mvc.Controllers.ControllerActivatorProvider+<>c__DisplayClass4_0.b__0(ControllerContext controllerContext)
Microsoft.AspNetCore.Mvc.Controllers.ControllerFactoryProvider+<>c__DisplayClass5_0.g__CreateController|0(ControllerContext controllerContext)
Microsoft.
Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.InvokeInnerFilterAsync
() Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeNextResourceFilter
()
Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Rethrow(ResourceExecutedContext context)
Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Next
(ref State next, ref Scope scope, ref object state, ref bool isCompleted)
Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeFilterPipelineAsync()
Microsoft.AspNetCore.Routing.EndpointMiddleware.Invoke(HttpContext httpContext)
Microsoft.AspNetCore.Routing.EndpointRoutingMiddleware.Invoke
(HttpContext httpContext)
Microsoft.AspNetCore.StaticFiles.StaticFileMiddleware.Invoke(HttpContext context)
Microsoft.AspNetCore.Diagnostics.ExceptionPage .Invoke(HttpContext context)
What is the correct way to use the DB inside the loaded assembly?
Assembly loading
public void ConfigureServices(IServiceCollection services)
{
executableLocation = Assembly.GetEntryAssembly().Location;
var path = Path.Combine(Path.GetDirectoryName(executableLocation), "Plugins");
var assemblies = Directory
.GetFiles(path, "*.dll", SearchOption.AllDirectories)
.Select(AssemblyLoadContext.Default.LoadFromAssemblyPath)
.ToList();
path = Path.Combine(path, "TestMVC1.dll");
var alc = new TestAssemblyLoadContex();
Assembly a = alc.LoadFromAssemblyPath(path);
var b = a.GetTypes();
services.AddMvc().AddApplicationPart(a).AddRazorOptions(
o =>
{
o.FileProviders.Add(new EmbeddedFileProvider(a, a.GetName().Name));
}
);
a.EntryPoint.Invoke(null, new Object[] { null });
}
private MobileContext db;
public HomeController(MobileContext context)
{
db = context;
}
public class MobileContext : DbContext
{
public DbSet<Phone> Phones { get; set; }
public MobileContext(DbContextOptions<MobileContext> options)
: base(options)
{
Database.EnsureCreated();
}
}
Answer the question
In order to leave comments, you need to log in
And you in general registered service for MobileContext somewhere?
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question