Visual Studio 2017中Fody-Costura与Obfuscar能否结合使用?
Absolutely, you can use these two tools together—you just need to tweak the MSBuild target execution order to make sure Obfuscar runs before Costura processes your dependencies. Here's how to fix the "missing dependencies" error you're seeing:
The Core Issue
Costura’s job is to embed dependencies into your executable, which means it will either replace original dependency files with embedded versions or remove them from your output directory. Right now, Costura is running first, so when Obfuscar tries to process those dependencies, they’re already gone or modified. We need to flip that order: let Obfuscar obfuscate all assemblies (main + dependencies) first, then let Costura embed the obfuscated dependencies.
Step-by-Step Fixes
1. Adjust Obfuscar’s Execution Target via Project File
MSBuild.Obfuscar lets you control when its Obfuscate target runs using the ObfuscateBeforeTargets property. Since Costura runs as part of Fody’s core FodyWeave target, we can tell Obfuscar to run right before that:
Open your project’s .csproj file (right-click project → Edit
<PropertyGroup> <!-- Make Obfuscar run before Fody/Costura weaves the assembly --> <ObfuscateBeforeTargets>FodyWeave</ObfuscateBeforeTargets> </PropertyGroup>
This ensures Obfuscar processes all your unmodified dependencies while they’re still in the output directory.
2. Force Target Order with a Custom Target (Fallback)
If the above doesn’t work (depending on your specific Fody/Costura version), you can explicitly create a target that triggers Obfuscar before Costura runs:
Add this to your .csproj file:
<Target Name="RunObfuscateBeforeCostura" BeforeTargets="FodyWeave"> <CallTarget Targets="Obfuscate" /> </Target>
This custom target will execute the Obfuscate target immediately before Fody starts weaving (which includes Costura’s embedding step).
Verification Tips
- Clean and Rebuild: Always clean your project first (
Build → Clean Solution) then rebuild to ensure the new target order is respected. - Check MSBuild Logs: If you’re still having issues, run MSBuild with detailed logging to confirm the order. Open the Developer Command Prompt for VS 2017, navigate to your project directory, and run:
Look for entries formsbuild /v:detailedObfuscateandFodyWeave—you should seeObfuscateexecute first.
Key Notes
- Make sure Obfuscar is configured to process all your dependency assemblies (not just the main executable). Double-check your
Obfuscar.xmlconfig to include any DLLs you want obfuscated before embedding. - After obfuscation, Costura will embed the obfuscated versions of your dependencies, so your final executable will have both obfuscated code and embedded dependencies as intended.
内容的提问来源于stack exchange,提问作者TheGuy

