Fix: “assembly paths must be rooted” in Visual Studio 2019 and .Net Core 3.1
When referencing a COM object in a .Net Core project in Visual Studio 2019 the first build or rebuild always fails with an ‘Assembly paths must be rooted‘ error like:
error : The assembly path obj\Debug\netcoreapp3.1\Interop.AXMMCFGLib.dll is invalid.
Assembly paths must be rooted
The next build should succeed. However, publishing a web project always fails since this relies on rebuilding the entire project.
This affects anyone that needs a reference to a COM object as well as users of the SMS Server API, SMS component or E-mail component using .Net core 3.1.
Unfortunately this is a known bug in Visual Studio. You can find more information about it here. At the time of this writing there is no date set for its resolution by Microsoft.
The best way to fix it requires a quick manual tweak to the project file.
Please follow these steps to fix the problem:
1. Close your visual studio project.
2. Make a backup copy of your visual studio project file (The .csproj file).
2. Open your visual studio project file in Notepad++ or some other file editor.
3. Insert the following snippet in the project file underneath the root node. Its siblings should be other ‘Target’ tags or ‘ItemGroup’ tags.
<Target Name="resolveInteropOutputPath" BeforeTargets="ResolveComReferences"
Condition="'@(COMReference)'!='' or '@(COMFileReference)'!=''">
<PropertyGroup Condition=" '$(InteropOutputPath)' == '' ">
<InteropOutputPath>$(MSBuildProjectDirectory)\$(IntermediateOutputPath)</InteropOutputPath>
</PropertyGroup>
</Target>
4. Save the project file and open it again in Visual Studio.
The ‘Assembly paths must be rooted’ error should now be solved. You should be able to build the project as well as publish without any problems.
This extra target will cause Visual Studio to always have a rooted assembly paths for the COM references in your project when necessary.