MagicOnion icon indicating copy to clipboard operation
MagicOnion copied to clipboard

The code generator cannot find System.Threading.Tasks.Task

Open AntonC9018 opened this issue 2 years ago • 1 comments

After I run the code generator, it writes to the console failed to get metadata of System.Threading.Tasks.Task, after which it breaks on the hub implementation example with the error System.InvalidOperationException: A return type of a method must be 'void', 'UnaryResult<T>', 'Task<T>', 'Task'.. The methods in the example return a task, so the error is, I would guess, because it wasn't able to find the task type. Would it be because the task assembly wasn't referenced in the csproj, do you think? It happens both when trying to reference just the subproject that I need to generate code for, and when trying to reference the whole Assembly-CSharp.csproj, albeit when trying the latter it takes more than a minute to load everything. Here's the stack trace:

System.InvalidOperationException: A return type of a method must be 'void', 'UnaryResult<T>', 'Task<T>', 'Task'. (Method: IGamingHub.LeaveAsync(), Return: System.Threading.Tasks.Task)
   at MagicOnion.CodeAnalysis.MethodCollector.ExtractRequestResponseType(IMethodSymbol method, MethodType& methodType, String& requestType, String& responseType, ITypeSymbol& unwrappedOriginalResponseType) in /home/runner/work/MagicOnion/MagicOnion/src/MagicOnion.GeneratorCore/CodeAnalysis/MethodCollector.cs:line 264
   at MagicOnion.CodeAnalysis.MethodCollector.CreateMethodDefinition(IMethodSymbol y) in /home/runner/work/MagicOnion/MagicOnion/src/MagicOnion.GeneratorCore/CodeAnalysis/MethodCollector.cs:line 211
   at System.Linq.Enumerable.SelectEnumerableIterator`2.ToArray()
   at MagicOnion.CodeAnalysis.MethodCollector.<CollectHubInterface>b__8_0(INamedTypeSymbol x) in /home/runner/work/MagicOnion/MagicOnion/src/MagicOnion.GeneratorCore/CodeAnalysis/MethodCollector.cs:line 158
   at System.Linq.Enumerable.SelectArrayIterator`2.ToArray()
   at System.Linq.Buffer`1..ctor(IEnumerable`1 source)
   at System.Linq.OrderedEnumerable`1.ToArray()
   at System.Linq.Enumerable.ToArray[TSource](IEnumerable`1 source)
   at MagicOnion.CodeAnalysis.MethodCollector.CollectHubInterface() in /home/runner/work/MagicOnion/MagicOnion/src/MagicOnion.GeneratorCore/CodeAnalysis/MethodCollector.cs:line 155
   at MagicOnion.MagicOnionCompiler.GenerateFileAsync(String input, String output, Boolean unuseUnityAttr, String namespace, String conditionalSymbol, String messagePackGeneratedNamespace) in /home/runner/work/MagicOnion/MagicOnion/src/MagicOnion.GeneratorCore/MagicOnionCompiler.cs:line 52
   at MagicOnion.Generator.Program.RunAsync(String input, String output, Boolean unuseUnityAttr, String namespace, String messagePackGeneratedNamespace, String conditionalSymbol) in /home/runner/work/MagicOnion/MagicOnion/src/MagicOnion.Generator/Program.cs:line 30
   at ConsoleAppFramework.ConsoleAppEngine.RunCore(ConsoleAppContext ctx, Type type, MethodInfo methodInfo, String[] args, Int32 argsOffset)

I'm running the latest release (4.5.2) approximately like this:

"dotnet" tool run dotnet-moc -input D:\projects\UnityProject\Assembly-CSharp.csproj -output D:\projects\UnityProject\Assets\Source\MagicOnion\Runtime\MessagePackGenerated -unuseUnityAttr -namespace Generated -messagePackGeneratedNamespace Generated -conditionalSymbols UNITY_EDITOR

AntonC9018 avatar Sep 10 '22 17:09 AntonC9018

I think the filtering is wrong which makes the runtime get imported twice. Removing the .NET Core standard references added in manually and just letting it load on its own, even with the filtering enabled, made the error go away. It works fine even if I remove the filtering. I'm talking about this line. So the solution would be to either improve the filtering, or possibly remove it. I can't put my finger on whether it will work for older unity versions with that removed though. My version is 2021.

AntonC9018 avatar Sep 13 '22 09:09 AntonC9018

@AntonC9018 Please try to set your server side .csproj instead of Assembly-CSharp.csproj.

yKimisaki avatar Nov 06 '22 09:11 yKimisaki

@yKimisaki I can't tho, there would be no Unity references then.

AntonC9018 avatar Nov 06 '22 09:11 AntonC9018

For Vector3 and stuff

AntonC9018 avatar Nov 06 '22 09:11 AntonC9018

And if you mean the csproj with the code I need code generation for, then I had already done that at the time of writing, as mentioned in the post

AntonC9018 avatar Nov 06 '22 09:11 AntonC9018

Sorry, I'm not good at English, so I may have missed and miscommunicated.

I had a same issue(failed to get metadata of System.Threading.Tasks.Task) with the command below. dotnet moc -i C:\Works\MyProject\MyProject.UnityClient\Assembly-CSharp.csproj

I resolved this issue with the command below. dotnet moc -i C:\Works\MyProject\MyProject.Server\MyProject.Server.csproj

MyProject.Server.csproj and Assembly-CSharp.csproj share .cs files of streaming hub definition interfaces. I added MessagePack.UnityShims nuget package to server side project. MessagePack.UnityShims allows to use Unity types(for example UnityEngine.Vector3) on server side project.

yKimisaki avatar Nov 06 '22 09:11 yKimisaki

This issue is stale because it has been open 180 days with no activity. Remove stale label or comment or this will be closed in 30 days.

github-actions[bot] avatar May 06 '23 00:05 github-actions[bot]