dotnet / orleans

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

Code Generator issue on net standard 2.0 #6325

Closed ALEXR92 closed 4 years ago

ALEXR92 commented 4 years ago

Hi, I am trying to migrate my solution to .net standard 2.0 (now under .net framework 4.7.2), but I am facing an issue when compiling on my grain interfaces project using Microsoft.Orleans.OrleansCodeGenerator.Build 3.0.2:

-- Code Generation FAILED --
2>
2>Exc level 0: System.ArgumentException: A BadImageFormatException has been thrown while parsing the signature. This is likely due to lack of a generic context. Ensure genericTypeArguments and genericMethodArguments are provided and contain enough context.
2>   at System.Reflection.RuntimeModule.ResolveType(Int32 metadataToken, Type[] genericTypeArguments, Type[] genericMethodArguments)
2>   at System.Reflection.CustomAttribute.FilterCustomAttributeRecord(MetadataToken caCtorToken, MetadataImport& scope, RuntimeModule decoratedModule, MetadataToken decoratedToken, RuntimeType attributeFilterType, Boolean mustBeInheritable, ListBuilder`1& derivedAttributes, RuntimeType& attributeType, IRuntimeMethodInfo& ctor, Boolean& ctorHasParameters, Boolean& isVarArg)
2>   at System.Reflection.CustomAttribute.AddCustomAttributes(ListBuilder`1& attributes, RuntimeModule decoratedModule, Int32 decoratedMetadataToken, RuntimeType attributeFilterType, Boolean mustBeInheritable, ListBuilder`1 derivedAttributes)
2>   at System.Reflection.CustomAttribute.GetCustomAttributes(RuntimeType type, RuntimeType caType, Boolean inherit)
2>   at System.RuntimeType.GetCustomAttributes(Type attributeType, Boolean inherit)
2>   at System.Attribute.GetCustomAttributes(MemberInfo element, Type type, Boolean inherit)
2>   at System.Attribute.GetCustomAttribute(MemberInfo element, Type attributeType, Boolean inherit)
2>   at System.Reflection.CustomAttributeExtensions.GetCustomAttribute[T](MemberInfo element)
2>   at Orleans.CodeGenerator.RoslynCodeGenerator.AddSerializationTypes(SerializationTypeDescriptions serializationTypes, Assembly targetAssembly, List`1 assemblies) in D:\build\agent\_work\25\s\src\Orleans.CodeGeneration\RoslynCodeGenerator.cs:line 456
2>   at Orleans.CodeGenerator.RoslynCodeGenerator.GenerateCode(Assembly targetAssembly, List`1 assemblies) in D:\build\agent\_work\25\s\src\Orleans.CodeGeneration\RoslynCodeGenerator.cs:line 345
2>   at Orleans.CodeGenerator.RoslynCodeGenerator.GenerateSourceForAssembly(Assembly input) in D:\build\agent\_work\25\s\src\Orleans.CodeGeneration\RoslynCodeGenerator.cs:line 162
2>   at Orleans.CodeGeneration.CodeGenerator.GenerateSourceForAssembly(Assembly grainAssembly, LogLevel logLevel)
2>   at Orleans.CodeGeneration.CodeGenerator.GenerateCodeInternal(CodeGenOptions options)
2>   at Orleans.CodeGeneration.CodeGenerator.GenerateCode(CodeGenOptions options)
2>   at Orleans.CodeGeneration.Program.Main(String[] args)
2>Exc level 1: System.BadImageFormatException: Could not load file or assembly 'System.ServiceModel.Primitives, Version=4.7.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. Reference assemblies should not be loaded for execution.  They can only be loaded in the Reflection-only loader context. (0x80131058)
2>   at System.ModuleHandle.ResolveTypeHandleInternal(RuntimeModule module, Int32 typeToken, RuntimeTypeHandle[] typeInstantiationContext, RuntimeTypeHandle[] methodInstantiationContext)
2>   at System.Reflection.RuntimeModule.ResolveType(Int32 metadataToken, Type[] genericTypeArguments, Type[] genericMethodArguments)
2>Exc level 2: System.BadImageFormatException: Cannot load a reference assembly for execution.

Looks like code generation fails on System.ServiceModel.Primitives assembly under .net standard 2.0 when it didn't under .net framework 4.7.2. Do you have any idea on how to fix this?

Thanks in advance

Rgds

ReubenBond commented 4 years ago

Hi, @ALEXR92, have you tried using the Microsoft.Orleans.CodeGenerator.MSBuild package? It has better support for .NET Standard 2.0 projects.

Apologies for the hassle - I hope to remove the Microsoft.Orleans.OrleansCodeGenerator.Build package in the future and I would consider it deprecated for now.

ALEXR92 commented 4 years ago

Indeed, replacing Microsoft.Orleans.OrleansCodeGenerator.Build with Microsoft.Orleans.CodeGenerator.MSBuild now enables compilation to succeed.

Thank you @ReubenBond !