InvalidOperationException: Unable to resolve service for type 'BusinessLayer.BusinessContext'

10 views
Skip to first unread message

Kerem

unread,
Jul 6, 2019, 10:53:25 AM7/6/19
to structuremap-users

Hello, I'm new to StructureMap yet :) . I added the error as an image file. Thanks everyone

In web project:
public class HomeController : BaseController
  {
    private readonly ILogger _logger;
    private readonly BusinessContext _businessContext;

    public HomeController(BusinessContext businessContext) : base(config)
    {
      _businessContext = businessContext;
    }

    [HttpGet]
    public IActionResult Index()
    {
      var ads = _businessContext.AdsService.All();
      return View(JsonConvert.SerializeObject(ads));
    }

  }


In Class library. Project name is BusinessLayer:

using BusinessLayer.Services;
using StructureMap;

namespace BusinessLayer
{
  public class BusinessContext
  {
    private readonly IContainer _container;
    public virtual AdsService AdsService { get { return GetService<AdsService>(); } }

    public BusinessContext()
    {
      _container = new Container();
    }

    public BusinessContext(IContainer container)
    {
      _container = container;
    }

    private T GetService<T>() where T : class
    {
      return _container.GetInstance<T>();
    }
  }
}
error.PNG
Reply all
Reply to author
Forward
0 new messages