[英]“Response has already started” exception in a bare-bones ASP.NET Core middleware project
[英]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應該用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多行代碼就把我的同事吹走了,但是當我們查看文件大小時,它並沒有那么令人印象深刻......
目前所有這些依賴項,我的WebAPI編譯為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,因此您可以將其用作“工作項目”。
例如, 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。
當然,您可能不希望在更大的項目上執行此操作。 在這個項目上只需要一分鍾左右。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.