昨天我的問題暫時解了
但目前遇到一個新的問題好像是跟AutoFac有關
我執行後會跑出這樣的錯誤
None of the constructors found with 'Autofac.Core.Activators.Reflection.DefaultConstructorFinder' on type 'Service.UserService' can be invoked with the available services and parameters:
我雖然有查過但還是看不出個所以然,想請問這個問題該如何解呢?
這是我程式架構
Startup
public void ConfigureContainer(ContainerBuilder builder)
{
builder.RegisterModule(new AutofacModuleRegister());
}
AutofacModuleRegister
public class AutofacModuleRegister : Autofac.Module
{
protected override void Load(ContainerBuilder builder)
{
var BasePath = AppContext.BaseDirectory;
var ServiceDll = Path.Combine(BasePath, "Service.dll");
var RepositoryDll = Path.Combine(BasePath, "Repository.dll");
var AssemblysServices = Assembly.LoadFrom(ServiceDll);
builder.RegisterAssemblyTypes(AssemblysServices)
.AsImplementedInterfaces()
.InstancePerDependency();
var AssemblysRepository = Assembly.LoadFrom(RepositoryDll);
builder.RegisterAssemblyTypes(AssemblysRepository)
.AsImplementedInterfaces()
.InstancePerDependency();
}
}
我是採用Dll批量注入
針對昨天你發問的問題,我發了兩個PR 解法,供你參考看看
https://github.com/Jarkwoof/BoardCore/pulls
Autofac的問題,是因為你的Repository是泛型要另外註冊,PR的Commit可以查看
builder.RegisterGeneric(typeof(BaseRepository<>)).As(typeof(IBaseRepository<>))
.AsImplementedInterfaces()
.InstancePerLifetimeScope();