C # .NET 1.6 standard with excessive dependencies in posted application

I am testing Visual Studio 2017 and .NET Core in my first attempt at C # /. NET after a few years (returning from Golang). I tried to create a small world-style welcome web app that just listens and echoes in from tcp clients:

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);
                    }
                });
            }

        }
    }
}

      

My project / build config looks like this:

<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>

      

After publishing the app (publishing to dotnet), I get the folder with the executable file that I need. The problem I am facing is that the folder contains 205 files (mostly related to .NET DLL files) and is over 30MB in size. This includes DLL files with reference to .NET libraries that I don't even use, like System.Xml and Linq.

Is there a way to reduce the number of files and the size of the published application so that only what I need is included?

UPDATE: Tried to recreate the project from scratch using dotnet tool and not visual studio:

dotnet new console - C # language - Socket name - Outlet socket --framework netcoreapp1.0

This created this project (which for some reason compiles to dll despite the exe output type and target console console):

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>netcoreapp1.0</TargetFramework>
  </PropertyGroup>
</Project>

      

Then manually modify the csproj file to look like this:

<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>

      

Now publish with:

dotnet publish -runtime win7-x64 --configuration Release

And go to:

./bin/Release/netcoreapp1.0/win7-x64

Now all of a sudden a working exe file appears and only a few DLL files of about 1 MB in size. By deleting the subfolder called "publish" I now have something that works without most of the bells and whistles. Not sure why it worked, and if it is considered the expected behavior. Don't know what publishing is for or not for deploying on a machine without .NET installed. Microsoft has some work on its documentation.

+3


source to share


1 answer


Have a look at https://docs.microsoft.com/en-us/dotnet/articles/core/deploying/index

In the first case, your command publish

generates output for Framework-dependent deployment

which relies on the presence of .NET Core on the target machine. Your code (in the form of dll (s)) will be executed at runtime.

In the second case, when you specify the target runtime using a parameter --runtime

, it publish

generates output for Self-contained deployment

, which includes the version of the .NET Core you are using. There is a way to reduce the overall size of the output, as described in the above article mentioning section Deploying a self-contained deployment with a smaller footprint

.

As for why <OutputType>Exe</OutputType>

didn't generate the file exe

, check https://docs.microsoft.com/en-us/dotnet/articles/core/tools/dotnet-build



Basically it says that the generated

binaries include project code in intermediate language (IL) files with a .dll extension

and what's the difference between indicating and not indicating <OutputType>Exe</OutputType>

is that the DL DL DL for the library does not contain entry points and cannot be executed.

+2


source







All Articles