如何用结构图扫描另一个库
how to scan another library with structure map
我有一个名为 ServiceLayer 的 class 库,其中包含以下代码:
IProductService.cs
public interface IProductService
{
void AddNewProduct(Product product);
IList<Product> GetAllProducts();
}
ProductService.cs
public class ProductService : IProductService
{
readonly IDbSet<Product> _products;
public ProductService(IUnitOfWork uow)
{
_products = uow.Set<Product>();
}
public void AddNewProduct(Product product)
{
_products.Add(product);
}
public IList<Product> GetAllProducts()
{
return _products.Include(x => x.Category).ToList();
}
}
我安装了 Structuremap.MVC5,所以在 DefaultRegistry
文件中我有以下代码:
DefaultRegistry.cs
public DefaultRegistry()
{
Scan(
scan =>
{
scan.TheCallingAssembly();
scan.WithDefaultConventions();
scan.With(new ControllerConvention());
});
For<IUnitOfWork>().HybridHttpOrThreadLocalScoped().Use(() => new ApplicationDbContext());
}
但是结构图不起作用,给我这个异常:
An exception of type 'StructureMap.StructureMapConfigurationException'
occurred in StructureMap.dll but was not handled in user code
附加信息:没有注册默认实例,无法自动确定类型 'MEF.ServiceLayer.IProductService'
所以我的问题是,结构图如何扫描主项目以外的另一个 class 库?
您应该尝试添加包含 IProductService 的程序集。例如
Scan(
scan =>
{
scan.TheCallingAssembly();
scan.WithDefaultConventions();
// Add the assembly that contains a certain type
scan.AssemblyContainingType<IProductService>();
scan.With(new ControllerConvention());
}
);
如果上述方法不起作用,可能是 ProductServce
中缺少默认构造函数。尝试添加以下内容:
public class ProductService : IProductService
{
readonly IDbSet<Product> _products;
[DefaultConstructor]
public AccountController()
{
}
...
}
如果您引用了包含IProductService
的项目,您可以使用Assembly方法并传递项目名称:
Scan(scan =>
{
// YourProject.Service: The project that contains IProductService
scan.Assembly("YourProject.Service");
scan.TheCallingAssembly();
scan.WithDefaultConventions();
});
我有一个名为 ServiceLayer 的 class 库,其中包含以下代码:
IProductService.cs
public interface IProductService
{
void AddNewProduct(Product product);
IList<Product> GetAllProducts();
}
ProductService.cs
public class ProductService : IProductService
{
readonly IDbSet<Product> _products;
public ProductService(IUnitOfWork uow)
{
_products = uow.Set<Product>();
}
public void AddNewProduct(Product product)
{
_products.Add(product);
}
public IList<Product> GetAllProducts()
{
return _products.Include(x => x.Category).ToList();
}
}
我安装了 Structuremap.MVC5,所以在 DefaultRegistry
文件中我有以下代码:
DefaultRegistry.cs
public DefaultRegistry()
{
Scan(
scan =>
{
scan.TheCallingAssembly();
scan.WithDefaultConventions();
scan.With(new ControllerConvention());
});
For<IUnitOfWork>().HybridHttpOrThreadLocalScoped().Use(() => new ApplicationDbContext());
}
但是结构图不起作用,给我这个异常:
An exception of type 'StructureMap.StructureMapConfigurationException' occurred in StructureMap.dll but was not handled in user code
附加信息:没有注册默认实例,无法自动确定类型 'MEF.ServiceLayer.IProductService'
所以我的问题是,结构图如何扫描主项目以外的另一个 class 库?
您应该尝试添加包含 IProductService 的程序集。例如
Scan(
scan =>
{
scan.TheCallingAssembly();
scan.WithDefaultConventions();
// Add the assembly that contains a certain type
scan.AssemblyContainingType<IProductService>();
scan.With(new ControllerConvention());
}
);
如果上述方法不起作用,可能是 ProductServce
中缺少默认构造函数。尝试添加以下内容:
public class ProductService : IProductService
{
readonly IDbSet<Product> _products;
[DefaultConstructor]
public AccountController()
{
}
...
}
如果您引用了包含IProductService
的项目,您可以使用Assembly方法并传递项目名称:
Scan(scan =>
{
// YourProject.Service: The project that contains IProductService
scan.Assembly("YourProject.Service");
scan.TheCallingAssembly();
scan.WithDefaultConventions();
});