Search code examples
c#visual-studiodllnuget

How NOT to debug a referenced dll?


I'm using a Release version of a dll which is referenced in other project. This dll has tampering detection when debugger is running (dll is also mine, I don't plan to do something illegal) which gives false positive results when debugging.

Is there an option in Visual Studio (2017-1019) not to "debug" referenced dll, but still be able to debug code which is being developed? "Debug" means that I don't want to Step-In in this specific dll, but just to get the data from the method the same way I could get it in Release.

E.g:

//Release dll
int Sum (int a, int b)
{
bool TamperingDetected = CheckForTampering();
if (TamperingDetected)
  return 0;
else
  return a + b;
}

//Other project
MessageBox.Show(dll.Sum(1, 1));

If I run this part of the code in Release I can easily get the correct value. No tampering will be detected, if someone would like to reverse engineer this method, it's very easy to do it in Release "legally". If I run in Debug, I will always get 0. As said before, I don't want to Step-In in Sum method, so no debugging should be done in this dll, but everything else should work like in Debug.

Just My Code option makes no difference.

The background is that one team creates this dll with tampering detection and the other is using it as a NuGet. Now they cannot debug their part of the code since our is detecting tampering and returning false values. It would be too much for the other team to have the source code, since we then risk that they do something wrong during the build on that part of the code which they don't know.

One option would be to give them Debug and Release dll's and they could switch between them like explained here: https://stackoverflow.com/a/7284102, but then we lose the simplicity of sharing a NuGet and we still risk that Debug dll is copied instead of Release dll, thus losing tampering detection in the final product. NuGet is then always in Release.

Switching NuGets automatically with switching the Debug and Release configuration is not possible, or I still didn't find a way to it, but this would also be a good option. Only if some completely other project is used just to download both Debug and Release NuGets and then using the above mentioned answer, but I don't like it either.


Solution

  • Since there is a very small chance that it's possible to do what I first wanted, second best solution is to automatically switch between NuGets and this works with Choose-When-Otherwise. Condition in PackageReference or in ItemGroup doesn't work (at least not in VisualStudio, according to this). So I'll create two NuGets, one will be Release with tampering detection and the other will also be Release but without tampering detection ("-debug" suffix is added to this version). NuGet is then not installed through NuGet Manager but only the dllVersion is updated in .csproj:

    <PropertyGroup>
      <dllVersion>1.2.3</dllVersion>
    </PropertyGroup>
    <Choose>
      <When Condition="'$(Configuration)'=='Debug'">
        <ItemGroup>
          <PackageReference Include="dllNuGet" Version=$(dllVersion)-debug/>
        </ItemGroup>
      </When>
      <Otherwise>
        <ItemGroup>
          <PackageReference Include="dllNuGet" Version=$(dllVersion)/>
        </ItemGroup>
      </Otherwise>
    </Choose>