0

Hello I'm trying to instantiate a class on application startup and then share that instance through dependency injection to a controller. For some reason it won't let me use a Controller Constructor with parameters. It gives the errors:

[MissingMethodException: No parameterless constructor defined for this object.]

and

[InvalidOperationException: An error occurred when trying to create a controller of type 'Project1.Controllers.HomeController'. Make sure that the controller has a parameterless public constructor.]

I've found a couple of people who have had similar issues but I haven't been able to figure out what the exact problem is.

I also left out some of the extraneous code (why it doesn't look like the code actually does anything useful).

Startup.cs

using Project1.Services;

[assembly: OwinStartup(typeof(Project1.Startup))]

namespace Project1
{
    public class Startup
    {
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddSingleton<IPackageScraperService>(new PackageScraperService());
        }

        public void Configuration(IAppBuilder app)
        {
        }
    }
}

IPackageScraperService.cs

using Project1.Services;

namespace Project1.Services
{
    public interface IPackageScraperService
    {
        List<PackageScraperService.Script> GetLoadedScripts();
    }
}

PackageScraperService.cs

namespace Project1.Services
{
  public class PackageScraperService : Services.IPackageScraperService
  {
      public class Script
      {
          public FileAttributes Attibutes { get; set; }

          public string Text { get; set; }
      }


      public List<Script> Scripts;

      public List<Script> GetLoadedScripts()
      {
          return Scripts;
      }   
  }

HomeController.cs

using Project1.Services;

namespace Project1.Controllers
{
    public class HomeController : Controller
    {
        private IPackageScraperService _packageScraperService;

        public HomeController(IPackageScraperService packageScraperService)
        {
            _packageScraperService = packageScraperService;
        }

        public ActionResult Index()
        {
            return View();
        }
    }

}

For anyone else looking for documentation, this tutorial worked for me.

http://scottdorman.github.io/2016/03/17/integrating-asp.net-core-dependency-injection-in-mvc-4/

als9xd
  • 834
  • 2
  • 12
  • 23
  • Your startup class looks like a bad mix between an ASP.NET and an ASP.NET Core application. How exactly did you get that code and what version of the framework are you using? – Camilo Terevinto Aug 02 '18 at 22:22
  • I was going off of this guide http://dotnetliberty.com/index.php/2015/10/15/asp-net-5-mvc6-dependency-injection-in-6-steps/ . I believe I'm using 4.7.2 framework. The one difference is that I used `AddSingleton()` instead of `AddInstance()` but from my research everyone said that was the replacement. – als9xd Aug 02 '18 at 22:31
  • Ugh that's horribly, horribly old (ASP.NET 5 was rebranded to ASP.NET Core in 2015, it never got into production, imagine...). Follow the official docs, it's going to be easier: https://learn.microsoft.com/en-us/aspnet/core/fundamentals/dependency-injection?view=aspnetcore-2.1 – Camilo Terevinto Aug 02 '18 at 22:33
  • That link is for asp.net core. Would the process be the same for asp.net framework? – als9xd Aug 03 '18 at 15:34
  • The guide you linked is for ASP.NET Core, not ASP.NET, that's why I pointed you to the new version of the same documentation – Camilo Terevinto Aug 03 '18 at 15:35
  • Ah ok. I'm using asp.net framework and haven't been able to find any guides or documentation for asp.net framework. Is DI an asp.net core only feature? – als9xd Aug 03 '18 at 15:38
  • The thing is that you need to use the correct terminology. You are either working with ASP.NET (4.x) (it's easier to search for ASP.NET MVC 5) or ASP.NET Core. Look [here](https://stackoverflow.com/questions/43311099/how-to-create-dependency-injection-for-asp-net-mvc-5) for some guidance – Camilo Terevinto Aug 03 '18 at 15:42
  • To be able to use constructor injection in your ASP.NET MVC controllers, you need to replace the default `IControllerFactory` implementation with one that calls back into your DI Container. – Steven Aug 03 '18 at 18:47

0 Answers0