在Linux上安装.NET MAUI。

huangapple go评论71阅读模式
英文:

Installing .NET MAUI on Linux

问题

dotnet workload install android
未处理的异常: System.IO.IOException: 只读文件系统: '/snap/dotnet-sdk/216/metadata'
   在 System.IO.FileSystem.CreateDirectory(String fullPath, UnixFileMode unixCreateMode)
   在 System.IO.Directory.CreateDirectory(String path)
   在 Microsoft.DotNet.Workloads.Workload.Install.WorkloadInstallerFactory.CanWriteToDotnetRoot(String dotnetDir)
   在 Microsoft.DotNet.Workloads.Workload.Install.WorkloadInstallerFactory.GetWorkloadInstaller(IReporter reporter, SdkFeatureBand sdkFeatureBand, IWorkloadResolver workloadResolver, VerbosityOptions verbosity, String userProfileDir, Boolean verifySignatures, INuGetPackageDownloader nugetPackageDownloader, String dotnetDir, String tempDirPath, PackageSourceLocation packageSourceLocation, RestoreActionConfig restoreActionConfig, Boolean elevationRequired)
   在 Microsoft.DotNet.Workloads.Workload.Install.WorkloadInstallCommand..ctor(ParseResult parseResult, IReporter reporter, IWorkloadResolver workloadResolver, IInstaller workloadInstaller, INuGetPackageDownloader nugetPackageDownloader, IWorkloadManifestUpdater workloadManifestUpdater, String dotnetDir, String userProfileDir, String tempDirPath, String version, IReadOnlyCollection`1 workloadIds, String installedFeatureBand)
   在 Microsoft.DotNet.Cli.WorkloadInstallCommandParser.<>c.<ConstructCommand>b__6_0(ParseResult parseResult)
   在 System.CommandLine.Invocation.InvocationPipeline.<>c__DisplayClass4_0.<<BuildInvocationChain>b__0>d.MoveNext()
--- 前一位置的堆栈跟踪的结尾 ---
   在 Microsoft.DotNet.Cli.Parser.<>c__DisplayClass17_0.<<UseParseErrorReporting>b__0>d.MoveNext()
--- 前一位置的堆栈跟踪的结尾 ---
   在 System.CommandLine.CommandLineBuilderExtensions.<>c__DisplayClass11_0.<<UseHelp>b__0>d.MoveNext()
--- 前一位置的堆栈跟踪的结尾 ---
   在 System.CommandLine.CommandLineBuilderExtensions.<>c.<<UseSuggestDirective>b__17_0>d.MoveNext()
--- 前一位置的堆栈跟踪的结尾 ---
   在 System.CommandLine.CommandLineBuilderExtensions.<>c__DisplayClass15_0.<<UseParseDirective>b__0>d.MoveNext()
--- 前一位置的堆栈跟踪的结尾 ---
   在 System.CommandLine.CommandLineBuilderExtensions.<>c__DisplayClass7_0.<<UseExceptionHandler>b__0>d.MoveNext()

我运行了以下命令来安装.NET SDK:"sudo snap install dotnet-sdk --classic",这安装了.NET SDK版本7。然后我尝试运行上述命令".Net workload install android",并发现上述错误。我正在使用Rider IDE。对此任何帮助将不胜感激。谢谢。

英文:
dotnet workload install android
Unhandled exception: System.IO.IOException: Read-only file system : '/snap/dotnet-sdk/216/metadata'
   at System.IO.FileSystem.CreateDirectory(String fullPath, UnixFileMode unixCreateMode)
   at System.IO.Directory.CreateDirectory(String path)
   at Microsoft.DotNet.Workloads.Workload.Install.WorkloadInstallerFactory.CanWriteToDotnetRoot(String dotnetDir)
   at Microsoft.DotNet.Workloads.Workload.Install.WorkloadInstallerFactory.GetWorkloadInstaller(IReporter reporter, SdkFeatureBand sdkFeatureBand, IWorkloadResolver workloadResolver, VerbosityOptions verbosity, String userProfileDir, Boolean verifySignatures, INuGetPackageDownloader nugetPackageDownloader, String dotnetDir, String tempDirPath, PackageSourceLocation packageSourceLocation, RestoreActionConfig restoreActionConfig, Boolean elevationRequired)
   at Microsoft.DotNet.Workloads.Workload.Install.WorkloadInstallCommand..ctor(ParseResult parseResult, IReporter reporter, IWorkloadResolver workloadResolver, IInstaller workloadInstaller, INuGetPackageDownloader nugetPackageDownloader, IWorkloadManifestUpdater workloadManifestUpdater, String dotnetDir, String userProfileDir, String tempDirPath, String version, IReadOnlyCollection`1 workloadIds, String installedFeatureBand)
   at Microsoft.DotNet.Cli.WorkloadInstallCommandParser.<>c.<ConstructCommand>b__6_0(ParseResult parseResult)
   at System.CommandLine.Invocation.InvocationPipeline.<>c__DisplayClass4_0.<<BuildInvocationChain>b__0>d.MoveNext()
--- End of stack trace from previous location ---
   at Microsoft.DotNet.Cli.Parser.<>c__DisplayClass17_0.<<UseParseErrorReporting>b__0>d.MoveNext()
--- End of stack trace from previous location ---
   at System.CommandLine.CommandLineBuilderExtensions.<>c__DisplayClass11_0.<<UseHelp>b__0>d.MoveNext()
--- End of stack trace from previous location ---
   at System.CommandLine.CommandLineBuilderExtensions.<>c.<<UseSuggestDirective>b__17_0>d.MoveNext()
--- End of stack trace from previous location ---
   at System.CommandLine.CommandLineBuilderExtensions.<>c__DisplayClass15_0.<<UseParseDirective>b__0>d.MoveNext()
--- End of stack trace from previous location ---
   at System.CommandLine.CommandLineBuilderExtensions.<>c__DisplayClass7_0.<<UseExceptionHandler>b__0>d.MoveNext()

I ran the following command to install the dot net sdk : "sudo snap install dotnet-sdk --classic" , which installed dot net sdk version 7 .I then tried running the above command : ".Net workload install android"and found the above error as a result .I am using the Rider IDE.Any help on the same will be much appreciated.Thanks.

答案1

得分: 2

因为Snap软件包管理器创建了安装内容的只读镜像,所以它无法被篡改。还可以参考这里:https://askubuntu.com/a/919098

对我来说效果更好的方法是按照这里描述的安装脚本安装.NET:https://learn.microsoft.com/dotnet/core/install/linux-scripted-manual#scripted-install

  1. 下载脚本
wget https://dot.net/v1/dotnet-install.sh -O dotnet-install.sh
  1. 使其可执行
chmod +x ./dotnet-install.sh
  1. 执行
./dotnet-install.sh --version latest

这将安装最新版本,可能不是LTS版本。如果你想要当前的LTS版本,请不要包含任何参数。

不要忘记将DOTNET_ROOTPATH环境变量添加到你的路径中。参见这里

假设你正在使用Bash,请打开~/.bashrc并添加以下内容:

export DOTNET_ROOT=$HOME/.dotnet
export PATH=$PATH:$DOTNET_ROOT:$DOTNET_ROOT/tools

你可能需要执行touch ~/.bashrc或打开一个新的终端窗口。然后你可以运行dotnet --info来查看.NET信息。

要安装.NET MAUI工作负载,请运行:dotnet workload install maui-android

在撰写本文时,默认的.NET MAUI项目模板在Linux上无法构建。这是因为它尝试构建iOS和macOS目标。按照以下方式进行修复:

  1. 打开你的csproj文件,然后搜索<TargetFrameworks>标签。它将包含3个条目,类似于net7.0-android;net7.0-ios;net7.0-maccatalyst。还有一个带有Condition属性的条目用于Windows,你可以保留不变。

  2. <TargetFrameworks>标签更改为如下所示:

<TargetFrameworks>net7.0-android</TargetFrameworks>
<TargetFrameworks Condition="!$([MSBuild]::IsOSPlatform('linux'))">$(TargetFrameworks);net7.0-ios;net7.0-maccatalyst</TargetFrameworks>
<TargetFrameworks Condition="$([MSBuild]::IsOSPlatform('windows'))">$(TargetFrameworks);net7.0-windows10.0.19041.0</TargetFrameworks>

请注意,其中的net7.0也可以根据你运行的版本而变为net6.0或net8.0。

英文:

That is because the Snap package manager creates read-only images of the things you install so it can't be tampered with. Also see here: https://askubuntu.com/a/919098

What worked better for me is installing .NET through the install script as described here: https://learn.microsoft.com/dotnet/core/install/linux-scripted-manual#scripted-install

  1. Download script

wget https://dot.net/v1/dotnet-install.sh -O dotnet-install.sh

  1. Make it executable

chmod +x ./dotnet-install.sh

  1. Execute

./dotnet-install.sh --version latest

This will install the latest version which might not be the LTS version. If you want the current LTS version, don't include any parameter.

Don't forget to add the path to your DOTNET_ROOT and PATH environment variables. See here.

Assuming you're using Bash, open ~/.bashrc and add

export DOTNET_ROOT=$HOME/.dotnet
export PATH=$PATH:$DOTNET_ROOT:$DOTNET_ROOT/tools

You might need to execute touch ~/.bashrc or start a new terminal window. Then you can now do dotnet --info and it should give you the .NET info.

To install the .NET MAUI workload run: dotnet workload install maui-android.

At the time of writing the out-of-the-box .NET MAUI project template doesn't build on Linux. This is because it tries to build the iOS and macOS targets as well. Fix it like this:

  1. Open your csproj file and search for the <TargetFrameworks> tag. It will have 3 entries like net7.0-android;net7.0-ios;net7.0-maccatalyst. There is also one that has a Condition attribute on it for Windows, you can leave that one as is.

  2. Change the <TargetFrameworks> tags to look like this:

<TargetFrameworks>net7.0-android</TargetFrameworks>
<TargetFrameworks Condition="!$([MSBuild]::IsOSPlatform('linux'))">$(TargetFrameworks);net7.0-ios;net7.0-maccatalyst</TargetFrameworks>
<TargetFrameworks Condition="$([MSBuild]::IsOSPlatform('windows'))">$(TargetFrameworks);net7.0-windows10.0.19041.0</TargetFrameworks>

Note that where it says net7.0 it could also say net6.0 or net8.0 depending on the version that you're running.

huangapple
  • 本文由 发表于 2023年7月13日 19:56:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/76679107.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定