Net452中使用仓储 Autofac怎么注入 返回
SqlSugar
老数据
1
1930
悬赏:5 飞吻
public SqlSugarRepository(ISqlSugarClient context = null) : base(context)//注意这里要有默认值等于null
{
if (context == null)
{
Context = new SqlSugarClient(new ConnectionConfig()
{
//数据库连接基础配置
DbType = DbType.Oracle,
InitKeyType = InitKeyType.Attribute,
IsAutoCloseConnection = true,
ConnectionString = ConfigurationManager.ConnectionStrings["oracle"].ConnectionString,
//扩展配置
ConfigureExternalServices = new ConfigureExternalServices
{
//MemoryCache
DataInfoCacheService = new MemoryCacheService()
}
});
//在调试窗口输出SQL脚本及参数
Context.Aop.OnLogExecuting = (s, p) =>
{
System.Diagnostics.Debug.WriteLine("\n" + s);
System.Diagnostics.Debug.WriteLine(string.Join(",", p?.Select(it => it.ParameterName + ":" + it.Value)) + "\n");
};
//修改SQL和参数:移除预编译SQL中参数的Conditional字符,消除参数名过长导致SQL执行报错问题
Context.Aop.OnExecutingChangeSql = (sql, pars) =>
{
sql = sql.Replace("Conditional", "");
if (pars != null && pars.Length > 0)
{
pars.ToList().ForEach(it => it.ParameterName = it.ParameterName.Replace("Conditional", ""));
}
return new KeyValuePair<string, SugarParameter[]>(sql, pars);
};
}
}Autofac注入
var serviceDllFile = Path.Combine(basePath, @"bin\ZYKP_JCApi.Service.dll"); var serviceAssembly = Assembly.LoadFrom(serviceDllFile); builder.RegisterAssemblyTypes(serviceAssembly).AsImplementedInterfaces(); var repositoryDllFil = Path.Combine(basePath, @"bin\ZYKP_JCApi.Repository.dll"); var repositoryAssembly = Assembly.LoadFrom(repositoryDllFil); builder.RegisterAssemblyTypes(repositoryAssembly).AsImplementedInterfaces();
service层引用repository,运行时报错:None of the constructors found with 'Autofac.Core.Activators.Reflection.DefaultConstructorFinder'
on type 'ZYKP_JCApi.Service.MonitorInfoService' can be invoked with the available services and parameters:
Cannot resolve parameter 'ZYKP_JCApi.IRepository.ISqlSugarRepository`1[ZYKP_JCApi.Model.MonitorInfo] _repository' of constructor
'Void .ctor(ZYKP_JCApi.IRepository.ISqlSugarRepository`1[ZYKP_JCApi.Model.MonitorInfo])'
热忱回答(1)
-
fate sta VIP0
2021/10/14看着像构造函数有参数
0 回复