dotnet / orleans

Cloud Native application framework for .NET
https://docs.microsoft.com/dotnet/orleans
MIT License
10.07k stars 2.03k forks source link

Strange activation exception. Am I missing some registration ? #5484

Closed centur closed 3 years ago

centur commented 5 years ago

Hi, we are getting some strange exception on activation

An exception was thrown while activating λ:Orleans.IGrainFactory -> λ:Orleans.GrainFactory -> Orleans.Runtime.InsideRuntimeClient -> Orleans.Runtime.GrainTypeManager -> λ:Orleans.Serialization.SerializationManager

from what I understand - Orleans can't resolve SerializationManager for the chain of dependencies to activate an instance of IGrainFactory. I'm using AutofacServiceProvider here to wrap and re-register all the types from Microsoft.Extensions DI container. I'm not sure why the error appear as I'd assume Orleans must register all internal types correctly. Searching in the gitter channel shows only one similar case here. But skimming through the conversation and #4524 - it looks like the symptom is similar but not the root case.

Some stats about the setup: Orleans is latest, 2.3, migrating from 1.5.4 Cloud Services for hosting, new project formats everywhere where we can (ccproj is not - so need some binding redirects.) Full framework 4.7.2 iirc. I'm trying to do a cleanup with this migration so i'm not using legacy nugets but building everything from docs and from scratch.

PS: Nested stack is even more confusing:

Autofac.Core.DependencyResolutionException
  HResult=0x80131500
  Message=An exception was thrown while activating λ:Orleans.IGrainFactory -> λ:Orleans.GrainFactory -> Orleans.Runtime.InsideRuntimeClient -> Orleans.Runtime.GrainTypeManager -> λ:Orleans.Serialization.SerializationManager.
  Source=Autofac
  StackTrace:
   at Autofac.Core.Resolving.InstanceLookup.Activate(IEnumerable`1 parameters, Object& decoratorTarget)
   at Autofac.Core.Lifetime.LifetimeScope.GetOrCreateAndShare(Guid id, Func`1 creator)
   at Autofac.Core.Resolving.InstanceLookup.Execute()
   at Autofac.Core.Resolving.ResolveOperation.GetOrCreateInstance(ISharingLifetimeScope currentOperationScope, IComponentRegistration registration, IEnumerable`1 parameters)
   at Autofac.Core.Resolving.ResolveOperation.Execute(IComponentRegistration registration, IEnumerable`1 parameters)
   at Autofac.ResolutionExtensions.TryResolveService(IComponentContext context, Service service, IEnumerable`1 parameters, Object& instance)
   at Autofac.ResolutionExtensions.ResolveOptionalService(IComponentContext context, Service service, IEnumerable`1 parameters)
   at Autofac.Extensions.DependencyInjection.AutofacServiceProvider.GetService(Type serviceType)
   at Microsoft.Extensions.DependencyInjection.ActivatorUtilities.ConstructorMatcher.CreateInstance(IServiceProvider provider)
   at Microsoft.Extensions.DependencyInjection.ActivatorUtilities.CreateInstance[T](IServiceProvider provider, Object[] parameters)
   at System.Lazy`1.CreateValue()
   at System.Lazy`1.LazyInitValue()
   at Orleans.Runtime.KeyedServiceCollection`2.GetService(IServiceProvider services, TKey key)
   at Orleans.Runtime.KeyedServiceCollectionExtensions.GetServiceByKey[TKey,TService](IServiceProvider services, TKey key)
   at Orleans.Streams.PersistentStreamStorageConfigurationValidator.ValidateConfiguration()
   at Orleans.Hosting.SiloHostBuilder.ValidateSystemConfiguration(IServiceProvider serviceProvider)
   at Orleans.Hosting.SiloHostBuilder.Build()
   at DBCloud.ActorsHost.WorkerRole.Run() in C:\Projects\bullclip-backend\DBCloud.ActorsHost\WorkerRole.cs:line 37
   at Microsoft.WindowsAzure.ServiceRuntime.RoleEnvironment.StartRoleInternal()
   at Microsoft.WindowsAzure.ServiceRuntime.Implementation.Loader.RoleRuntimeBridge.<StartRole>b__2()
   at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Threading.ThreadHelper.ThreadStart()

Inner Exception 1:
TargetInvocationException: Exception has been thrown by the target of an invocation.

Inner Exception 2:
TypeInitializationException: The type initializer for 'Orleans.Runtime.Constants' threw an exception.

Inner Exception 3:
BadImageFormatException: Could not load file or assembly 'System.Memory, Version=4.0.1.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51' or one of its dependencies. Reference assemblies should not be loaded for execution.  They can only be loaded in the Reflection-only loader context. (Exception from HRESULT: 0x80131058)

Inner Exception 4:
BadImageFormatException: Cannot load a reference assembly for execution.
ReubenBond commented 5 years ago

The exception messages indicate that the issue is at build time - in particular, the last exception implies that a reference assembly (from a folder with /ref/ somewhere in the path) is being copied into the published ccproj. Perhaps there is something wrong with a <HintPath> in the ccproj? I'm not sure, let's take a look together. Feel free to message me.

centur commented 5 years ago

I'm not sure how you inferred the issue is at build time. It builds correctly and initializes half-way through. I still have to go through some painful binding redirects because all outputs of these projects in a new format are being referenced in old ones - thus need to do it. Don't know how to get rid of it, but it's a sidetrack. Messaged you, keen to understand more where you spotted /ref/ part ,unless it's based on internal knowledge or similar errors you saw before.

centur commented 5 years ago

So far, installing System.Memory nuget solves the above problem, but I'm reaching a next one. It's the same and I hope I can solve them all using the same practice - install missing nugets one by one. I'm going to post all exceptions to this issue so next poor soul can find this in search.

centur commented 5 years ago
Autofac.Core.DependencyResolutionException
  HResult=0x80131500
  Message=An exception was thrown while activating Orleans.Hosting.SiloWrapper -> Orleans.Runtime.Silo.
  Source=Autofac
  StackTrace:
   at Autofac.Core.Resolving.InstanceLookup.Activate(IEnumerable`1 parameters, Object& decoratorTarget)
   at Autofac.Core.Lifetime.LifetimeScope.GetOrCreateAndShare(Guid id, Func`1 creator)
   at Autofac.Core.Resolving.InstanceLookup.Execute()
   at Autofac.Core.Resolving.ResolveOperation.GetOrCreateInstance(ISharingLifetimeScope currentOperationScope, IComponentRegistration registration, IEnumerable`1 parameters)
   at Autofac.Core.Resolving.ResolveOperation.Execute(IComponentRegistration registration, IEnumerable`1 parameters)
   at Autofac.ResolutionExtensions.TryResolveService(IComponentContext context, Service service, IEnumerable`1 parameters, Object& instance)
   at Autofac.ResolutionExtensions.ResolveService(IComponentContext context, Service service, IEnumerable`1 parameters)
   at Autofac.Extensions.DependencyInjection.AutofacServiceProvider.GetRequiredService(Type serviceType)
   at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService[T](IServiceProvider provider)
   at DBCloud.ActorsHost.WorkerRole.Run() in C:\Projects\bullclip-backend\DBCloud.ActorsHost\WorkerRole.cs:line 38
   at Microsoft.WindowsAzure.ServiceRuntime.RoleEnvironment.StartRoleInternal()
   at Microsoft.WindowsAzure.ServiceRuntime.Implementation.Loader.RoleRuntimeBridge.<StartRole>b__2()
   at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Threading.ThreadHelper.ThreadStart()

Inner Exception 1:
DependencyResolutionException: An exception was thrown while invoking the constructor 'Void .ctor(Orleans.Runtime.ILocalSiloDetails, System.IServiceProvider)' on type 'Silo'.

Inner Exception 2:
DependencyResolutionException: An exception was thrown while activating λ:Orleans.Runtime.Dispatcher -> Orleans.Runtime.Catalog -> λ:Orleans.Streams.IStreamProviderRuntime -> Orleans.Runtime.Providers.SiloProviderRuntime -> λ:Orleans.Runtime.ConsistentRing.IConsistentRingProvider.

Inner Exception 3:
BadImageFormatException: Could not load file or assembly 'System.Runtime.CompilerServices.Unsafe, Version=4.0.4.1, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. Reference assemblies should not be loaded for execution.  They can only be loaded in the Reflection-only loader context. (Exception from HRESULT: 0x80131058)

Inner Exception 4:
BadImageFormatException: Cannot load a reference assembly for execution.
centur commented 5 years ago

This is the message I'm getting now. It looks the same, but it's not the same error really - Previously, I've resolved every single assemblyBinding error I had, (with the help of fuslogw) so now it doesn't look like a binding error..

ReubenBond commented 5 years ago

I'm not sure how you inferred the issue is at build time.

The error message mentions that System.Runtime.CompilerServices.Unsafe is a "reference assembly". References assemblies should only ever be present at build time, not when the app is published/deployed. Those assemblies don't have any method bodies and they cannot be loaded by the runtime. They exist to speed up compilation (they're smaller so they load more quickly)

ReubenBond commented 5 years ago

Closing for now, but we can re-open if needed

RyanSmith3402443 commented 3 years ago

Hi, Check whether there is any circular reference between Orleans.Hosting.SiloWrapper and Orleans.Runtime.Silo