我创建了基于ASP.NET Core 3预览版6的服务器,并尝试在基于Linux的docker中运行它。任何请求都会收到错误:“ System.TypeLoadException:无法从程序集“ Microsoft.AspNetCore.WebUtilities,版本= 3.0.0.0,区域性=中性,PublicKeyToken = adb9793829ddae60”中加载类型“ Microsoft.AspNetCore.WebUtilities.FileBufferingWriteStream”。
在本地计算机(Windows Server 2012)上运行Kestrel时,一切正常。除了AspNetCore和NewtonsoftJson外,我不使用任何库。该项目是使用Controller API模板创建的。
Project.csproj:
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>netcoreapp3.0</TargetFramework>
<DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
<UserSecretsId>2bd64d79-ff97-4e47-8cbd-514aa4874416</UserSecretsId>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="3.0.0-preview6.19307.2" />
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.7.12" />
<FrameworkReference Include="Microsoft.AspNetCore.App" />
</ItemGroup>
</Project>
Dockerfile:
FROM mcr.microsoft.com/dotnet/core/aspnet:3.0-stretch-slim AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443
FROM mcr.microsoft.com/dotnet/core/sdk:3.0-stretch AS build
WORKDIR /src
COPY ["DockerTest/DockerTest.csproj", "DockerTest/"]
RUN dotnet restore "DockerTest/DockerTest.csproj"
COPY . .
WORKDIR "/src/DockerTest"
RUN dotnet build "DockerTest.csproj" -c Release -o /app
FROM build AS publish
RUN dotnet publish "DockerTest.csproj" -c Release -o /app
FROM base AS final
WORKDIR /app
COPY --from=publish /app .
ENTRYPOINT ["dotnet", "DockerTest.dll"]
控制器:
using Microsoft.AspNetCore.Mvc;
namespace DockerTest.Controllers
{
[Route("api/[action]")]
[ApiController]
public class ValuesController : ControllerBase
{
// GET api/values
[HttpGet]
public JsonResult Get()
{
return new JsonResult(new string[] { "value1", "value2" });
}
// GET api/values/5
[HttpGet("{id}")]
public ActionResult<string> Get(int id)
{
return "value";
}
// POST api/values
[HttpPost]
public void Post([FromBody] string value)
{
}
// PUT api/values/5
[HttpPut("{id}")]
public void Put(int id, [FromBody] string value)
{
}
// DELETE api/values/5
[HttpDelete("{id}")]
public void Delete(int id)
{
}
}
}
Startup.cs:
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers()
.AddNewtonsoftJson();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
//app.UseHttpsRedirection();
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
}
错误:
Connection id "0HLOBGRFHMARF", Request id "0HLOBGRFHMARF:00000001": An unhandled exception was thrown by the application.
System.TypeLoadException: Could not load type 'Microsoft.AspNetCore.WebUtilities.FileBufferingWriteStream' from assembly 'Microsoft.AspNetCore.WebUtilities, Version=3.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60'.
at Microsoft.AspNetCore.Mvc.NewtonsoftJson.NewtonsoftJsonResultExecutor.ExecuteAsync(ActionContext context, JsonResult result)
at System.Runtime.CompilerServices.AsyncMethodBuilderCore.Start[TStateMachine](TStateMachine& stateMachine)
at Microsoft.AspNetCore.Mvc.NewtonsoftJson.NewtonsoftJsonResultExecutor.ExecuteAsync(ActionContext context, JsonResult result)
at Microsoft.AspNetCore.Mvc.JsonResult.ExecuteResultAsync(ActionContext context)
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.InvokeResultAsync(IActionResult result)
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.InvokeNextResultFilterAsync[TFilter,TFilterAsync]()
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Rethrow(ResultExecutedContext context)
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.ResultNext[TFilter,TFilterAsync](State& next, Scope& scope, Object& state, Boolean& isCompleted)
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.InvokeResultFilters()
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.InvokeFilterPipelineAsync()
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.InvokeAsync()
at Microsoft.AspNetCore.Routing.EndpointMiddleware.Invoke(HttpContext httpContext)
at Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.Invoke(HttpContext context)
at Microsoft.AspNetCore.Routing.EndpointRoutingMiddleware.Invoke(HttpContext httpContext)
at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpProtocol.ProcessRequests[TContext](IHttpApplication`1 application)
我试图基于dotnet / core / aspnet:3.0.0-preview6启动一个docker,但是我没有发现任何变化。
据我了解,一些库已从asp.net Core 3中删除,包括Microsoft.AspNetCore.WebUtilities。
我什至加入了项目,但再次没有注意到差异
答案 0 :(得分:0)
对于有效的演示,请尝试更改dockerfile,以将mcr.microsoft.com/dotnet/core/aspnet:3.0-stretch-slim
替换为mcr.microsoft.com/dotnet/core/aspnet:3.0-buster-slim
,将mcr.microsoft.com/dotnet/core/sdk:3.0-stretch
替换为mcr.microsoft.com/dotnet/core/sdk:3.0-buster
。
FROM mcr.microsoft.com/dotnet/core/aspnet:3.0-buster-slim AS base
#FROM mcr.microsoft.com/dotnet/core/aspnet:3.0-stretch-slim AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443
FROM mcr.microsoft.com/dotnet/core/sdk:3.0-buster AS build
#FROM mcr.microsoft.com/dotnet/core/sdk:3.0-stretch AS build
WORKDIR /src