dependency injection - Unable to resolve service for type 'Core.Data.CoreContext' while attempting to activate 'Core.Data.Repositories.UnitOfWork' -
i have part of webapi application want move separate project class library. common base structure every app has idea make easy shareable. application type is: asp.net core web application / .net framework - asp.net core 2.0 / web api
what have done in direction created project named core , moved shared elements there, including base entities (e.g. user, settings, etc), corecontext, unitofwork, generic repository, baserepositories,...
in main project of app there others entities, appcontext inherits corecontext, more repositories, , controllers, ...
i able build app when starting following error:
invalidoperationexception: unable resolve service type 'core.data.corecontext' while attempting activate 'core.data.repositories.unitofwork'.
microsoft.extensions.dependencyinjection.servicelookup.callsitefactory.createargumentcallsites(type servicetype, type implementationtype, iset callsitechain, parameterinfo[] parameters, bool throwifcallsitenotfound)...
problem seems unitofwork
class in core project , in main project startup.cs
method configureservices
has services.addtransient<iunitofwork, unitofwork>();
.
is bug or not configuring correctly , how achieved, if possible @ all?
*further technical details:
netcore & ef core version: 2.0
database provider: microsoft.entityframeworkcore.sqlserver
ide: visual studio 2017 15.3
operating system: windows 10
update: code sample
namespace main { public class program { public static void main(string[] args) { buildwebhost(args).run(); } public static iwebhost buildwebhost(string[] args) => webhost.createdefaultbuilder(args).usestartup<startup>().build(); } public class startup { public startup(iconfiguration configuration) { configuration = configuration; } public iconfiguration configuration { get; } public void configureservices(iservicecollection services) { services.addtransient<iunitofwork, unitofwork>(); services.addmvc(); var conn = configuration.getconnectionstring("defaultconnection"); services.adddbcontext<appcontext>(options => options.usesqlserver(conn)); } } } namespace main.data { public class appcontext : corecontext { ... } }
-
namespace core.data { public class unitofwork : iunitofwork, idisposable { public corecontext context; public unitofwork(corecontext context) { this.context = context; } public t get<t>() t : baserepository, new() { var repository = new t(); repository.init(context); return repository; } public void dispose() { dispose(true); gc.suppressfinalize(this); } } }
wiki
Comments
Post a Comment