0

I have a class library that called ServiceLayer which contains the below code:

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();
    }
}

I installed Structuremap.MVC5, so in DefaultRegistry file i have the below code:

DefaultRegistry.cs

 public DefaultRegistry()
    {
        Scan(
            scan =>
            {
                scan.TheCallingAssembly();
                scan.WithDefaultConventions();
                scan.With(new ControllerConvention());
            });

        For<IUnitOfWork>().HybridHttpOrThreadLocalScoped().Use(() => new ApplicationDbContext());
    }

But structure map doesn't work and gives me this exception:

An exception of type 'StructureMap.StructureMapConfigurationException' occurred in StructureMap.dll but was not handled in user code

Additional information: No default Instance is registered and cannot be automatically determined for type 'MEF.ServiceLayer.IProductService'

So my question is this, How can structure map scan another class library other then main project?

SwDevMan81
  • 48,814
  • 22
  • 151
  • 184
kamal
  • 237
  • 1
  • 5
  • 18

2 Answers2

0

You should try to add the assembly that contains the IProductService. For example

Scan(
   scan =>
      {
         scan.TheCallingAssembly();
         scan.WithDefaultConventions();
         // Add the assembly that contains a certain type
         scan.AssemblyContainingType<IProductService>();
         scan.With(new ControllerConvention());
      }
    );

If the above doesnt work, it would be the lack of a default constructor in the ProductServce. Try adding the following:

public class ProductService : IProductService
{
   readonly IDbSet<Product> _products;
   [DefaultConstructor]
   public AccountController()
   {

   }

   ...
}     
SwDevMan81
  • 48,814
  • 22
  • 151
  • 184
  • Thanks, it work now. But instead of adding `AssemblyContainingType`, I just added `Assembly`, since i might delete `IProductService` in future. – kamal Aug 06 '15 at 14:08
0

If you have referenced the project that contains IProductService, You can use Assembly method and pass project name:

Scan(scan =>
     {
                // YourProject.Service: The project that contains IProductService
                scan.Assembly("YourProject.Service");
                scan.TheCallingAssembly();
                scan.WithDefaultConventions();
     });
Sirwan Afifi
  • 10,654
  • 14
  • 63
  • 110