dotnet / dotnet/orleans

Orleans 7.0 Serializer not working with POCOs originating from Nuget package

Open
#8,312 12 comments 0 reactions 0 assignees View on GitHub
area-serialization
Dominant language
C#
Stars
10.9k
Forks
2.1k
Avg merge
14h 42m
Merged PRs (30d)
354

Description

I have a Orleans 7.0 project with and I'm having an issue with the new default serializer.

The old serializer in Orleans 3.6.5 worked fine and it was serializing all the models and Dtos in the several internal Nuget packages.

Now in Orleans 7.0 the project does not recognize the models in the Nuget packages and I get this error:

```
Orleans.Serialization.CodecNotFoundException: Could not find a copier for type InternalProject.Model.
   at Orleans.Serialization.Serializers.CodecProvider.ThrowCopierNotFound(Type type) in /_/src/Orleans.Serialization/Serializers/CodecProvider.cs:line 666
   at Orleans.Serialization.Serializers.CodecProvider.GetDeepCopier[T]() in /_/src/Orleans.Serialization/Serializers/CodecProvider.cs:line 300
   at Orleans.Serialization.ServiceCollectionExtensions.CopierHolder`1.get_Value() in /_/src/Orleans.Serialization/Hosting/ServiceCollectionExtensions.cs:line 203
   at Orleans.Serialization.GeneratedCodeHelpers.OrleansGeneratedCodeHelper.GetService[TService](Object caller, ICodecProvider codecProvider) in /_/src/Orleans.Serialization/GeneratedCodeHelpers/OrleansGeneratedCodeHelper.cs:lin
e 75
   at OrleansCodeGen.InternalProject.Model.Copier_SpecificModel..ctor(ICodecProvider codecProvider) in C:\Projects\InternalProject.Model\Orleans.CodeGenerator\Orleans.CodeGenerator.Orle
ansSerializationSourceGenerator\InternalProject.Model.orleans.g.cs:line 20736
   at System.RuntimeMethodHandle.InvokeMethod(Object target, Void** arguments, Signature sig, Boolean isConstructor)
   at System.Reflection.ConstructorInvoker.Invoke(Object obj, IntPtr* args, BindingFlags invokeAttr)
   at System.Reflection.RuntimeConstructorInfo.Invoke(BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
   at Microsoft.Extensions.DependencyInjection.ActivatorUtilities.ConstructorMatcher.CreateInstance(IServiceProvider provider)
   at Microsoft.Extensions.DependencyInjection.ActivatorUtilities.CreateInstance(IServiceProvider provider, Type instanceType, Object[] parameters)
   at Microsoft.Extensions.DependencyInjection.ActivatorUtilities.GetServiceOrCreateInstance[T](IServiceProvider provider)
   at Orleans.Serialization.GeneratedCodeHelpers.OrleansGeneratedCodeHelper.GetService[TService](Object caller, ICodecProvider codecProvider) in /_/src/Orleans.Serialization/GeneratedCodeHelpers/OrleansGeneratedCodeHelper.cs:lin
e 72
```

Now for the interesting part: when I copy paste the exact same class in my main project I no longer get this error, and the class serializes perfectly fine.

I tried referencing the Nuget project directly in the main project(ProjectReference), the result is the same.

I only get rid of errors in two scenarios:
1. when I use the Json Serializer with IgnoreCycles (undesirable in my scenario, doesn't get rid of the underlying problem)

```
siloBuilder.Services.AddSerializer(sb =>
{
sb.AddJsonSerializer(
isSupported: type => type.Namespace.StartsWith("Nuget.Namespace"),
new JsonSerializerOptions()
{
ReferenceHandler = ReferenceHandler.IgnoreCycles
}
);
});
```
2. when I add the classes directly in the main project. Example class from the error:

```
[GenerateSerializer]
public sealed class Class1: Class1Base
{
public Class1()
{

}

public Class1(IRequest request, bool boolean, InternalClass internalClass)
: base(request, boolean)
{
InternalClass = internalClass;
}

[Id(0)]
public InternalClass InternalClass{ get; set; }

}

[GenerateSerializer]
public class Class1Base
{
public Class1Base()
{

}
public Class1Base(IRequest request, bool boolean)
{
Request = request;
Boolean= boolean;
}

[Id(0)]
public IRequest Request { get; set; }
[Id(1)]
public bool Boolean{ get; set; }
}
```

Contributor guide

Open the contributing guide

Research direction

Start by comparing the generated Orleans serialization source for the NuGet-referenced POCOs with the same classes in the main project. Inspect Orleans.Serialization/Serializers/CodecProvider.cs around lines 300 and 666, then review the generated .orleans.g.cs file and the serializer registration shown in the report. Done means identifying why a copier is missing for the package types and reproducing a working serialization case without the JSON IgnoreCycles workaround.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
backend, distributed-systems
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
28/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.