在框架分层的情况下成功使用AutoFac控制反转!
跪求解决方案!
我的框架是MVC模式的.NET框架,
包含框架核心层,分数据服务接口层、数据服务层、业务处理接口层、业务处理层,表现层
表现层引用业务处理接口层,
业务处理层引用数据服务接口层、业务处理接口层,
数据服务层引用数据服务接口层,框架核心层的仓储接口
然后在表现层的Global.asax文件中的Application_Start方法中调用如下代码进行依赖注入
但是无法获取到数据服务层、及业务处理层的实例
求解决!
using Autofac;
using Autofac.Integration.Mvc;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Web;
using System.Web.Mvc;
namespace MicroCloud.Admin
{
public class AutoFacConfig
{
public static void RegisterAutoFac()
{
var builder = new ContainerBuilder();
//加载层的控制器,否则无法在其他层控制器构造注入,只能在web层注入
Assembly[] asm = GetAllAssembly("MicroCloud.Admin.dll").ToArray();
builder.RegisterAssemblyTypes(asm);
//注册服务
Assembly[] asmServices = GetAllAssembly("MicroCloud.DataServices.dll").ToArray
();
builder.RegisterAssemblyTypes(asmServices)
.Where(t => t.Name.EndsWith("Service")&&t.Name!= "BaseService" && t.Name !=
"BaseTableService")
.AsImplementedInterfaces();
//注册业务
Assembly[] asmBusiness = GetAllAssembly("MicroCloud.Business.dll").ToArray();
builder.RegisterAssemblyTypes(asmBusiness)
.Where(t => t.Name.EndsWith("Business"))
.AsImplementedInterfaces();
builder.RegisterControllers(Assembly.GetExecutingAssembly());
builder.RegisterModelBinders(Assembly.GetExecutingAssembly());
builder.RegisterModelBinderProvider();
//注册过滤器
//builder.RegisterFilterProvider();
//builder.RegisterType().PropertiesAutowired();
builder.RegisterControllers(typeof(MvcApplication).Assembly);
var container = builder.Build();
DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
}
#region 加载程序集
public static List GetAllAssembly(string dllName)
{
List pluginpath = FindPlugin(dllName);
var list = new List();
foreach (string filename in pluginpath)
{
try
{
string asmname = Path.GetFileNameWithoutExtension(filename);
if (asmname != string.Empty)
{
Assembly asm = Assembly.LoadFrom(filename);
list.Add(asm);
}
}
catch (Exception ex)
{
Console.Write(ex.Message);
}
}
return list;
}
//查找所有插件的路径
private static List FindPlugin(string dllName)
{
List pluginpath = new List();
string path = AppDomain.CurrentDomain.BaseDirectory; string dir = Path.Combine(path, "bin"); string[] dllList = Directory.GetFiles(dir, dllName); if (dllList.Length > 0) { pluginpath.AddRange(dllList.Select(item => Path.Combine(dir,
item.Substring(dir.Length + 1))));
}
return pluginpath;
}
#endregion
}
}
我的框架是MVC模式的.NET框架,
包含框架核心层,分数据服务接口层、数据服务层、业务处理接口层、业务处理层,表现层
表现层引用业务处理接口层,
业务处理层引用数据服务接口层、业务处理接口层,
数据服务层引用数据服务接口层,框架核心层的仓储接口
然后在表现层的Global.asax文件中的Application_Start方法中调用如下代码进行依赖注入
但是无法获取到数据服务层、及业务处理层的实例
求解决!
using Autofac;
using Autofac.Integration.Mvc;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Web;
using System.Web.Mvc;
namespace MicroCloud.Admin
{
public class AutoFacConfig
{
public static void RegisterAutoFac()
{
var builder = new ContainerBuilder();
//加载层的控制器,否则无法在其他层控制器构造注入,只能在web层注入
Assembly[] asm = GetAllAssembly("MicroCloud.Admin.dll").ToArray();
builder.RegisterAssemblyTypes(asm);
//注册服务
Assembly[] asmServices = GetAllAssembly("MicroCloud.DataServices.dll").ToArray
();
builder.RegisterAssemblyTypes(asmServices)
.Where(t => t.Name.EndsWith("Service")&&t.Name!= "BaseService" && t.Name !=
"BaseTableService")
.AsImplementedInterfaces();
//注册业务
Assembly[] asmBusiness = GetAllAssembly("MicroCloud.Business.dll").ToArray();
builder.RegisterAssemblyTypes(asmBusiness)
.Where(t => t.Name.EndsWith("Business"))
.AsImplementedInterfaces();
builder.RegisterControllers(Assembly.GetExecutingAssembly());
builder.RegisterModelBinders(Assembly.GetExecutingAssembly());
builder.RegisterModelBinderProvider();
//注册过滤器
//builder.RegisterFilterProvider();
//builder.RegisterType().PropertiesAutowired();
builder.RegisterControllers(typeof(MvcApplication).Assembly);
var container = builder.Build();
DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
}
#region 加载程序集
public static List GetAllAssembly(string dllName)
{
List pluginpath = FindPlugin(dllName);
var list = new List();
foreach (string filename in pluginpath)
{
try
{
string asmname = Path.GetFileNameWithoutExtension(filename);
if (asmname != string.Empty)
{
Assembly asm = Assembly.LoadFrom(filename);
list.Add(asm);
}
}
catch (Exception ex)
{
Console.Write(ex.Message);
}
}
return list;
}
//查找所有插件的路径
private static List FindPlugin(string dllName)
{
List pluginpath = new List();
string path = AppDomain.CurrentDomain.BaseDirectory; string dir = Path.Combine(path, "bin"); string[] dllList = Directory.GetFiles(dir, dllName); if (dllList.Length > 0) { pluginpath.AddRange(dllList.Select(item => Path.Combine(dir,
item.Substring(dir.Length + 1))));
}
return pluginpath;
}
#endregion
}
}
没有找到相关结果
已邀请:
0 个回复