Jaxson Reynolds
Jaxson Reynolds

Reputation: 11

Cannot add Service layer to Orchard site

I am trying to add a service project into my Orchard CMS site but when I inject that service into my controller I get this error:

 Autofac.Core.DependencyResolutionException: None of the constructors found with 'Orchard.Environment.AutofacUtil.DynamicProxy2.ConstructorFinderWrapper' on type 'tiko23867.SharedUI.Controllers.ContactUsController' can be invoked with the available services and parameters:
Cannot resolve parameter 'Autofac.ContainerBuilder builder' of constructor 'Void .ctor(Autofac.ContainerBuilder, Services.ContactUs.IContactUsService)'.

I have looked up questions on stack overflow but those problems that involve this error seem to be with freshly installing Orchard.

My controller :

using BusinessObjects.ContactUs;
using Orchard.Themes;
using Services.ContactUs;
using tiko23867.SharedUI.Models;

namespace tiko23867.SharedUI.Controllers
{
    [Themed]
    public partial class ContactUsController : Controller
    {
        private readonly IContactUsService _contactUsService;

        public ContactUsController(IContactUsService contactUsService)
        {
            _contactUsService = contactUsService;
        }

        [HttpGet]
        public virtual ActionResult Index()
        {
            var viewModel = new ContactUsViewModel();

            return View(viewModel);
        }

        [HttpPost]
        public virtual ActionResult Index(ContactUsViewModel contactUsViewModel)
        {
            _contactUsService.SaveContactUsRequest(new ContactUsItem
            {
                Email = contactUsViewModel.Email,
                FirstName = contactUsViewModel.FirstName,
                LastName = contactUsViewModel.LastName,
                Message = contactUsViewModel.Message
            });

            return View("Thanks");
        }

    }
}

My Service:


using BusinessObjects.ContactUs;

namespace Services.ContactUs
{
    public interface IContactUsService
    {
        void SaveContactUsRequest(ContactUsItem item);
    }

    public class ContactUsService
    {
        public ContactUsService()
        {
        }

        public void SaveContactUsRequest(ContactUsItem item)
        {
        }
    }
}

Upvotes: 0

Views: 73

Answers (1)

Hazza
Hazza

Reputation: 6591

You can make your interface inherit from IDependency, which will automatically register your service and make it discoverable via constructor injection. No need to mess around with autofac.

Upvotes: 1

Related Questions