ConfuserEx icon indicating copy to clipboard operation
ConfuserEx copied to clipboard

.NET Core 2.2 with async methods causes TypeResolveException

Open pulawskig opened this issue 5 years ago • 7 comments

Hello, I'm trying to use ConfuserEx 2 on my .NET Core 2.2 app, but it fails with Exception: dnlib.DotNet.TypeResolveException: Could not resolve type: System.Runtime.CompilerServices.IAsyncStateMachine (System.Runtime, Version=4.2.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a) After some debugging I noticed two things:

  1. System.Runtime.CompilerServices.IAsyncStateMachine is defined in System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e
  2. It only happens when Renamer starts to resolve dependencies on types generated from async-await methods.

I've tried referencing System.Private.CoreLib as external module, but no luck. I've removed any additional options from .crproj so right now it looks like this:

<project outputDir="D:\MyApp\publish\Confused" baseDir="D:\MyApp\publish" xmlns="http://confuser.codeplex.com"> <module path="MyApp.dll" /> <module path="System.Private.CoreLib.dll" external="true" /> </project>

Has anyone experienced something like this and/or knows how to a fix/workaround for it? Thank you for any help and have a nice day :)

pulawskig avatar Jun 14 '19 11:06 pulawskig

Try adding the full absolute path to System.Private.CoreLib.dll into the reference. ConfuserEx can't resolve the path to .NET Core properly as of right now.

mkaring avatar Jun 14 '19 12:06 mkaring

Hey, thanks for answering :) I wasn't sure if you meant in .crproj or in project so I did both, but unfortunately it didn't help. I'm attaching .crproj and .csproj. Also I'm building/publishing for win-x86 if it helps.

MyApp_csproj.txt MyApp_crproj.txt (Had to rename them to attach)

pulawskig avatar Jun 14 '19 13:06 pulawskig

In the csproj file it shouldn’t be required to add this file explicitly. The reference to netcoreapp2.2 should take care of this.

My guess is that other references to .NET Core are missing. Could you try to add the runtime assembly path of .NET Core to the crproj file? You are able to find the directory using the dotnet command line.

mkaring avatar Jun 14 '19 13:06 mkaring

Sorry for the delay. Unfortunately adding the runtime path didn't work (got it via dotnet --list-runtimes command). MyApp_crproj.txt

pulawskig avatar Jun 17 '19 08:06 pulawskig

Could it be connected with #46 ? In it you mentioned a problem with type redirects in .NET Standard and .NET Core. I made two simple exactly the same programs in Standard 2.0 and Core 2.2 ( Program.txt ), and the first went without any problems, and the other got the Exception for IAsyncStateMachine.

pulawskig avatar Jun 18 '19 15:06 pulawskig

I still have this problem in .net 5.

Using the following:

<?xml version="1.0" encoding="utf-8"?>
<project baseDir="." outputDir="." xmlns="http://confuser.codeplex.com">
  <rule preset="minimum" pattern="true">
    <protection id="anti debug" />
    <!--<protection id="anti dump" />-->
    <protection id="anti ildasm" />
    <!--<protection id="anti tamper" />-->
    <protection id="constants" />
    <protection id="ctrl flow" />
    <!--<protection id="invalid metadata" />-->
    <!--<protection id="ref proxy" />-->
    <protection id="rename" />
    <protection id="resources" />
  </rule>
  <module path="ConnectorLib.dll" />
  <probePath>C:\Program Files\dotnet\shared\Microsoft.NETCore.App\5.0.1\</probePath>
</project>

Expectation: It works. It used to when the target was .NET Framework.

Result:

1>Exception: dnlib.DotNet.TypeResolveException: Could not resolve type: System.Runtime.CompilerServices.IAsyncStateMachine (System.Runtime, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a)
1>   at dnlib.DotNet.TypeRef.ResolveThrow(ModuleDef sourceModule) in C:\Users\KatDevsGames\source\repos\ModPhuserEx\dnlib\src\DotNet\TypeRef.cs:line 334

It's not finding some classes that get used implicitly by the async/await keywords. The probePath path definitely contains System.Runtime.dll.

KatDevsGames avatar Jan 13 '21 06:01 KatDevsGames

I added System.Runtime.dll library to target folder and it fixed

Abbossbek avatar Aug 14 '21 09:08 Abbossbek