Another dumb parameterless constructor problem

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
12 years ago
I created a new service for my installation of 2.3 to print out some reports in Excel.  I made the new interface, created the service to implement the interface, and registered the interface.  It has worked fine for a few weeks now.

Today I decided to add a dependency to it so I can use the IProductService.  I had no constructor before because I didn't need anything so I added one as you would expect.  Now when I loaded my site via VS, it fails on the first Controller it can fine because of a missing parameterless constructor.  I narrowed it down to my IExcelService.  If I remove the constructor, everything works.  

I know I have it registered because I made my own DependencyRegistrar class and I can see it executing.  What am I missing?

public partial class ExcelService : IExcelService
{
        private IProductService _productService;

        public ExcelService(IProductService productService)
        {
              this._productService = productService;
        }

        public virtual byte[] AGreatFunction()
        {         }
}


public class ASCDependencyRegistrar : IDependencyRegistrar
{
        public virtual void Register(ContainerBuilder builder, ITypeFinder typeFinder)
        {
            builder.RegisterType<ASCImportManager>().As<IImportManager>().InstancePerHttpRequest();
            builder.RegisterType<ASCWorkflowMessageService>().As<IWorkflowMessageService>().InstancePerHttpRequest();
            builder.RegisterType<ASCMessageTokenProvider>().As<IMessageTokenProvider>().InstancePerHttpRequest();
            builder.RegisterType<ASCPriceCalculationService>().As<IPriceCalculationService>().InstancePerHttpRequest();
            builder.RegisterType<ASCProductService>().As<IProductService>().InstancePerHttpRequest();
            builder.RegisterType<ExcelService>().As<IExcelService>().InstancePerHttpRequest();
        }

        public int Order
        {
            get { return 1; }
        }
}


Any code that is touching the ExcelService is loading IExcelService in it's constructor.

I'm working around it right now by using
_productService = Nop.Core.Infrastructure.EngineContext.Current.Resolve<IProductService>();
in my function but that's not ideal.
12 years ago
Your code seems to be OK. But I presume that you pass an implemented class to some services instead of an interface. I see that you have some other classes such as ASCPriceCalculationService and ASCProductService. Please ensure that all of them use interfaces. If it doesn't help, please share your solution (send me a PM) and I'll have a look
12 years ago
I think it has to be something to do with that ExcelService because if I remove the constructor, everything works fine.  I'll go over it again tomorrow.
12 years ago
Can you post your ExcelService constructor here?
12 years ago
It's in the first post.
12 years ago
AndyMcKenna wrote:
It's in the first post.

Really weird. I don't know where the issue it. If you could share your solution, then I'll try testing it.
12 years ago
Would it make sense if I was making circular dependencies?

CatalogController was injecting IWorkflowMessageService
I changed IWorkflowMessageService to inject IExcelService

That was working fine for a long time.  But then when I change IExcelService to inject IProductService, it all breaks.  I think it's because IProductService injects IWorkflowMessageService.  Now we've got a full loop.  I just started getting Stack Overflow errors which is what put me on the infinite loop thought.

I think my fix is to have a member function that I can use to populate the services from a unit test.  If I need the service in one of the functions, resolve it from the NopEngine if it's null.
11 years ago
I'm getting parameterless constructor problem when web starts up (home page), on this line:

...\src\Presentation\Nop.Web\Views\Home\Index.cshtml
@Html.Action("HomepageCategories", "Catalog")


All I've done is to add IShippingService to this constructor

        public CheckoutAttributeService(ICacheManager cacheManager,
            IRepository<CheckoutAttribute> checkoutAttributeRepository,
            IRepository<CheckoutAttributeValue> checkoutAttributeValueRepository,
            IEventPublisher eventPublisher,
            IShippingService shippingService)

(I've removed all the other code I added, and left only the above parameter, and the using stmt)

The runtime never even reaches the constructor; the stack looks like this
  [External Code]  
>  App_Web_oeerdfsg.dll!ASP._Page_Views_Home_Index_cshtml.Execute() Line 9 + 0x25 bytes  C#
  [External Code]  


Here's what stack looks like when it's working without the extra param:

>  Nop.Services.DLL!Nop.Services.Orders.CheckoutAttributeService.CheckoutAttributeService(Nop.Core.Caching.ICacheManager cacheManager, Nop.Core.Data.IRepository<Nop.Core.Domain.Orders.CheckoutAttribute> checkoutAttributeRepository, Nop.Core.Data.IRepository<Nop.Core.Domain.Orders.CheckoutAttributeValue> checkoutAttributeValueRepository, Nop.Core.Events.IEventPublisher eventPublisher) Line 56  C#
  [External Code]  
  Nop.Core.DLL!Nop.Core.Infrastructure.DependencyManagement.ContainerManager.Resolve(System.Type type) Line 115 + 0x17 bytes  C#
  Nop.Core.DLL!Nop.Core.Infrastructure.NopEngine.Resolve(System.Type type) Line 86 + 0x19 bytes  C#
  Nop.Web.Framework.DLL!Nop.Web.Framework.Mvc.NopDependencyResolver.GetService(System.Type serviceType) Line 14 + 0x14 bytes  C#
  [External Code]  
  App_Web_rbee3l0o.dll!ASP._Page_Views_Home_Index_cshtml.Execute() Line 8 + 0x25 bytes  C#
  [External Code]  


Also, if I use IWorkContext, instead of IShippingService it works fine.  It reaches the constructor, and the stack shows like above.

Is there something about the order of builder.RegisterType in  \src\Presentation\Nop.Web.Framework\DependencyRegistrar.cs

or some time of circular ref problem as per prior post?  Is this expected?


(No solution here either
https://www.nopcommerce.com/boards/t/15191/no-parameterless-constructor-defined-for-this-object.aspx)
11 years ago
New York wrote:
All I've done is to add IShippingService to this constructor...

/you have circular references. IShippingService depends on ICheckoutAttributeParser which depends on ICheckoutAttributeService.

Try not passing  IShippingService into CheckoutAttributeService constructor but resolving it by EngineContext.Current.Resolve.
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.