最小足迹/准系统 ASP.NET 核心 WebAPI
Minimal Footprint / Bare-bones ASP.NET Core WebAPI
只是为了好玩,今天早些时候我的一位同事问我是否可以尝试使用 ASP.NET Core 制作一个响应请求的占用空间很小的 WebAPI。 我用大约 70 行代码就完成了 WebAPI。多亏了 ASP.NET Core 太棒了! 所以,这就是我到目前为止的结果。
代码
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Newtonsoft.Json;
using System;
using System.Linq;
namespace TinyWebApi
{
class Program
{
static readonly IWebHost _host;
static readonly string[] _urls = { "http://localhost:80" };
static Program()
{
IConfiguration _configuration = new ConfigurationBuilder()
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
.Build();
_host = BuildHost(new WebHostBuilder(), _configuration, _urls);
}
static void Main(string[] args)
{
_host.Run();
}
static IWebHost BuildHost(
IWebHostBuilder builder, IConfiguration configuration, params string[] urls)
{
return builder
.UseKestrel(options =>
{
options.NoDelay = true;
})
.UseConfiguration(configuration)
.UseUrls(urls)
.Configure(app =>
{
app.Map("/echo", EchoHandler);
})
.Build();
}
static void EchoHandler(IApplicationBuilder app)
{
app.Run(async context =>
{
await context.Response.WriteAsync(
JsonConvert.SerializeObject(new
{
StatusCode = (string)context.Response.StatusCode.ToString(),
PathBase = (string)context.Request.PathBase.Value.Trim('/'),
Path = (string)context.Request.Path.Value.Trim('/'),
Method = (string)context.Request.Method,
Scheme = (string)context.Request.Scheme,
ContentType = (string)context.Request.ContentType,
ContentLength = (long?)context.Request.ContentLength,
QueryString = (string)context.Request.QueryString.ToString(),
Query = context.Request.Query
.ToDictionary(
_ => _.Key,
_ => _.Value,
StringComparer.OrdinalIgnoreCase)
})
);
});
}
}
}
(上面的代码按预期工作,没有损坏。)
WebAPI
WebAPI 应该用 JSON.
回显请求
示例请求
http://localhost/echo?q=foo&q=bar
示例响应
{
"StatusCode": "200",
"PathBase": "echo",
"Path": "",
"Method": "GET",
"Scheme": "http",
"ContentType": null,
"ContentLength": null,
"QueryString": "?q=foo&q=bar",
"Query": {
"q": [
"foo",
"bar"
]
}
}
我的问题
我仅用 70 多行代码就让我的同事大吃一惊,但是当我们查看文件大小时,它并没有那么令人印象深刻...
At the moment with all these dependencies, my WebAPI is compiling to 54.3MB.
我一直在想办法减少这个项目的磁盘占用。我安装的软件包捆绑了很多我们并不真正需要的东西,我一直在 运行 遇到麻烦,试图找出最好的资源或方法来删除这个项目不需要的引用.
.csproj
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp1.1</TargetFramework>
<RuntimeIdentifier>win7-x64</RuntimeIdentifier>
<ApplicationIcon>Icon.ico</ApplicationIcon>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore" Version="1.1.2" />
<PackageReference Include="Microsoft.AspNetCore.Hosting.Abstractions" Version="1.1.2" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Core" Version="1.1.3" />
<PackageReference Include="Microsoft.AspNetCore.Server.Kestrel" Version="1.1.2" />
<PackageReference Include="Microsoft.Extensions.Configuration.FileExtensions" Version="1.1.2" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="1.1.2" />
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="1.1.2" />
<PackageReference Include="Newtonsoft.Json" Version="10.0.2" />
</ItemGroup>
</Project>
有没有什么方法可以让我根据提供的代码快速了解我的项目需要哪些引用?我早些时候开始清除所有上述依赖项,然后尝试添加他们一个接一个,但事实证明,试图解决丢失的参考文献是一个永无止境的头痛,而且似乎需要永远解决。我敢肯定有人有这样的解决方案,但我似乎无法找到它。谢谢。
我所做的只是将代码复制到一个新的 .NET Core 控制台项目并解决了缺少的引用。为了弄清楚你需要为缺失的 API 添加哪些,我刚刚在缺失的成员上执行了 F12(转到定义) 在工作项目 中查看所有参考在哪个程序集中定义了 API。
因为你在这里没有使用任何花哨的东西,所以 VS 中的 ASP.NET Core Web application
模板已经使用了所有这些 API,所以你可以将它用作 'working project' .
例如 AddJsonFile
是在 Microsoft.Extensions.Configuration.Json
包中定义的。
所以最后我只剩下
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp1.1</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Server.Kestrel" Version="1.1.2" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="1.1.2" />
<PackageReference Include="Newtonsoft.Json" Version="10.0.2" />
</ItemGroup>
</Project>
发布时总计 2.41MB。
当然,您可能不想在更大的项目上这样做。
在这个项目上只需要一分钟左右。
只是为了好玩,今天早些时候我的一位同事问我是否可以尝试使用 ASP.NET Core 制作一个响应请求的占用空间很小的 WebAPI。 我用大约 70 行代码就完成了 WebAPI。多亏了 ASP.NET Core 太棒了! 所以,这就是我到目前为止的结果。
代码
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Newtonsoft.Json;
using System;
using System.Linq;
namespace TinyWebApi
{
class Program
{
static readonly IWebHost _host;
static readonly string[] _urls = { "http://localhost:80" };
static Program()
{
IConfiguration _configuration = new ConfigurationBuilder()
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
.Build();
_host = BuildHost(new WebHostBuilder(), _configuration, _urls);
}
static void Main(string[] args)
{
_host.Run();
}
static IWebHost BuildHost(
IWebHostBuilder builder, IConfiguration configuration, params string[] urls)
{
return builder
.UseKestrel(options =>
{
options.NoDelay = true;
})
.UseConfiguration(configuration)
.UseUrls(urls)
.Configure(app =>
{
app.Map("/echo", EchoHandler);
})
.Build();
}
static void EchoHandler(IApplicationBuilder app)
{
app.Run(async context =>
{
await context.Response.WriteAsync(
JsonConvert.SerializeObject(new
{
StatusCode = (string)context.Response.StatusCode.ToString(),
PathBase = (string)context.Request.PathBase.Value.Trim('/'),
Path = (string)context.Request.Path.Value.Trim('/'),
Method = (string)context.Request.Method,
Scheme = (string)context.Request.Scheme,
ContentType = (string)context.Request.ContentType,
ContentLength = (long?)context.Request.ContentLength,
QueryString = (string)context.Request.QueryString.ToString(),
Query = context.Request.Query
.ToDictionary(
_ => _.Key,
_ => _.Value,
StringComparer.OrdinalIgnoreCase)
})
);
});
}
}
}
(上面的代码按预期工作,没有损坏。)
WebAPI
WebAPI 应该用 JSON.
回显请求示例请求
http://localhost/echo?q=foo&q=bar
示例响应
{
"StatusCode": "200",
"PathBase": "echo",
"Path": "",
"Method": "GET",
"Scheme": "http",
"ContentType": null,
"ContentLength": null,
"QueryString": "?q=foo&q=bar",
"Query": {
"q": [
"foo",
"bar"
]
}
}
我的问题
我仅用 70 多行代码就让我的同事大吃一惊,但是当我们查看文件大小时,它并没有那么令人印象深刻...
At the moment with all these dependencies, my WebAPI is compiling to 54.3MB.
我一直在想办法减少这个项目的磁盘占用。我安装的软件包捆绑了很多我们并不真正需要的东西,我一直在 运行 遇到麻烦,试图找出最好的资源或方法来删除这个项目不需要的引用.
.csproj
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp1.1</TargetFramework>
<RuntimeIdentifier>win7-x64</RuntimeIdentifier>
<ApplicationIcon>Icon.ico</ApplicationIcon>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore" Version="1.1.2" />
<PackageReference Include="Microsoft.AspNetCore.Hosting.Abstractions" Version="1.1.2" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Core" Version="1.1.3" />
<PackageReference Include="Microsoft.AspNetCore.Server.Kestrel" Version="1.1.2" />
<PackageReference Include="Microsoft.Extensions.Configuration.FileExtensions" Version="1.1.2" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="1.1.2" />
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="1.1.2" />
<PackageReference Include="Newtonsoft.Json" Version="10.0.2" />
</ItemGroup>
</Project>
有没有什么方法可以让我根据提供的代码快速了解我的项目需要哪些引用?我早些时候开始清除所有上述依赖项,然后尝试添加他们一个接一个,但事实证明,试图解决丢失的参考文献是一个永无止境的头痛,而且似乎需要永远解决。我敢肯定有人有这样的解决方案,但我似乎无法找到它。谢谢。
我所做的只是将代码复制到一个新的 .NET Core 控制台项目并解决了缺少的引用。为了弄清楚你需要为缺失的 API 添加哪些,我刚刚在缺失的成员上执行了 F12(转到定义) 在工作项目 中查看所有参考在哪个程序集中定义了 API。
因为你在这里没有使用任何花哨的东西,所以 VS 中的 ASP.NET Core Web application
模板已经使用了所有这些 API,所以你可以将它用作 'working project' .
例如 AddJsonFile
是在 Microsoft.Extensions.Configuration.Json
包中定义的。
所以最后我只剩下
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp1.1</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Server.Kestrel" Version="1.1.2" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="1.1.2" />
<PackageReference Include="Newtonsoft.Json" Version="10.0.2" />
</ItemGroup>
</Project>
发布时总计 2.41MB。
当然,您可能不想在更大的项目上这样做。 在这个项目上只需要一分钟左右。