microsoft / MSBuildLocator

An API to locate MSBuild assemblies from an installed Visual Studio location. Use this to ensure that calling the MSBuild API will use the same toolset that a build from Visual Studio or msbuild.exe would.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Is there any way to call or invoke MSBuildLocator in the net4x from a dotnet SDK project?

awesome-liuxiao opened this issue · comments

I've noticed that this article tells that it is impossible to directly call a .net framework msbuild in a .net sdk project; but I'm wondering that is it possible to invoke .net framework msbuild as an assembly and use its methods (such as RegisterMSBuildPath() or RegisterInstance()) to build a .net framework based project and collect the compilation result?

Hi @awesome-liuxiao,

MSBuildLocator running on .NET Core does not support invoking MSBuild assemblies targeting the .NET Framework. However, you can still build a project with a TargetFramework set to net472 or a similar version using the .NET Core SDK.

If you need further clarification or have specific requirements, please feel free to ask. Providing additional details will help us address your needs more accurately.

hi @YuliiaKovalova ,

Thank you for your reply,
I understand that it is difficult to invoke different msbuild assemblies between .net sdk and .net framework projects; therefore, I've changed my strategy to build two independent projects for .net sdk and .net framework, respectively. But now when I trying to load assembly called Microsoft.CodeAnalysis.CSharp.CodeStyle for the two projects, the .net framework project keeps failing on getting DefinedTypes with reflectiontypeloadexception. The version of the assembly is 4.9.2, which needs dependencies of .NETStandard 2.0, and my project is targetting .net framework 4.8, which should be supported according to https://learn.microsoft.com/en-us/dotnet/standard/net-standard?tabs=net-standard-2-0#net-platforms-support?WT.mc_id=DT-MVP-5002999
Am I doing incorrectly?
By the way, the other project based on .net sdk loads the mentioned assembly perfectly.

@awesome-liuxiao, can you share the full exception, including the callstack?

@YuliiaKovalova
sorry to bother you, turns out I didn't import the dll as a reference in my project; after importing the dll and modified the app.config, the dll can be successfully loaded.

thank you!