dotnet-exec 0.11.0 released

dotnet-exec 0.11.0 released

Intro

dotnet-exec 是一个 C# 程序的小工具,可以用来运行一些简单的 C# 程序而无需创建项目文件,让 C# 像 python/nodejs 一样简单,而且可以自定义项目的入口方法,支持但不限于 Main 方法。

Install/Update

dotnet-exec 是一个 dotnet tool,可以使用安装 dotnet tool 的命令来安装

安装/更新最新稳定版本:

dotnet tool update -g dotnet-execute

安装最新的 preview 版本:

dotnet tool update -g dotnet-execute --prerelease

执行 dotnet-exec -h 或者 dotnet-exec --help 即可看到一些使用说明

cc8e88ca05a24d313f38159103c9ddab.png

Features

Static using && using alias

在之前版本中对于 script,我们是不支持 static using 和 using alias 的,只支持 code 中使用,在 0.10.0 版本中我们支持了在 script 中使用 static using 和 using 的别名了

举个栗子

dotnet-exec 'MyConsole.WriteLine(PI)' --using 'MyConsole=System.Console' --using static 'System.Math'

8a1d696a833875d78ec66c2aca72221d.png

实现原理其实也比较简单,script 默认的 import 选项只能引入普通的命名空间,于是我们曲线救国,把 using 作为代码先执行了一下,之后再执行我们的 script 代码,在同一个上下文中会记住之前的 using 信息

6dc1c062b992ae7d8ada992cd86a1246.png

具体实现代码可以参考:https://github.com/WeihanLi/dotnet-exec/blob/2b2d2d4d47da5561001fb9f172bea65a8daa0932/src/dotnet-exec/CSharpScriptingExecutor.cs#L50-L52

Execute without SDK

在之前的版本中我们进行编译的时候始终会选择去使用引用程序集进行编译,在没有 SDK 的环境里会尝试从 nuget 上下载引用程序集进行编译

在 0.10.0 版本中,我们默认会使用 runtime 的程序集进行编译,这样即使没有网络,没有 SDK,只有 runtime 依然是可以工作的,当然你仍然可以使用 --ref-compile 选项来指定始终使用引用程序集来编译

Execute without web

在之前的版本中我们的 docker 镜像使用的是 aspnet  的镜像,考虑很多场景可能用不到 web 框架,所以从 0.11.0 版本开始默认不会再引用 web 框架引用,当然你也可以使用 -w/--web 来显式添加 web 框架引用

对于 docker 镜像,新的 latest tag 的镜像的基础镜像换成了 runtime,另外单独提供了一个 web tag 的镜像基础镜像是 aspnet,这样我们的镜像就能小很多了

但是即使你使用的是 latest 指定了使用 web 框架引用那你仍然可以运行,只是本地没有 aspnet runtime 的时候会尝试从 nuget 下载,而 web 则内置了 runtime 不需要再去下载,来个例子

首先我们可以来看一下最新的 docker 镜像中只有一个 .NET Core runtime 的信息,没有 aspnet 的 runtime 了

3ebed65c5e1ab53bf2cbb412a42253dc.png

我们用这个镜像来跑一个 web api 项目

docker run --rm --pull=always weihanli/dotnet-exec:latest dotnet-exec 'WebApplication.Create().Run();' --web

7bc6747e98c93b0ca802268260c38662.png

从上面的截图可以看到我们的 webapi 已经跑起来了,我们可以进一步使用暴露一个端口,请求一下我们的 API 试一下,让我们稍加改造

docker run --rm --pull=always -p 5000:80 weihanli/dotnet-exec:latest dotnet-exec 'WebApplication.Create().Chain(_ => _.MapRuntimeInfo()).Run();' --web --reference "nuget:WeihanLi.Web.Extensions" --using 'WeihanLi.Web.Extensions'

584c23335f934effb9969c1e1189e6b2.png

这里引用了一个我自己封装的一个扩展,会注册一个 runtime-info 的 endpoint,我这里使用的是 Github 的 CodeSpaces,会自动暴露一个 endpoint 来访问我们的服务,我们可以访问我们的 runtime-info endpoint 会看到一个类似下面的 response

61deef5e6869a0d189016d35a66cce9a.png

我们也可以使用 curl 或者可以使用 dotnet-httpie 来访问这个 endpoint

dotnet-http :5000/runtime-info

4990888e01f8f1a95eab155bfaca4998.png

ProjectReference

在 0.11.0 版本中我们增加项目引用的支持,实现原理是尝试 build 项目,引用 build 之后的 dll

使用方式如下:

dotnet-exec 'ApplicationHelper.GetLibraryInfo(typeof(CsvHelper))' --reference 'project:C:\projects\sources\WeihanLi.Npoi\src\WeihanLi.Npoi\WeihanLi.Npoi.csproj' --using 'WeihanLi.Npoi'

fa44465be0046b07a44b77b2b1549037.png

和引用 nuget 的效果基本一致

c7a9c8c2ef0eed89a44a61924739e999.png

ApplicationHelper.GetLibraryInfo 是从 assembly 信息中获取信息封装的一个方法,可以参考:https://github.com/WeihanLi/WeihanLi.Common/blob/d2db73a0e02cef009dc61190a41263ad6cb2b6bc/src/WeihanLi.Common/Helpers/ApplicationHelper.cs#L28

More

原来引用本地的 dll 需要指定一个绝对路径(full path),在 0.11.0 版本中我们支持了相对路径,使用起来也是更加的简单,新增加的项目引用也是支持相对路径的

上面的更新包含了 0.10.0 版本和 0.11.0 版本,具体更新可以参考 Github,代码变更可以参考:https://github.com/WeihanLi/dotnet-exec/compare/0.9.0...0.11.0

References

  • https://github.com/WeihanLi/dotnet-exec

  • https://www.nuget.org/packages/dotnet-execute/

  • https://hub.docker.com/r/weihanli/dotnet-exec

  • https://github.com/WeihanLi/dotnet-exec/compare/0.9.0...0.11.0

  • https://github.com/WeihanLi/WeihanLi.Common/blob/d2db73a0e02cef009dc61190a41263ad6cb2b6bc/src/WeihanLi.Common/Helpers/ApplicationHelper.cs#L28

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值