Obfuscar for asp.net core 3.1

3.2k Views Asked by At

In the ASP.NET Core 3.1 app, the given path in the obfuscar.xml is correct but it is not able to find the assembly/Failed to resolve assembly:

<?xml version='1.0'?>
<Obfuscator>
  <Var name="InPath" value=".\bin\Debug\netcoreapp3.1" />
  <Var name="OutPath" value="$(InPath)\publish" />
  <Var name="KeepPublicApi" value="false" />
  <Var name="HidePrivateApi" value="true" />
  <Var name="RenameProperties" value="true" />
  <Var name="RenameEvents" value="true" />
  <Var name="RenameFields" value="true" />
  <Var name="UseUnicodeNames" value="true" />
  <Var name="HideStrings" value="true" />
  <Var name="OptimizeMethods" value="true" />
  <Var name="SuppressIldasm" value="true" />
  <Module file="$(InPath)\Sample.dll" />
</Obfuscator>

The build error:

4>Note that Rollbar API is enabled by default to collect crashes. If you want to opt out, please run with -s switch
4>Loading project .\obfuscar.xml...
4>An error occurred during processing:
4>Unable to find assembly:  .\bin\Debug\netcoreapp3.1\Sample.dll
4>Failed to resolve assembly: 'Microsoft.AspNetCore.Mvc.Core, Version=3.1.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60'
4>G:\SomePath\Sample.csproj(110,5): error MSB3073: The command "if Debug == Debug obfuscar.console .\obfuscar.xml" exited with code 1.
4>Done building project "Sample.csproj" -- FAILED.
2

There are 2 best solutions below

2
On

Use

<AssemblySearchPath path="C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App\5.0.5" />
0
On

This combination worked for me on netcoreapp3.1

Build Command

Make sure you use publish as well as --self-contained and -r to ensure all necessary dependencies are copied to the output directory.

dotnet publish -c Release --self-contained=true -r win-x64 <project.csproj>

project.csproj

  <ItemGroup>
    <Content Include="obfuscar.xml">
      <CopyToOutputDirectory>Always</CopyToOutputDirectory>
    </Content>
  </ItemGroup>

  <ItemGroup>
    <PackageReference Include="Obfuscar">
      <Version>2.2.14</Version>
      <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
      <PrivateAssets>all</PrivateAssets>
    </PackageReference>
  </ItemGroup>

  <PropertyGroup>
    <PostBuildEvent>$(Obfuscar) obfuscar.xml</PostBuildEvent>
  </PropertyGroup>

Obfuscar.xml

<?xml version='1.0'?>
<Obfuscator>
  <Var name="InPath" value="." />
  <Var name="OutPath" value=".\Obfuscator_Output" />
  <Var name="KeepPublicApi" value="true" />
  <Var name="HidePrivateApi" value="true" />
  <Var name="RenameProperties" value="true" />
  <Var name="RenameEvents" value="true" />
  <Var name="RenameFields" value="true" />
  <Var name="UseUnicodeNames" value="true" />
  <Var name="HideStrings" value="true" />
  <Var name="OptimizeMethods" value="true" />
  <Var name="SuppressIldasm" value="true" />
  <Module file="$(InPath)\Project.dll" />
</Obfuscator>