使用Visual Studio 2017将.NET Core应用程序编译为EXE文件 [英] Compile a .NET Core application as an EXE file using Visual Studio 2017
问题描述
我在Visual Studio 2017中创建了一个.NET Core应用程序(v1.1).当我对其进行编译时,我得到了一个DLL文件,而不是所生成项目的预期EXE文件.我确实检查了csproj
文件,并确认输出类型设置为exe
,但没有骰子.
为什么Visual Studio 2017仍在生成DLL文件?
我确定这是我忘了的快速设置...
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp1.1</TargetFramework>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<PlatformTarget>AnyCPU</PlatformTarget>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\Core.EF.SqlServer\Core.EF.SqlServer.csproj" />
</ItemGroup>
</Project>
更新2019:
.NET Core 3.0+项目现在将包含默认情况下所构建平台的可执行文件.这只是一个填充程序可执行文件,您的主要逻辑仍然在.dll
文件中.
但是.NET Core 3.0还引入了单文件部署,因此可以使用
进行部署dotnet publish -r win-x64 -p:PublishSingleFile=True --self-contained false
将创建一个包含所有依赖项的.exe文件.您可以将--self-contained
更改为true
来也包含.NET Core运行时,因此不需要在目标计算机上全局安装.NET Core.
原始
.NET Core应用程序应该是.dll
文件.在这种情况下,将OutputType
设置为Exe
表示可执行".并执行确保输出可运行的所有必要操作(Main()
方法的入口点,.runtimeconfig.json
文件).生成的DLL文件应使用以下命令运行:
dotnet yourapp.dll
此DLL文件可在.NET Core运行时支持的所有平台(Windows,Linux和macOS)上运行.这被称为便携式的".或框架相关"部署.
如果您确实需要.exe
文件,请考虑独立的部署.这样将创建一个包含自己的.NET Core运行时副本和yourapp.exe
文件的输出-但它还会增加已发布应用程序的大小,并且在发布新版本的运行时时需要对其进行更新.>
此外,生成的应用程序仅适用于为其发布的操作系统.
请参考 解决方案
Update 2019:
.NET Core 3.0+ projects will now include an executable for the platform you build on by default. This is just a shim executable and your main logic is still inside a .dll
file.
But .NET Core 3.0 also introduced single-file deployments so deploying with
dotnet publish -r win-x64 -p:PublishSingleFile=True --self-contained false
will create a single .exe file containing all your dependencies. You can change --self-contained
to true
to also include the .NET Core Runtime as well so .NET Core does not need to be installed globally on the target machine.
Original
.NET Core applications are supposed to be .dll
files. OutputType
set to Exe
in this case means "executable" and does everything necessary to ensure that the output is runnable (entry point from Main()
method, .runtimeconfig.json
file). The resulting DLL file is meant to be run using:
dotnet yourapp.dll
This DLL file works across all platforms that are supported by the .NET Core runtime (Windows, Linux, and macOS). This is called a "portable" or "framework dependent" deployment.
If you want really a .exe
file, consider self-contained deployments. This will create an output that contains its own copy of the .NET Core runtime and an yourapp.exe
file - but it also increases the size of the published application and it needs to be updated when new versions of the runtime are released.
Also, the resulting application only works on the operating system published for.
Refer to .NET Core application deployment for more details on the deployment options and how to set them up.
这篇关于使用Visual Studio 2017将.NET Core应用程序编译为EXE文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!