我正在试用 Visual Studio 2017 和 .NET Core,这是我几年来第一次尝试 C#/.NET(从 Golang 回来)。我试图创建一个小型的 hello world 风格的网络应用程序,它只监听和回显来自 tcp 客户端的输入:
using System;
using System.Threading.Tasks;
using System.Net;
using System.Net.Sockets;
using System.IO;
using System.Text;
namespace TaskSockets
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
TcpListener server = new TcpListener(IPAddress.Any, 5555);
server.Start();
while (true)
{
TcpClient newClient = server.AcceptTcpClientAsync().Result;
Task.Run(async () => {
StreamWriter sWriter = new StreamWriter(newClient.GetStream(), Encoding.ASCII);
StreamReader sReader = new StreamReader(newClient.GetStream(), Encoding.ASCII);
Boolean bClientConnected = true;
String sData = null;
while (bClientConnected)
{
sData = await sReader.ReadLineAsync();
Console.WriteLine("Client: " + sData);
}
});
}
}
}
}
我的项目/构建配置如下所示:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netstandard1.6</TargetFramework>
<RuntimeIdentifiers>win7-x64</RuntimeIdentifiers>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NETCore.Runtime.CoreCLR" Version="1.1.1" />
<PackageReference Include="Microsoft.NETCore.DotNetHostPolicy" Version="1.1.0" />
</ItemGroup>
</Project>
发布应用程序 (dotnet publish) 后,我得到一个包含我需要的可执行文件的文件夹。然而,我遇到的问题是该文件夹包含 205 个文件(主要是 .NET 相关的 DLL 文件)并且大小超过 30 MB。这包括引用我什至不使用的 .NET 库的 dll 文件,例如 System.Xml 和 Linq。
有什么方法可以减少已发布应用程序的文件数量和大小,以便包含我需要的内容吗?
更新: 尝试使用 dotnet 工具而不是 visual studio 从头开始重新创建项目:
dotnet new console --language C# --name Socket --output Socket --framework netcoreapp1.0
这创建了这个项目(尽管有 exe 输出类型和控制台应用程序目标,但出于某种原因编译为 dll):
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp1.0</TargetFramework>
</PropertyGroup>
</Project>
然后手动修改csproj文件,让它看起来像这样:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp1.0</TargetFramework>
<RuntimeIdentifiers>win7-x64</RuntimeIdentifiers>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NETCore.Runtime.CoreCLR" Version="1.0.6" />
</ItemGroup>
</Project>
现在发布:
dotnet publish --runtime win7-x64 --configuration Release
然后转到:
./bin/Release/netcoreapp1.0/win7-x64
现在突然有一个工作的 exe 文件和几个大约 1 MB 大小的 DLL 文件。通过删除名为“publish”的子文件夹,我现在可以使用一些没有大部分膨胀的东西。不知道为什么会这样,也不知道它是否被认为是预期的行为。不知道发布文件夹的用途,也不知道是否需要在未安装 .NET 的计算机上进行部署。 Microsoft 在其文档方面还有一些工作要做。
最佳答案
看看https://learn.microsoft.com/en-us/dotnet/articles/core/deploying/index
在第一种情况下,您的 publish
命令生成 Framework-dependent deployment
的输出这依赖于目标机器上 .NET Core 框架的存在。您的代码(以 dll(s) 的形式)将由运行时执行。
在第二种情况下,当您使用 --runtime
指定目标运行时时选项,publish
为 Self-contained deployment
生成输出其中使用的 .NET Core 框架版本包含。有一种方法可以减少输出的总大小,如上述 Deploying a self-contained deployment with a smaller footprint
中的文章所述。部分。
至于为什么<OutputType>Exe</OutputType>
没有生成 exe
-文件,查看https://learn.microsoft.com/en-us/dotnet/articles/core/tools/dotnet-build
基本上说的是生成
binaries include the project's code in Intermediate Language (IL) files with a .dll extension
以及指定和不指定之间的区别 <OutputType>Exe</OutputType>
is that the IL DLL for a library doesn't contain entry points and can't be executed.
关于C# .NET 标准 1.6 在已发布的应用程序中具有过多的依赖性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43142224/