dsuryd / dotNetify

Simple, lightweight, yet powerful way to build real-time web apps.
https://dotnetify.net
Other
1.17k stars 164 forks source link

AspectCore.DynamicProxy.AspectInvocationException: Exception has been thrown by the aspect of an invocation. #146

Closed qcjxberin closed 5 years ago

qcjxberin commented 5 years ago

`public class HelloWorld : BaseVM { private Timer _timer; public string Greetings => "Hello World!"; public DateTime ServerTime => DateTime.Now;

    public HelloWorld()
    {
        _timer = new Timer(state =>
        {
            Changed(nameof(ServerTime));
            PushUpdates();
        }, null, 0, 10000); // every 1000 ms.
    }
    public override void Dispose() => _timer.Dispose();
}`

Unhandled Exception: AspectCore.DynamicProxy.AspectInvocationException: Exception has been thrown by the aspect of an invocation. ---> Instances cannot be resolved and nested lifetimes cannot be created from this LifetimeScope as it has already been disposed.. ---> 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() in C:\projects\autofac\src\Autofac\Core\Lifetime\LifetimeScope.cs:line 369 at Autofac.Core.Lifetime.LifetimeScope.ResolveComponent(IComponentRegistration registration, IEnumerable1 parameters) in C:\projects\autofac\src\Autofac\Core\Lifetime\LifetimeScope.cs:line 278 at Autofac.ResolutionExtensions.TryResolveService(IComponentContext context, Service service, IEnumerable1 parameters, Object& instance) in C:\projects\autofac\src\Autofac\ResolutionExtensions.cs:line 1031 at Autofac.ResolutionExtensions.ResolveOptionalService(IComponentContext context, Service service, IEnumerable1 parameters) in C:\projects\autofac\src\Autofac\ResolutionExtensions.cs:line 814 at Autofac.ResolutionExtensions.ResolveOptional(IComponentContext context, Type serviceType, IEnumerable1 parameters) in C:\projects\autofac\src\Autofac\ResolutionExtensions.cs:line 644 at Autofac.ResolutionExtensions.ResolveOptional(IComponentContext context, Type serviceType) in C:\projects\autofac\src\Autofac\ResolutionExtensions.cs:line 628 at Autofac.Extensions.DependencyInjection.AutofacServiceProvider.GetService(Type serviceType) in C:\projects\autofac-extensions-dependencyinjection\src\Autofac.Extensions.DependencyInjection\AutofacServiceProvider.cs:line 86 at AspectCore.DynamicProxy.Parameters.EnableParameterAspectInterceptor.Invoke(AspectContext context, AspectDelegate next) --- End of inner exception stack trace --- at AspectCore.DynamicProxy.AspectActivator.Invoke[TResult](AspectActivatorContext activatorContext) at AspectCore.DynamicGenerated.HubPipeline.RunExceptionMiddleware(HubCallerContext context, Exception exception) at DotNetify.DotNetifyHub.RunRespondingVMFilters(String vmId, BaseVM vm, Object vmData, Action`1 vmAction) at DotNetify.VMController.PushUpdates(VMInfo vmInfo, String vmData) at DotNetify.VMController.PushUpdates(VMInfo vmInfo) at DotNetify.VMController.VmInstance_RequestPushUpdates(Object sender, EventArgs e) at DotNetify.BaseVM.PushUpdates() at FivePowerHEMS.Test.HelloWorld.<.ctor>b__5_0(Object state) in E:\Code\南宁\医院设备科管理系统\FivePowerHEMS\src\FivePowerHEMS\Test\HelloWorld.cs:line 18 at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state) --- End of stack trace from previous location where exception was thrown --- at System.Threading.TimerQueueTimer.CallCallback() at System.Threading.TimerQueueTimer.Fire() at System.Threading.TimerQueue.FireNextTimers()

qcjxberin commented 5 years ago

当使用时发现有如上的错误,不知道该如何解决。

dsuryd commented 5 years ago

Sorry, I don't read Chinese, and your stack trace indicates DI resolution failure from some library that dotNetify isn't designed to work with.

qcjxberin commented 5 years ago

How to solve this problem? If you do not use that timing, you can get the default data normally.

dsuryd commented 5 years ago

The exception is from AspectCore, failing to resolve a service from an Autofac scope as the scope has been disposed. You need to debug AspectCore and find what service it was, and why it tried to resolve the service from a timer thread.

qcjxberin commented 5 years ago

/// <summary> /// 启用Aop /// </summary> /// <param name="builder">容器生成器</param> /// <param name="configAction">Aop配置</param> public static void EnableAop( this ContainerBuilder builder,Action<IAspectConfiguration> configAction = null ) { builder.RegisterDynamicProxy( config => { config.EnableParameterAspect(); config.NonAspectPredicates.Add( t => Reflection.GetTopBaseType( t.DeclaringType ).SafeString() == "Microsoft.EntityFrameworkCore.DbContext" ); config.NonAspectPredicates.Add(t => t.DeclaringType.Namespace.Contains("DotNetify")); configAction?.Invoke( config ); } ); builder.EnableAspectScoped(); }

It is a problem with AOP, and it can be solved by eliminating it.