AutoFac Error with a custom service

Topics: Customizing Orchard
Apr 18, 2012 at 4:09 PM

I am trying to extend the existing Orchard.blogs module and need to inject another dependency into its constructor.

So, as per Orchard documentation, I created a service IUserRoleService that implements IDependency. 

Then, I modified the Orcahrd.Blogs constructor as this -

 public BlogPostAdminController(IOrchardServices services, IBlogService blogService, IBlogPostService blogPostService, IUserRoleService userRoleService)
        {
            Services = services;
            _blogService = blogService;
            _blogPostService = blogPostService;
            T = NullLocalizer.Instance;
            _userRoleService = userRoleService;
        }

However, when I run my solution, I get this error - No constructors on type 'Orchard.Blogs.Services.UserRoleService' can be found with 'Orchard.Environment.AutofacUtil.DynamicProxy2.ConstructorFinderWrapper

Am I missing something?

 

Thanks

Coordinator
Apr 18, 2012 at 6:26 PM

I can't find IUserRoleService in Orchard. Is it something you built? Then chances are you forgot to make it derive from IDependency.

Also, it's bad practice to modify core modules. You could instead build your own module or replace the implementation.

Apr 18, 2012 at 7:45 PM

Yes, IUserRoleService is my custom service that I am trying to integrate inside the Blogs module. Also, I double checked that it is indeed deriving from IDependency interface. I don't understand the reason why exception is thrown?

I understand that modifying the core modules is a bad idea, but Orchard.Blogs is part of Web modules and not the Core modules. Is that a correct statement?

And instead of creating a Custom Blog module, I assessed that extending the existing module would be faster and quicker. Comments ?

 

Apr 18, 2012 at 8:03 PM

Core modules in this case means anything that comes with a clone of the Orchard source code repository. So even though Orchard.Blogs isn't part of the "Orchard.Core" it is considered a core module.

It is not ideal for you to modify the source code for Orchard.Blogs because it will make things difficult for you if/when you ever need to upgrade the version of Orchard you are using for this project. You would have to merge the updates to Orchard.Blogs with your own changes. "Faster and quicker" often means worse in the long run. 

Apr 18, 2012 at 9:04 PM

Thanks, I get an idea now. But, what about the AutoFac exception, which was originally the question I posted? I am still curious on that part.

Apr 18, 2012 at 9:08 PM

Post code for IUserRoleService, and the default implementation of it? 

Apr 18, 2012 at 10:43 PM
Edited Apr 18, 2012 at 10:44 PM

IUserRoleService.cs

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Orchard.Blogs.Services
{
    public interface IUserRoleService :IDependency
    {
        IList GetUserRoles();
        IList GetAllUsersInARole(string roleName);
    }
}


UserRoleService.cs

using System; using System.Collections.Generic; using System.Linq; using System.Web; using Orchard.Roles.Models; using Orchard.Data; using Orchard.Users; using Orchard.Users.Models; namespace Orchard.Blogs.Services { public class UserRoleService : IUserRoleService { private readonly IOrchardServices _services; private readonly ISessionLocator _sessionLocator; UserRoleService(IOrchardServices services, ISessionLocator sessionLocator) { sessionLocator = _sessionLocator; _services = services; } public IList GetUserRoles() { throw new NotImplementedException(); } public IList GetAllUsersInARole(string roleName) { throw new NotImplementedException(); } } }
Coordinator
Apr 18, 2012 at 10:54 PM

Things to check: that your module is enabled, and that you added it as a dependency in the blog module's manifest.

I would still make a separate module that does SuppressDependency on the things you need to replace.

Jul 21, 2012 at 12:29 AM
Edited Jul 21, 2012 at 1:05 AM

Did anyone ever figure this out? I have a clear example of using DI exactly as documented, but its not working:

using System.Web.Mvc;
using Orchard.Blogs.Services;
using Orchard.ContentManagement;
using Orchard.Themes;

namespace MySite.Sitemap.Controllers
{
    public class SitemapController : Controller
    {
        private readonly IContentManager _ContentManager;
        private readonly IBlogService _BlogService;
        private readonly IBlogPostService _BlogPostService;

        SitemapController(IContentManager contentManager, IBlogService blogService, IBlogPostService blogPostService)
        {
            _ContentManager = contentManager;
            _BlogService = blogService;
            _BlogPostService = blogPostService;
        }
        
        [Themed]
        public ActionResult GetSitemap()
        {
            return View("Sitemap");
        }
    }
}

Yes, the module is enabled. Yes, I have the module.txt list dependencies. These are core services. The controller and route work fine until that constructor is added, then I get the Big Error:

No constructors on type 'MySite.Sitemap.Controllers.SitemapController' can be found with 'Orchard.Environment.AutofacUtil.DynamicProxy2.ConstructorFinderWrapper'.

I've read every post and thread on this. I destroy all generated files and restart IIS between testing sessions just to be sure it isn't a rogue assembly not going away. I fail to see how this use case is anything but EXACTLY what is specified by the documentation.

Coordinator
Jul 21, 2012 at 12:38 AM

Maybe you want to make this constructor public ;)

Mistakes happen sometimes ... and they take an awful amount of time to fix !

Jul 21, 2012 at 1:03 AM
Edited Jul 21, 2012 at 1:08 AM

Grr! You nailed it Sebastian. Good eye, thanks.

Hopefully this thread will help the next person who makes this *dumb* mistake.