Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exception of ArgumentOutOfRangeException and ObjectDisposedException #45

Closed
kulend opened this issue Sep 6, 2017 · 13 comments

Comments

@kulend
Copy link

commented Sep 6, 2017

RabbitMQ&Mysql环境

当我publish一条消息后,如果没有在Controller添加任何CapSubscribe属性的action,则报以下错误
System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index
at System.ThrowHelper.ThrowArgumentOutOfRange_IndexException()
at System.Collections.Generic.List1.get_Item(Int32 index) at DotNetCore.CAP.SubscibeQueueExecutor.<ExecuteSubscribeAsync>d__7.MoveNext()
然后写了CapSubscribe属性的action,则会报以下出错信息
System.ObjectDisposedException: Instances cannot be resolved and nested lifetimes cannot be created from this LifetimeScope as it has already been disposed. at Autofac.Core.Lifetime.LifetimeScope.CheckNotDisposed() at Autofac.Core.Lifetime.LifetimeScope.ResolveComponent(IComponentRegistration registration, IEnumerable1 parameters)
at Autofac.ResolutionExtensions.TryResolveService(IComponentContext context, Service service, IEnumerable1 parameters, Object& instance) at Autofac.ResolutionExtensions.ResolveOptionalService(IComponentContext context, Service service, IEnumerable1 parameters)
at Microsoft.Extensions.Internal.ActivatorUtilities.ConstructorMatcher.CreateInstance(IServiceProvider provider)
at Microsoft.Extensions.Internal.ActivatorUtilities.CreateInstance(IServiceProvider provider, Type instanceType, Object[] parameters)
at DotNetCore.CAP.Internal.DefaultConsumerInvoker.d__6.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at DotNetCore.CAP.SubscibeQueueExecutor.d__7.MoveNext()
`

@yang-xiaodong

This comment has been minimized.

Copy link
Member

commented Sep 6, 2017

你使用最新的 2.0.1-preview-21392243 版本,看是否还有此问题?

@kulend

This comment has been minimized.

Copy link
Author

commented Sep 8, 2017

还是有问题。
_20170908103345
这里需要实例化controller,但貌似已经不在一个请求的生命周期内了。

@yang-xiaodong

This comment has been minimized.

Copy link
Member

commented Sep 8, 2017

Controller 和 CAP Subscribe 的调用是不在一个生命周期的

@yang-xiaodong

This comment has been minimized.

Copy link
Member

commented Sep 8, 2017

你贴一下你的代码,我怎么样才能复现你的问题?

@kulend

This comment has been minimized.

Copy link
Author

commented Sep 8, 2017

发现问题所在了,controller的构造函数是这样的
private IUserActionLogService service; public LogController(IUserActionLogService service) { this.service = service; }
注册service是InstancePerLifetimeScope
builder.RegisterAssemblyTypes(typeof(IUserService).GetTypeInfo().Assembly) .Where(t => t.Name.EndsWith("Service", StringComparison.CurrentCultureIgnoreCase)) .AsImplementedInterfaces() .InstancePerLifetimeScope();
这样IUserActionLogService就没法实例化,然后就报上面的错了。
不知道大神有没有好的解决办法?

@kulend

This comment has been minimized.

Copy link
Author

commented Sep 8, 2017

使用ICapSubscribe Service也有同样的问题,这个问题希望能解决下,总不可能Subscribe用到的都必须注册成Singleton或是Transient吧?

@yang-xiaodong

This comment has been minimized.

Copy link
Member

commented Sep 8, 2017

Scope的是没有问题的,你用的Autofac?

@kulend

This comment has been minimized.

Copy link
Author

commented Sep 8, 2017

@yuleyule66

是的,用的是Autofac

@yang-xiaodong

This comment has been minimized.

Copy link
Member

commented Sep 8, 2017

没有测试过Autofac,CAP内部拿Services都是微软的DI的,建议切换为微软DI

@kulend

This comment has been minimized.

Copy link
Author

commented Sep 11, 2017

亲测真和autofac有关系,那这个是autofac的锅?

@kulend

This comment has been minimized.

Copy link
Author

commented Sep 11, 2017

@yuleyule66
我调试了cap的代码,貌似是有点问题。
在\src\DotNetCore.CAP\IQueueExecutor.Subscibe.cs第107行:
await _consumerInvokerFactory.CreateInvoker(consumerContext).InvokeAsync();
CreateInvoker里面,using (var scope = _serviceProvider.CreateScope()),但返回了才去InvokeAsync,那这个scope已dispose掉了吧。Invoke会去实例化controller,然后就报错了。
这个地方我把InvokeAsync移到using里面测试就正常了。
public async Task CreateInvokerAndInvokeAsync(ConsumerContext consumerContext)
{
using (var scope = _serviceProvider.CreateScope())
{
var context = new ConsumerInvokerContext(consumerContext)
{
Result = new DefaultConsumerInvoker(_logger, scope.ServiceProvider, _modelBinderFactory, consumerContext)
};
await context.Result.InvokeAsync();
}
}

@yang-xiaodong

This comment has been minimized.

Copy link
Member

commented Sep 12, 2017

这里看起来好像有问题,但是我这边复现不到呢? 你是怎么测试出来的呢?

yang-xiaodong added a commit that referenced this issue Sep 12, 2017
@yang-xiaodong

This comment has been minimized.

Copy link
Member

commented Sep 12, 2017

@kulend 升级 2.0.1-preview-21993007 再看一下有没有问题呢

@yang-xiaodong yang-xiaodong changed the title ArgumentOutOfRangeException或ObjectDisposedException异常 Exception of ArgumentOutOfRangeException and ObjectDisposedException Nov 25, 2017

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants
You can’t perform that action at this time.