Roslyn 编译的项目生成空程序集
Roslyn-compiled project generates empty assembly
这是 的后续内容,我已经通过几十篇文章的组合获得了解决方案。
这很容易重现:
.csproj:
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net47</TargetFramework>
</PropertyGroup>
<ItemGroup>
<Folder Include="wwwroot\" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore" Version="2.0.0" />
<PackageReference Include="Microsoft.Build" Version="15.3.409" />
<PackageReference Include="Microsoft.Build.Tasks.Core" Version="15.3.409" />
<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="2.3.0-beta1" />
<PackageReference Include="Microsoft.CodeAnalysis.Common" Version="2.3.1" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="2.3.1" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="2.3.1" />
<PackageReference Include="Microsoft.CodeAnalysis.Workspaces.Common" Version="2.3.1" />
<PackageReference Include="Microsoft.CodeDom.Providers.DotNetCompilerPlatform" Version="1.0.7" />
<PackageReference Include="Microsoft.Net.Compilers" Version="2.3.1" />
</ItemGroup>
</Project>
然后:
using System.IO;
using System.Linq;
using Microsoft.CodeAnalysis.MSBuild;
namespace WebApplication1
{
public class Program
{
public static void Main(string[] args)
{
var workspace = MSBuildWorkspace.Create();
var project = workspace.OpenProjectAsyn(@"C:\WebApplication1\WebApplication1.csproj").Result;
var compilation = project.GetCompilationAsync().Result;
using (var memoryStream = new MemoryStream())
{
var result = compilation.Emit(memoryStream);
if (result.Success)
{
File.WriteAllBytes(Path.Combine(@"C:\WebApplication1\", $"{compilation.AssemblyName}.dll"), memoryStream.ToArray());
}
}
}
}
}
加载解决方案后查看 Worskpace,我在 Diagnostics 上收到一条错误消息 属性:
Msbuild failed when processing the file 'C:\WebApplication1\WebApplication1.csproj' with message: The SDK 'Microsoft.NET.Sdk.Web' specified could not be found.
根据@DustinCampbell here,这还不是一个受支持的场景。
我通过从 Process
.
调用 dotnet build
解决了这个问题
这是
这很容易重现:
.csproj:
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net47</TargetFramework>
</PropertyGroup>
<ItemGroup>
<Folder Include="wwwroot\" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore" Version="2.0.0" />
<PackageReference Include="Microsoft.Build" Version="15.3.409" />
<PackageReference Include="Microsoft.Build.Tasks.Core" Version="15.3.409" />
<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="2.3.0-beta1" />
<PackageReference Include="Microsoft.CodeAnalysis.Common" Version="2.3.1" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="2.3.1" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="2.3.1" />
<PackageReference Include="Microsoft.CodeAnalysis.Workspaces.Common" Version="2.3.1" />
<PackageReference Include="Microsoft.CodeDom.Providers.DotNetCompilerPlatform" Version="1.0.7" />
<PackageReference Include="Microsoft.Net.Compilers" Version="2.3.1" />
</ItemGroup>
</Project>
然后:
using System.IO;
using System.Linq;
using Microsoft.CodeAnalysis.MSBuild;
namespace WebApplication1
{
public class Program
{
public static void Main(string[] args)
{
var workspace = MSBuildWorkspace.Create();
var project = workspace.OpenProjectAsyn(@"C:\WebApplication1\WebApplication1.csproj").Result;
var compilation = project.GetCompilationAsync().Result;
using (var memoryStream = new MemoryStream())
{
var result = compilation.Emit(memoryStream);
if (result.Success)
{
File.WriteAllBytes(Path.Combine(@"C:\WebApplication1\", $"{compilation.AssemblyName}.dll"), memoryStream.ToArray());
}
}
}
}
}
加载解决方案后查看 Worskpace,我在 Diagnostics 上收到一条错误消息 属性:
Msbuild failed when processing the file 'C:\WebApplication1\WebApplication1.csproj' with message: The SDK 'Microsoft.NET.Sdk.Web' specified could not be found.
根据@DustinCampbell here,这还不是一个受支持的场景。
我通过从 Process
.
dotnet build
解决了这个问题