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/

8
  • 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? Commented Aug 2, 2018 at 22:22
  • I was going off of this guide dotnetliberty.com/index.php/2015/10/15/… . 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. Commented Aug 2, 2018 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: learn.microsoft.com/en-us/aspnet/core/fundamentals/… Commented Aug 2, 2018 at 22:33
  • That link is for asp.net core. Would the process be the same for asp.net framework? Commented Aug 3, 2018 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 Commented Aug 3, 2018 at 15:35

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.