2

I have a solution which builds in vs. When I load it with roslyn like so:

var workspace = MSBuildWorkspace.Create();
var solutionPath = @"c:\path\to\my.sln";
var solution = workspace.OpenSolutionAsync(solutionPath).Result;

then all of the projects have 0 metadata references.

If I try and get a type from a referenced assembly like this:

var compilation = solution.Projects.First().GetCompilationAsync().Result;
var myType = compilation.GetTypeByMetadataName("SomeTypeNanme);

it never finds the type. If I manually add metadata references like this:

var project = project.AddMetadataReference(AssemblyContaningTheType);

Then it can find the type.

My solution targets .NET Framework 4.5.2, in case that matters

Is this the only way to resolve the references, ie adding the metadata references manually? Can it not be done through the fact that the references are all in the csproj? How can I know what all the references that I need to add?

Ideally I'd like to have references resolved automatically, but would be ok with pointers on building all neccessary metadata references from the info in the project files...

Sam Holder
  • 32,535
  • 13
  • 101
  • 181
  • Does this problem occur when you run your code without the debugger attached? Also see this answer about some binding redirects you might need for `MSBuildWorkspace`: https://stackoverflow.com/a/55111549/300908 – JoshVarty Mar 18 '19 at 01:46

2 Answers2

1

As far as I can tell, it could be related to this issue, or maybe this one which leads to this open ticket.

Quoting the latter

Empty MetadataReferences for project referencing other project from same solution

Version Used: 2.7.0

Steps to Reproduce:

  1. Create solution with two projects, A and B

  2. Reference project B to project A

  3. Open project A using this code:

    var workspace = MSBuildWorkspace.Create(additionalProperties);

    Project project = workspace.OpenProjectAsync(projectPath).Result;

Expected Behavior:

project.MetadataReferences should contain project's references, such as mscorlib.dll

Actual Behavior:

project.MetadataReferences is empty.

Community
  • 1
  • 1
LoneWanderer
  • 3,058
  • 1
  • 23
  • 41
  • Can’t edit text for better formatting right now, editor keeps telling me some code is badly formatted :-/ – LoneWanderer Mar 13 '19 at 07:05
  • I'll try that, but I'm using the specific msbuild workspaces package alluded to in the posts, which claims to have fixed this issue. I'll update the question to make that clear shortly – Sam Holder Mar 13 '19 at 07:13
  • A link in the open ticket led me to a solution, which I've posted as an answer. Thanks. – Sam Holder Nov 04 '19 at 18:18
  • Glad you could make it ! I almost never used MS VS and surely never in this way, and I only gatherer some references I thought of use considering your problem ^^ – LoneWanderer Nov 05 '19 at 00:13
0

So I finally got around to looking at this again. The solution for me was from this gist:

https://gist.github.com/DustinCampbell/32cd69d04ea1c08a16ae5c4cd21dd3a3

Which basically forces a consistent version of MSBuild.

First add a reference to Microsoft.Build.Locator (it says it works for MSBuild 15, but it worked with 16 for me).

then add this line:

MSBuildLocator.RegisterDefaults();

somewhere in the bootstrapping, before you open the solution with an MSBuildWorkspace.

Sam Holder
  • 32,535
  • 13
  • 101
  • 181