NopCommerce disposing of register interface instances

330 views Asked by At

I have been struggling to get NopCommerce to pick up a registered interface.

This line here

this._connectionService = EngineContext.Current.Resolve<IConnectionService>(); 

is causing the error

Instances cannot be resolved and nested lifetimes cannot be created from this LifetimeScope as it has already been disposed.

I have registered the interface in the DependencyRegistrar

builder.RegisterType<ConnectionService>().As<IConnectionService>().InstancePerLifetimeScope();

The issue is that it works when the constructor is called once, but when the constructor is called again the interface has been disposed, the constructor is as seen below -

public ClientHub()
{
    this._connectionService = EngineContext.Current.Resolve<IConnectionService>();
}

By default NopCommerce has a lot of registered interface already so I have tried using what looks like a cached version

builder.RegisterType<CustomerActivityService>().As<ICustomerActivityService>()
            .WithParameter(ResolvedParameter.ForNamed<ICacheManager>("nop_cache_static"))
            .InstancePerLifetimeScope();

But I still get an error when trying to use the ConnectionService like this.

I have tried using multiple different methods on the builder such as

InstancePerHttpRequest();

and

InstancePerDependency();

But it's all being disposed of when the constructor is called a second time, I did head over to the NopCommerce for any help but no luck.

Any guidance / help would be amazing !

enter image description here

1

There are 1 answers

2
Divyang Desai On
public ClientHub()
{
    this._connectionService = EngineContext.Current.Resolve<IConnectionService>();
}

well, that may create an issue, you're trying to resolve dependency, but that messed up.
It could be:

private readonly IConnectionService _connectionService

public ClientHub(IConnectionService connectionService)
{
  this._connectionService = connectionService;
}

Or either it could be only:

private readonly IConnectionService _connectionService = EngineContext.Current.Resolve<IConnectionService>();

And not in constructor.