
```c#
async Task Main()
{
var builder = new ContainerBuilder();
builder.RegisterType<InstancePerRequest2Service>().As<IInstancePer2Request>();
builder.RegisterType<InstancePerLifetimeScopeService>().As<IInstancePerLifetimeScope>().InstancePerLifetimeScope();
builder.RegisterType<SingleInstance2Service>().As<ISingleInstance2>().SingleInstance();
Container = builder.Build();
Console.WriteLine("after build");
var scope = Container.BeginLifetimeScope(builder =>
{
Console.WriteLine("in configurationAction");
var scopeService = NSubstitute.Substitute.For<IInstancePerLifetimeScope>();
scopeService.Log().Returns("mock");
builder.RegisterInstance(scopeService); //.ShouldBe("mock"); error
///builder.RegisterInstance(scopeService).As<IInstancePerLifetimeScope>(); //.ShouldBe("mock"); error
///builder.RegisterInstance(scopeService).As<IInstancePerLifetimeScope>().InstancePerLifetimeScope();
/// error The instance registration 'Substitute.UserQuery+IInstancePerLifetimeScope|00ced0cd' can support SingleInstance() sharing only.
});
var pre = scope.Resolve<IInstancePer2Request>();
pre.ReturnLifetimeScopeLogMethodResult().Dump("IInstancePer2Request").ShouldBe("mock");
Console.WriteLine("shouldbe mock true");
var single = scope.Resolve<ISingleInstance2>();
single.ReturnLifetimeScopeLogMethodResult().Dump("ISingleInstance2").ShouldBe("mock");
}
public interface IInstancePer2Request
{
string ReturnLifetimeScopeLogMethodResult();
}
public class InstancePerRequest2Service : IInstancePer2Request, IDisposable
{
string _id;
IInstancePerLifetimeScope _scope;
public InstancePerRequest2Service(IInstancePerLifetimeScope scope)
{
_id = Guid.NewGuid().ToString();
Console.WriteLine($"in {nameof(IInstancePer2Request)} ctor");
_scope = scope;
}
public void Dispose()
{
Console.WriteLine($"in Dispose InstancePerRequestService锛歿_id}");
}
public string ReturnLifetimeScopeLogMethodResult()
{
return _scope.Log();
}
}
public interface IInstancePerLifetimeScope
{
string Log();
}
public class InstancePerLifetimeScopeService : IInstancePerLifetimeScope, IDisposable
{
string _id;
public InstancePerLifetimeScopeService()
{
_id = Guid.NewGuid().ToString();
Console.WriteLine($"in {nameof(IInstancePerLifetimeScope)} ctor id:{_id}");
}
public string Log()
{
var str = $"InstancePerLifetimeScopeService:{_id}";
return str;
}
public void Dispose()
{
Console.WriteLine($"in Dispose :{_id}");
//throw new NotImplementedException();
}
}
public interface ISingleInstance2
{
string ReturnLifetimeScopeLogMethodResult();
}
public class SingleInstance2Service : ISingleInstance2
{
IInstancePerLifetimeScope _scope;
public SingleInstance2Service(IInstancePerLifetimeScope scope)
{
Console.WriteLine($"in {nameof(SingleInstance2Service)} ctor ");
//
//Console.WriteLine("scope");
//
//scope.Log().Dump("IInstancePerLifetimeScope Log");
_scope = scope;
}
public string ReturnLifetimeScopeLogMethodResult()
{
return _scope.Log();
}
}
```
should be able to mock before you generate a singleinstance object
The performance of default registration is different from that of singleinstance

Just like that one ? https://github.com/autofac/Autofac/issues/809
Would you be so kind to update your issue with a descriptive text that describes your intention and the problem(s) that you encountered? It helps us to understand you and would definitely also improve search results for others.
@alsami
I'm very sorry. Thank you for your answer. I didn't have a good review before submitting
My question is about:
On the screenshot of my linqpad, I can get my mock object on line 34, but I can't get my mock object on line 36 because it's a singleton. Both of them refer to iinstanceperlife timescope, but only one of them works
I admit I'm still not entirely clear about the issue but it looks like you're trying to force a captive dependency by putting an object registered in a lifetime scope into a singleton registered in a parent scope. That won't work, and intentionally so. If that was allowed to happen, you could dispose the child scope and tender the singleton from the parent scope invalid for the remainder of the lifetime of the container.
When you resolve a singleton, all of the singleton's dependencies will come from the scope in which the singleton was registered (ie, the root container) so overriding it in a child container won't matter.
Admittedly I'm on my phone and trying to respond without a full compiler, but that's what it looks like. If that's not the issue, please try explaining a little better. It may help to remove some of the extra indirection and simplify the example a bit, too, just to make sure it's easy to follow with respect to the explanation. For example, the service instances in the classes are named _scope which I'd normally not have much problem with but I want to make sure we're separating the notion of a lifetime scope from the notion of a service resolved from a scope... and little things like that can introduce communication confusion.
Thank you. I understand that it's my wrong behavior that leads to this kind of mistake. Indeed, I'm like designing the lifecycle of singleton object management subdomain. In this case, I'll think of another way to realize it. Thank you for your patience. Thank you very much for turning off this issues. Thank you
@tillig
Most helpful comment
Would you be so kind to update your issue with a descriptive text that describes your intention and the problem(s) that you encountered? It helps us to understand you and would definitely also improve search results for others.