Unresolved assembly reference

Topics: Developer Forum, User Forum
Jan 13, 2011 at 9:38 AM

Hi All,

I made a project to create an help file for a dll (named "A.DLL") which contains a reference to another assembly (named "B.DLL").

When I tried to compile the documentation project, MRefBuilder exited with an error:

MREFBUILDER : error : Unresolved assembly reference: B (B, Version=2.0.5022.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35) required by A
    Last step completed in 00:00:23.9664

I added the correct reference to the project, but the error is always raised.

I found that adding B.DLL to the documentation project as a documentation source, the error is fixed, but I really do not want to document the B.DLL which is a third party tool.

It seems that MRefBuilder is not reveiving the correct /dep switch.

Do you have any suggestion?

Thanks in advance

Stefano

Jan 13, 2011 at 3:59 PM
Edited Jan 13, 2011 at 7:30 PM

I'm experiencing a similar problem and have tracked it down to a MSBUILD issue in its "ResolveReferences" pass in the generated "GenerateRefInfo.proj" file. The references are correctly being sent to this task, but they're not being resolved correctly.

In particular, this happens when the referenced assembly (e.g. "B.dll") is in another directory from "A.DLL" and is a .NET 4.0 assembly. This does not happen with similar setup on .NET 2.0 assemblies.

Does your "B.DLL" happen to be a .NET 4.0 assembly as well?

This seems to ultimately be caused by SHFB running the 3.5 version of MSBUILD instead of the 4.0 one.

I tried updating all my project files to have a

ToolsVersion="4.0"

(Unfortunately, the GUI always reverts it back to "3.5")

But that didn't seem to work, so I temporarily hacked around it by updating SandcastleProject.cs and places where an MSBuild Project is created. I'm sure missing something basic but this gets me going for now without having to update SHFB to propagate a /tv flag or do this automatically when the SHFB project specifies a FrameworkVersion:

msBuildProject = new Project(Engine.GlobalEngine, GetMSBuildToolsVersionFromContents(cloneProject.MSBuildProject.Xml));
...
msBuildProject = new Project(Engine.GlobalEngine, GetMSBuildToolsVersionFromPath(filename));
...
msBuildProject = new Project(Engine.GlobalEngine, SandcastleProject.GetMSBuildToolsVersionFromPath(projectFile));

and


        // HACK: Gets around ToolsVersion not being obeyed issue
        public static string GetMSBuildToolsVersionFromPath(string path)
        {
            try
            {
                if (File.Exists(path))
                {
                    return GetMSBuildToolsVersionFromContents(File.ReadAllText(path));
                }
            }
            catch (Exception)
            {
            }
            return null;
        }
        public static string GetMSBuildToolsVersionFromContents(string projectFileContents)
        {
            try
            {
                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.LoadXml(projectFileContents);
                XmlNode toolsVersionNode = xmlDoc.SelectSingleNode("//@ToolsVersion");

                if ((toolsVersionNode != null) && !String.IsNullOrEmpty(toolsVersionNode.Value))
                {
                    return toolsVersionNode.Value;
                }
            }
            catch (Exception)
            {
            }
            return null;
        }


Coordinator
Jan 13, 2011 at 7:24 PM

This has been reported once before but I wasn't able to duplicate the issue and couldn't get an example that did.  If anyone has one they can send me, I'll look into it.  My e-mail address is in the About box in the GUI and in the footer of the pages in the help file.

Eric

 

Jan 13, 2011 at 9:27 PM

Before investigating further, I think I'm probably missing something basic. What's the best way to set the ToolsVersion to ultimately affect this line of code:

msBuildExePath = Path.Combine(Engine.GlobalEngine.Toolsets[
                    project.MSBuildProject.ToolsVersion].ToolsPath, "MSBuild.exe");

? Are you able to easily set that to be 4.0?

 

Coordinator
Jan 14, 2011 at 7:12 PM

I don't think you can just set it to 4.0.  The current SHFB release isn't built against MSBuild 4.0 and I can't say what that might affect.  Not only that, it may stop working for anyone that doesn't have .NET 4.0 installed.  I'd rather see an example of the problem to diagnose it than guess at a possible fix.

Eric

 

Jan 14, 2011 at 11:13 PM
Edited Jan 14, 2011 at 11:23 PM

Hi All,

I tried to track down the problem and I arrived to the following: the template used to build the mrefbuilder project (which is instantiated by SHFB) is not right.

For example, while analysing the produced project file (GenerateRefInfo.proj) I saw some lines like these:

<SandcastleBuilder.Utils.MSBuild.MRefBuilder DocumentInternals="$(DocInternals)" SandcastlePath="$(SandcastlePath)" WorkingFolder="$(WorkingFolder)" Assemblies="@(Assembly)" References="@(ReferencePath)" />

That line is not correct because, in SHFB, the element containing references to external assemblies is an item group like the following:

<ItemGroup>

  <Reference Include="B">

    <HintPath> d:\external\B.DLL</HintPath>

  </Reference>

  <Reference Include="C">

    <HintPath> d:\external\C.DLL</HintPath>

  </Reference>

</ItemGroup>

Because of that, the line should be something like this:

<SandcastleBuilder.Utils.MSBuild.MRefBuilder DocumentInternals="$(DocInternals)" SandcastlePath="$(SandcastlePath)" WorkingFolder="$(WorkingFolder)" Assemblies="@(Assembly)" References="%(Reference.HintPath)" />

The additional problem I found is that %(Reference.HintPath) placeholder is not expanded into the list: "B.DLL;C.DLL" which is a suitable parameter for the '/DEP:" switch sent to MRefBuilder.exe tool.

Is there anybody who knows the right syntax to use in order to obtain a correct expanding the @(Reference) item group?

 

Thanks in advance

Stefano

 

 

Coordinator
Jan 15, 2011 at 2:21 AM

Stefano,

I've said this several times now:  If somebody can send me an example that fails as you describe, I will see about fixing it.  The problem right now is that I cannot duplicate the issue with any project that I've created.  I'm just not seeing the behavior that you are.  If I can get an example that does fail, I can diagnose it and come up with a fix that works for every case.

Eric

 

Mar 30, 2011 at 2:53 PM

For what it's worth, I have a project that works fine when I compile it from the GUI on Windows Server 2008-64. When I tried to run it from the command line (using the 3.5 version of MSBuild) I got these errors:

 

D:\home\svn\vx\help>C:\Windows\Microsoft.NET\Framework\v3.5\msbuild /p:OutputPath=D:\home\pub\trunk\chm;HelpFileFormat=HtmlHelp1 vx_all.shfbproj
Microsoft (R) Build Engine Version 3.5.30729.1
[Microsoft .NET Framework, Version 2.0.50727.3615]
Copyright (C) Microsoft Corporation 2007. All rights reserved.

Build started 3/30/2011 10:18:36 AM.
Project "D:\home\svn\vx\help\vx_all.shfbproj" on node 0 (default targets).
D:\Program Files (x86)\EWSoftware\Sandcastle Help File Builder\SandcastleHelpFileBuilder.targets(23,5): error MSB4062:
The "SandcastleBuilder.Utils.MSBuild.BuildHelp" task could not be loaded from the assembly D:\Program Files (x86)\EWSof
tware\Sandcastle Help File Builder\\SandcastleBuilder.Utils.dll. Could not load file or assembly 'file:///D:\Program Fi
les (x86)\EWSoftware\Sandcastle Help File Builder\SandcastleBuilder.Utils.dll' or one of its dependencies. This assembl
y is built by a runtime newer than the currently loaded runtime and cannot be loaded. Confirm that the  decl
aration is correct, and that the assembly and all its dependencies are available.
Done Building Project "D:\home\svn\vx\help\vx_all.shfbproj" (default targets) -- FAILED.


Build FAILED.

"D:\home\svn\vx\help\vx_all.shfbproj" (default target) (1) ->
(CoreBuildHelp target) ->
  D:\Program Files (x86)\EWSoftware\Sandcastle Help File Builder\SandcastleHelpFileBuilder.targets(23,5): error MSB4062
: The "SandcastleBuilder.Utils.MSBuild.BuildHelp" task could not be loaded from the assembly D:\Program Files (x86)\EWS
oftware\Sandcastle Help File Builder\\SandcastleBuilder.Utils.dll. Could not load file or assembly 'file:///D:\Program
Files (x86)\EWSoftware\Sandcastle Help File Builder\SandcastleBuilder.Utils.dll' or one of its dependencies. This assem
bly is built by a runtime newer than the currently loaded runtime and cannot be loaded. Confirm that the  de
claration is correct, and that the assembly and all its dependencies are available.

    0 Warning(s)
    1 Error(s)

Time Elapsed 00:00:00.31

I tried the 64-bit version of 3.5 MSBuild and got this error:

 

D:\home\svn\vx\help>C:\Windows\Microsoft.NET\Framework64\v3.5\msbuild /p:OutputPath=D:\home\pub\trunk\chm;HelpFileFormat=HtmlHelp1 vx_all.shfbproj
Microsoft (R) Build Engine Version 3.5.30729.1
[Microsoft .NET Framework, Version 2.0.50727.3615]
Copyright (C) Microsoft Corporation 2007. All rights reserved.

Build started 3/30/2011 10:20:17 AM.
Project "D:\home\svn\vx\help\vx_all.shfbproj" on node 0 (default targets).
D:\Program Files (x86)\EWSoftware\Sandcastle Help File Builder\SandcastleHelpFileBuilder.targets(23,5): error MSB4062:
The "SandcastleBuilder.Utils.MSBuild.BuildHelp" task could not be loaded from the assembly D:\Program Files (x86)\EWSof
tware\Sandcastle Help File Builder\\SandcastleBuilder.Utils.dll. Could not load file or assembly 'file:///D:\Program Fi
les (x86)\EWSoftware\Sandcastle Help File Builder\SandcastleBuilder.Utils.dll' or one of its dependencies. This assembl
y is built by a runtime newer than the currently loaded runtime and cannot be loaded. Confirm that the  decl
aration is correct, and that the assembly and all its dependencies are available.
Done Building Project "D:\home\svn\vx\help\vx_all.shfbproj" (default targets) -- FAILED.


Build FAILED.

"D:\home\svn\vx\help\vx_all.shfbproj" (default target) (1) ->
(CoreBuildHelp target) ->
  D:\Program Files (x86)\EWSoftware\Sandcastle Help File Builder\SandcastleHelpFileBuilder.targets(23,5): error MSB4062
: The "SandcastleBuilder.Utils.MSBuild.BuildHelp" task could not be loaded from the assembly D:\Program Files (x86)\EWS
oftware\Sandcastle Help File Builder\\SandcastleBuilder.Utils.dll. Could not load file or assembly 'file:///D:\Program
Files (x86)\EWSoftware\Sandcastle Help File Builder\SandcastleBuilder.Utils.dll' or one of its dependencies. This assem
bly is built by a runtime newer than the currently loaded runtime and cannot be loaded. Confirm that the  de
claration is correct, and that the assembly and all its dependencies are available.

    0 Warning(s)
    1 Error(s)

Time Elapsed 00:00:00.39

 

I switched to the 64-bit version of 4.0 MSBuild and got this error:

 

D:\home\svn\vx\help>C:\Windows\Microsoft.NET\Framework64\v4.0.30319\msbuild /p:OutputPath=D:\home\pub\trunk\chm;HelpFileFormat=HtmlHelp1 vx_all.shfbproj
Microsoft (R) Build Engine Version 4.0.30319.1
[Microsoft .NET Framework, Version 4.0.30319.1]
Copyright (C) Microsoft Corporation 2007. All rights reserved.

Build started 3/30/2011 10:21:23 AM.
Project "D:\home\svn\vx\help\vx_all.shfbproj" on node 1 (default targets).
CoreBuildHelp:
  Building D:\home\svn\vx\help\vx_all.shfbproj
  Initializing
  FindingTools
  ValidatingDocumentationSources
  GenerateSharedContent
  GenerateApiFilter
  GenerateReflectionInfo
SHFB : error BE0043: Unexpected error detected in last build step.  See output above for details. [D:\home\svn\vx\help\
vx_all.shfbproj]
  Failed
  Build details can be found in D:\home\svn\vx\help\Help\LastBuild.log
Done Building Project "D:\home\svn\vx\help\vx_all.shfbproj" (default targets) -- FAILED.


Build FAILED.

"D:\home\svn\vx\help\vx_all.shfbproj" (default target) (1) ->
(CoreBuildHelp target) ->
  SHFB : error BE0043: Unexpected error detected in last build step.  See output above for details. [D:\home\svn\vx\hel
p\vx_all.shfbproj]

    0 Warning(s)
    1 Error(s)

Time Elapsed 00:00:06.51

 

The log file revealed this error:


Generating reflection information...
[C:\Windows\Microsoft.NET\Framework64\v3.5\MSBuild.exe]
Project file contains ToolsVersion="4.0", which is not supported by this version of MSBuild. Treating the project as if it had ToolsVersion="3.5".
D:\home\svn\vx\help\Working\GenerateRefInfo.proj(87,5): error MSB4062: The "SandcastleBuilder.Utils.MSBuild.MRefBuilder" task 
could not be loaded from the assembly D:\Program Files (x86)\EWSoftware\Sandcastle Help File Builder\\SandcastleBuilder.Utils.dll.
Could not load file or assembly 'file:///D:\Program Files (x86)\EWSoftware\Sandcastle Help File Builder\SandcastleBuilder.Utils.dll'
or one of its dependencies. This assembly is built by a runtime newer than the currently loaded runtime and cannot be loaded.
Confirm that the <UsingTask> declaration is correct, and that the assembly and all its dependencies are available. Last step completed in 00:00:01.0625 SHFB: Error BE0043: Unexpected error detected in last build step. See output above for details.

And then, I edited the project file to specify 4.0 for the ToolsVersion attribute:

<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="4.0">

I then ran it again (using the 4.0 version of MSBuild) and it worked fine:

D:\home\svn\vx\help>C:\Windows\Microsoft.NET\Framework64\v4.0.30319\msbuild /p:OutputPath=D:\home\pub\trunk\chm;HelpFile
Format=HtmlHelp1 vx_all.shfbproj
Microsoft (R) Build Engine Version 4.0.30319.1
[Microsoft .NET Framework, Version 4.0.30319.1]
Copyright (C) Microsoft Corporation 2007. All rights reserved.

Build started 3/30/2011 10:25:40 AM.
Project "D:\home\svn\vx\help\vx_all.shfbproj" on node 1 (default targets).
CoreBuildHelp:
  Building D:\home\svn\vx\help\vx_all.shfbproj
  Initializing
  ClearWorkFolder
  FindingTools
  ValidatingDocumentationSources
  GenerateSharedContent
  GenerateApiFilter
  GenerateReflectionInfo
  GenerateNamespaceSummaries
  ApplyVisibilityProperties
  TransformReflectionInfo
  ModifyHelpTopicFilenames
  CopyStandardContent
  CopyConceptualContent
  CreateConceptualTopicConfigs
  CopyAdditionalContent
  MergeTablesOfContents
  GenerateIntermediateTableOfContents
  CreateBuildAssemblerConfigs
  MergeCustomConfigs
  BuildConceptualTopics
  BuildReferenceTopics
  CombiningIntermediateTocFiles
  ExtractingHtmlInfo
  GenerateHelpFormatTableOfContents
  GenerateHelpFileIndex
  GenerateHelpProject
  CompilingHelpFile
  CleanIntermediates
  Completed
  The help file is located at: D:\home\svn\vx\help\Help\Documentation.chm
  Build details can be found in D:\home\svn\vx\help\Help\LastBuild.log
Done Building Project "D:\home\svn\vx\help\vx_all.shfbproj" (default targets).


Build succeeded.
    0 Warning(s)
    0 Error(s)

Time Elapsed 00:20:50.42

D:\home\svn\vx\help>

I would be interested in understanding what caused these various errors. As I said, it builds fine from the GUI. And it builds fine from the command line on Windows 7 32-bit (the only other platform I'm using). I can send you the project file if you like, but it's pretty plain vanilla stuff.

Thanks!

Ed

Coordinator
Mar 30, 2011 at 7:56 PM

Ed,

I don't think there's an issue here anymore.  The SHFB build tasks will not work with MSBuild 3.5 as SHFB is now built using .NET 4.0 and uses the MSBuild 4.0 assemblies.  This is evident in the error messages above where it states the assembly is built using a newer version of the runtime.  In addition, if you have an older project, you will need to either open it in the GUI and save it so that it updates the ToolsVersion to 4.0 or edit it manually as you did to set it to 4.0.  The project is being forced to the ToolsVersion to 4.0 when opened in the GUI which is why it works there but it won't persist the change unless you save the project (note the '*' on the title bar indicating the project has been modified when you first open it).  When built from the command line, as long as the version of MSBuild you start and the ToolsVersion agree with the version of MSBuild that SHFB is using, it won't fail with the mismatched version error.

Eric

 

Jan 24, 2012 at 6:55 PM

Earlier today I got the same error as top title, and I think I know why this bug is so difficult to diagnose, as I have found a workaround and possible lead on why this bug occurs.

First off, I installed Sandcastle and SHFB only last week, so the 3.5 vs 4.0 issue should not apply. So here's a detailed step by step of what I did before the error occurred:

  1. Create a successful Sandcastle build project. (.shfbproj), using one or more references and one of more documentation sources (normal setup will do)
  2. Copy the .shfbproj file and paste it in another folder location (preferably one that is not a parent / child of the original folder)
  3. Load the new .shfbproj file you copied in step 2
  4. Attempt to do a build

That is how I got the error. The workaround that works for me is simply delete the reference and re-add it. I noticed this had an impact even before I did the build because the HintPath property of the Reference item changed as a result of the workaround. Now all my Sandcastle projects build properly.

Hope this helps!