我遇到ReuseScope.Request
的问题。即使我指定ReuseScope.Request
,我也会在每个请求上注入相同的实例。我使用这两个调用配置容器以获取MasterConfig:
this.container.RegisterAutoWiredAs<ApiConfigFactory, IConfigFactory>().ReusedWithin(ReuseScope.Container);
this.container.Register(c => c.Resolve<IConfigFactory>().GetMasterConfig(true)).ReusedWithin(ReuseScope.Request);
GetMasterConfig(true)方法返回一个新的具体MasterConfig。但是,当我尝试在服务中使用MasterConfig时,我会在每个请求中获得相同的实例。
public class MyService
{
private readonly MasterConfig masterConfig;
public SaleService(MasterConfig masterConfig)
{
this.masterConfig = masterConfig;
}
public object Post(MyRequest request)
{
// **masterConfig is the same instance here on every request**
}
}
如果我将MasterConfig寄存器的范围更改为ReuseScope.None
,我会按预期加载新的MasterConfig。我错过了什么?注册MasterConfig的方式有问题吗?为什么ReuseScope.None
解决了这个问题?为什么ReuseScope.Request
给我相同的实例?
注意:
答案 0 :(得分:0)
我无法在自托管主机或ASP.NET主机中重现此问题。
这适用于最新版本的ServiceStack:
//AppHost
public class RequestScopeAppHost : AppSelfHostBase
{
public RequestScopeAppHost()
: base(typeof(RequestScopeAppHost).Name, typeof(RequestScopeService).Assembly) {}
private static int counter = 0;
public override void Configure(Container container)
{
container.Register(c => new MasterConfig {
Id = Interlocked.Increment(ref counter)
}).ReusedWithin(ReuseScope.Request);
}
}
服务:
public class MasterConfig
{
public int Id { get; set; }
}
public class GetMasterConfig : IReturn<MasterConfig> { }
public class RequestScopeService : Service
{
private readonly MasterConfig config;
public RequestScopeService(MasterConfig config)
{
this.config = config;
}
public object Any(GetMasterConfig request)
{
return config;
}
}
测试:
[TestFixture]
public class RequestScopeIssue
{
private readonly ServiceStackHost appHost;
public RequestScopeIssue()
{
appHost = new RequestScopeAppHost()
.Init()
.Start(Config.AbsoluteBaseUri);
}
[TestFixtureTearDown]
public void TestFixtureTearDown()
{
appHost.Dispose();
}
[Test]
public void Can_get_RequestScope_dependency()
{
var client = new JsonServiceClient(Config.AbsoluteBaseUri);
Assert.That(client.Get(new GetMasterConfig()).Id, Is.EqualTo(1));
Assert.That(client.Get(new GetMasterConfig()).Id, Is.EqualTo(2));
Assert.That(client.Get(new GetMasterConfig()).Id, Is.EqualTo(3));
}
}
在您的说明中ReuseScope.None
也按预期工作,它不会重复使用实例。
答案 1 :(得分:0)
使用ServiceStack 4.5.6版本,我也遇到了完全相同的问题
这是我的注册代码
container.RegisterAutoWiredAs<EventConditionalLoadingRepository, IEventConditionalLoadingRepository>()
.ReusedWithin(ReuseScope.Request);
container.RegisterAutoWiredAs<MetaRiskRepository, IMetaRiskRepository>()
.ReusedWithin(ReuseScope.Request);
container.RegisterAutoWiredAs<RiskStoreConnectivityService, IRiskStoreConnectivityService>()
.ReusedWithin(ReuseScope.Request);
container.Register<IUnitOfWork>(c =>
{
return new UnitOfWork();
}).ReusedWithin(ReuseScope.Request);
container.Register(c => (IUnitOfWorkEnlistable)c.Resolve<IUnitOfWork>())
.ReusedWithin(ReuseScope.Request);
这是我的服务,它采用这些依赖
public AnalysisServiceStackService(
IEventConditionalLoadingRepository eventConditionalLoadingRepository,
IMetaRiskRepository metaRiskRepository,
IUnitOfWork unitOfWork)
{
_eventConditionalLoadingRepository = eventConditionalLoadingRepository;
_metaRiskRepository = metaRiskRepository;
_unitOfWork = unitOfWork;
_log.Information("AnalysisServiceStackService constructed");
}
你可以看到我在那里有一个日志声明,当我通过邮递员为顺序请求点击这个服务时,我记录了这种事情
2017-03-30 15:34:10 [Information] AnalysisServiceStackService constructed
2017-03-30 15:34:11 [Information] AnalysisServiceStackService constructed
2017-03-30 15:34:12 [Information] AnalysisServiceStackService constructed
2017-03-30 15:34:13 [Information] AnalysisServiceStackService constructed
2017-03-30 15:34:14 [Information] AnalysisServiceStackService constructed
2017-03-30 15:34:15 [Information] AnalysisServiceStackService constructed
2017-03-30 15:34:16 [Information] AnalysisServiceStackService constructed
2017-03-30 15:34:17 [Information] AnalysisServiceStackService constructed
所以我知道服务本身应该尝试解析它所需的IOC组件的新PerRequest实例。
如果我然后使用Visual Studio并使用Debug&#34; Make Object ID&#34;功能来检查IOC容器给我的是同一个实例。
致电1:第一次申请
查看Visual Studio创建的#1对象ID
致电2:第二次请求
请参阅#1对象ID,这意味着它不应该是相同的实例。它应该是新请求的新实例
只是为了证明这些是不同的调用,这里是构造函数运行两次的日志,每次请求一次
只有为我解决此问题的是重用.None
的建议修复但是,如果我必须这样做,我也可以使用AutoFac适配器(我更喜欢使用它)并使用标准的AutoFac INstancePerLifeTimeScope:http://docs.autofac.org/en/latest/lifetime/instance-scope.html#instance-per-lifetime-scope
有什么想法吗?