1

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

Jessica
  • 115
  • 1
  • 8
  • NopCommerce seems to use Autofac. You should tag your question as such. – Steven May 11 '17 at 09:15
  • Which version you're using of nopCommerce? – Divyang Desai May 11 '17 at 11:34
  • It's version 3.90 – Jessica May 11 '17 at 11:38
  • You're are showing an image of the nopCommerce's code, but that's not what is causing the problem. That code, works. A lot of things depend on it. As the error says, it seems you're requesting the instance after the normal page cycle has finished and the dependency scope has been disposed. It seems you're trying to do something really late. To anyone being able to help you, you need to give a better and broader context. When is "second time"? – Marco Regueira May 12 '17 at 15:55

1 Answers1

0
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.

Divyang Desai
  • 7,483
  • 13
  • 50
  • 76
  • I can't pass it in to a constructor as the constructor needs to remain parameterless and doing it the third way gives me the same error as the normal way I am doing it – Jessica May 11 '17 at 22:43
  • @JessicaPartridge: Nope, image of an error not make any sense! we need code sample to generate same issue. – Divyang Desai May 12 '17 at 11:50