dotnet / sdk

Core functionality needed to create .NET Core projects, that is shared between Visual Studio and CLI
https://dot.net/core
MIT License
2.7k stars 1.06k forks source link

Allow .NET Core apps to optionally be run using the Mono runtime #7932

Open dsplaisted opened 7 years ago

dsplaisted commented 7 years ago

The Mono runtime should support running .NET Core apps. @migueldeicaza would like a switch in dotnet run to run apps targeting .NET Core using the Mono runtime.

Related: dotnet/sdk#7931

benaadams commented 7 years ago

Makes sense and good for testing 👍

tactical-drone commented 7 years ago

We need this.

drake7707 commented 7 years ago

Would be great because dotnet doesn't run on armv6 while mono does.

dasMulli commented 7 years ago

@drake7707 this issue is about extending the dotnet based tooling to use the mono runtime, which can't be used on systems that the dotnet cli doesn't run on.. You can still call mono myapp.dll on these systems, which should work as long as the app only uses API surface that is available in both netcoreapp and mono.

Additional note: The CLI uses whatever the project evaluates for $(RunCommand), $(RunArguments) and $(RunWorkingDirectory) so a change could be made to Microsoft.NET.Sdk.targets to allow for an option to use the mono runtime. This could even be used for netfx projects on windows using the windows build of mono.

tmds commented 7 years ago

Should mono have a different TFM than .NET Core? e.g. dotnet run -f mono50

dasMulli commented 7 years ago

There's a NuGet issue for creating a mono TFM: https://github.com/NuGet/Home/issues/2320

I believe the overall scenario is blocked on https://github.com/dotnet/sdk/issues/335 but if this is ever implemented (targeting pack NuGets, mono ref assembly resolving, …) then this will be a needed feature or handled by the SDK targets transparently.

In the meantime msbuild /t:Build,Run /p:TargetFramework=net461 works on mono for net* apps using the SDK. Not sure if running netcoreapp* apps is supported or works for many apps just by coincidence (because all the necessary type forwards are in place).

TheAngryByrd commented 7 years ago

A while back I made a tool dotnet-mono for running mono apps. https://github.com/TheAngryByrd/dotnet-mono

tmds commented 6 years ago

@marek-safar I see you are working on this. I recently tried to make mono use a netcoreapp2.0 assembly and it failed like this:

The "ResolvePackageAssets" task could not be instantiated from "/home/tmds/repos/source-build/src/cli/bin/2/linux-x64/dotnet/sdk/2.1.300-preview2-006908/Sdks/Microsoft.NET.Sdk/targets/../tools/net46/Microsoft.NET.Build.Tasks.dll". Could not load type of field 'Microsoft.NET.Build.Tasks.ResolvePackageAssets:TextEncoding' (26) due to: Could not resolve type with token 01000066 (from typeref, class/assembly System.Text.Encoding, System.Runtime, Version=4.2.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a) assembly:System.Runtime, Version=4.2.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a type:System.Text.Encoding member:

It seems this is due to the .NET Core 2.0 version of System.Text.Encoding (4.2.0.0) not being supported by mono.

benaadams commented 6 years ago

@tmds I'm successfully building for Mono with the following csproj and using System.Text.Encoding

The salient lines being

Target correct framework

<TargetFramework>net471</TargetFramework>
<RuntimeIdentifiers>linux-x64</RuntimeIdentifiers>

Targeting pack not being on nuget

<RestoreAdditionalProjectSources>https://dotnet.myget.org/F/dotnet-core/api/v3/index.json</RestoreAdditionalProjectSources>

Add targeting pack

<PackageReference Include="Microsoft.TargetingPack.NETFramework.v4.7.1" Version="1.0.0" ExcludeAssets="All" PrivateAssets="All" />
<Reference Include="netstandard" />
<PackageReference Include="NETStandard.Library" Version="2.0.0" />

Override framework with targeting pack

<FrameworkPathOverride>$(NuGetPackageRoot)microsoft.targetingpack.netframework.v4.7.1\1.0.0\lib\net471\</FrameworkPathOverride>

Then build and run with the following dockerfile

FROM microsoft/dotnet:2.1-sdk-stretch AS build
WORKDIR /app
COPY PlatformBenchmarks .
RUN dotnet publish -c Release -o out
COPY Benchmarks/appsettings.json ./out/appsettings.json

FROM mono:5.12.0.226 AS runtime
ENV ASPNETCORE_URLS http://+:8080
ENV KestrelTransport Libuv
WORKDIR /app
COPY --from=build /app/out ./

ENTRYPOINT ["mono", "--server", "--gc=sgen", "--gc-params=mode=throughput", "PlatformBenchmarks.exe"]
tmds commented 6 years ago

Yes, net 471 should work. netcoreapp2.0+ probably won't, and the title of the issue is 'running .net core apps on mono'. Maybe we need to update the title then?

benaadams commented 6 years ago

Yes, net 471 should work.

Well you can't run them on Mono with the cli currently

netcoreapp2.0+ probably won't

Possibly... Mono probably has more flexibility to adopt the additional .NET Core api set and achieve parity with netcoreappN much faster than desktop does

dasMulli commented 6 years ago

I think for netcoreapp2.0 this (mostly?) works because it brought more APIs that .net framework and mono already had and I guess mono implemented some APIs that .NET Core has. The code sharing initiative where mono uses some sources from corefx directly probably also means that mono may adapt new APIs automatically.

However some 2.1+ APIs still aren't in mono, e.g. BrotliStream since mono uses parts of the MIT reference source for System.IO.Compression and not (yet?) corefx source code.

Serentty commented 4 years ago

This is sort of related, but is there a chance that the .NET Core dotnet command will ever be able to load EXEs containing IL the way that Mono can? It would be useful for distributing cross-platform binaries, especially considering that a file with an EXE extension is more intuitive for a user as an executable with an entry point than a DLL.