zoukankan      html  css  js  c++  java
  • .net 5 单文件模式发布异常 CodeBase is not supported on assemblies loaded from a single-file bundle

    异常问题:
    System.NotSupportedException: CodeBase is not supported on assemblies loaded from a single-file bundle.
    at System.Reflection.RuntimeAssembly.get_CodeBase()
    at Microsoft.Extensions.DependencyModel.DependencyContextLoader.GetNormalizedCodeBasePath(Assembly assembly)
    at Microsoft.Extensions.DependencyModel.DependencyContextLoader.GetDepsJsonPath(Assembly assembly)
    at Microsoft.Extensions.DependencyModel.DependencyContextLoader.LoadAssemblyContext(Assembly assembly, IDependencyContextReader reader)
    at Microsoft.Extensions.DependencyModel.DependencyContextLoader.Load(Assembly assembly)
    at Microsoft.Extensions.DependencyModel.DependencyContext.Load(Assembly assembly)
    at Microsoft.AspNetCore.Mvc.ApplicationParts.AssemblyPartExtensions.GetReferencePaths(AssemblyPart assemblyPart)
    at Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation.RazorReferenceManager.GetReferencePaths()
    at Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation.RazorReferenceManager.GetCompilationReferences()
    at System.Threading.LazyInitializer.EnsureInitializedCore[T](T& target, Boolean& initialized, Object& syncLock, Func1 valueFactory) at System.Threading.LazyInitializer.EnsureInitialized[T](T& target, Boolean& initialized, Object& syncLock, Func1 valueFactory)
    at Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation.RazorReferenceManager.get_CompilationReferences()
    at Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation.LazyMetadataReferenceFeature.get_References()
    at Microsoft.CodeAnalysis.Razor.CompilationTagHelperFeature.GetDescriptors()
    at Microsoft.AspNetCore.Razor.Language.DefaultRazorTagHelperBinderPhase.ExecuteCore(RazorCodeDocument codeDocument)
    at Microsoft.AspNetCore.Razor.Language.RazorEnginePhaseBase.Execute(RazorCodeDocument codeDocument)
    at Microsoft.AspNetCore.Razor.Language.DefaultRazorEngine.Process(RazorCodeDocument document)
    at Microsoft.AspNetCore.Razor.Language.DefaultRazorProjectEngine.ProcessCore(RazorCodeDocument codeDocument)
    at Microsoft.AspNetCore.Razor.Language.RazorProjectEngine.Process(RazorProjectItem projectItem)
    at Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation.RuntimeViewCompiler.CompileAndEmit(String relativePath)
    at Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation.RuntimeViewCompiler.OnCacheMiss(String normalizedPath)
    --- End of stack trace from previous location ---
    at Microsoft.AspNetCore.Mvc.Razor.Compilation.DefaultRazorPageFactoryProvider.CreateFactory(String relativePath)
    at Microsoft.AspNetCore.Mvc.Razor.RazorViewEngine.CreateCacheResult(HashSet`1 expirationTokens, String relativePath, Boolean isMainPage)
    at Microsoft.AspNetCore.Mvc.Razor.RazorViewEngine.OnCacheMiss(ViewLocationExpanderContext expanderContext, ViewLocationCacheKey cacheKey)
    at Microsoft.AspNetCore.Mvc.Razor.RazorViewEngine.LocatePageFromViewLocations(ActionContext actionContext, String pageName, Boolean isMainPage)
    at Microsoft.AspNetCore.Mvc.Razor.RazorViewEngine.FindView(ActionContext context, String viewName, Boolean isMainPage)
    at Microsoft.AspNetCore.Mvc.ViewEngines.CompositeViewEngine.FindView(ActionContext context, String viewName, Boolean isMainPage)
    at Microsoft.AspNetCore.Mvc.ViewFeatures.ViewResultExecutor.FindView(ActionContext actionContext, ViewResult viewResult)
    at Microsoft.AspNetCore.Mvc.ViewFeatures.ViewResultExecutor.ExecuteAsync(ActionContext context, ViewResult result)
    at Microsoft.AspNetCore.Mvc.ViewResult.ExecuteResultAsync(ActionContext context)
    at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.g__Awaited|29_0[TFilter,TFilterAsync](ResourceInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)

    原因见微软文件描述:
    因为单文件程序集都合并为一个了,所以有一些路径相关的API均无法支持

    API Previous versions .NET 5.0 and later
    Assembly.Location Returns extracted DLL file path Returns empty string for bundled assemblies
    Assembly.CodeBase Returns extracted DLL file path Throws exception for bundled assemblies
    Assembly.GetFile(String) Returns null for bundled assemblies Throws exception for bundled assemblies
    Environment.GetCommandLineArgs()[0] Value is the name of the entry point DLL Value is the name of the host executable
    AppContext.BaseDirectory Value is the temporary extraction directory Value is the containing directory of the host executable
    详细见: https://docs.microsoft.com/en-us/dotnet/core/compatibility/3.1-5.0#assembly-related-api-behavior-changes-for-single-file-publishing-format

    解决方案:
    逐个将上述涉及的代码地方变更,确保所有3.x的组件全部升级到5.0

    参考:https://github.com/dotnet/runtime/pull/42306

    另外从dotnet core3.1 升级到 5.0需要注意的点
    Breaking changes for migration from version 3.1 to 5.0
    11/03/2020
    136 minutes to read

    If you're migrating from version 3.1 of .NET Core, ASP.NET Core, or EF Core to version 5.0 of .NET, ASP.NET Core, or EF Core, the breaking changes listed in this article may affect your app.

    ASP.NET Core

    • ASP.NET Core apps allow deserializing quoted numbers
    • Authentication: AzureAD.UI and AzureADB2C.UI APIs and packages marked obsolete
    • Authorization: Resource in endpoint routing is HttpContext
    • Azure: Microsoft-prefixed Azure integration packages removed
    • Blazor: Insignificant whitespace trimmed from components at compile time
    • Blazor: JSObjectReference and JSInProcessObjectReference types changed to internal
    • Blazor: ProtectedBrowserStorage feature moved to shared framework
    • Blazor: RenderTreeFrame readonly public fields have become properties
    • Blazor: Target framework of NuGet packages changed
    • Blazor: Updated browser support
    • Blazor: Updated validation logic for static web assets
    • Extensions: Package reference changes affecting some NuGet packages
    • HTTP: HttpClient instances created by IHttpClientFactory log integer status codes
    • HTTP: Kestrel and IIS BadHttpRequestException types marked obsolete and replaced
    • HttpSys: Client certificate renegotiation disabled by default
    • IIS: UrlRewrite middleware query strings are preserved
    • Kestrel: Configuration changes at run time detected by default
    • Kestrel: Default supported TLS protocol versions changed
    • Kestrel: HTTP/2 disabled over TLS on incompatible Windows versions
    • Kestrel: Libuv transport marked as obsolete
    • Localization: "Pubternal" APIs removed
    • Localization: Obsolete constructor removed in request localization middleware
    • Localization: ResourceManagerWithCultureStringLocalizer class and WithCulture interface member removed
    • Middleware: Database error page marked as obsolete
    • Middleware: Exception Handler Middleware throws original exception if handler not found
    • MVC: ObjectModelValidator calls a new overload of ValidationVisitor.Validate
    • Security: Cookie name encoding removed
    • Security: IdentityModel NuGet package versions updated
    • SignalR: MessagePack Hub Protocol moved to MessagePack 2.x package
    • SignalR: MessagePack Hub Protocol options type changed
    • SignalR: UseSignalR and UseConnections methods removed
    • Static files: CSV content type changed to standards-compliant

    https://docs.microsoft.com/en-us/dotnet/core/compatibility/3.1-5.0

    我这个异常的原因:
    .csproj中手动加了一条 false , 需要删去即可。

  • 相关阅读:
    C++对象模型与内存位对齐的简单分析(GNU GCC&VS2015编译器)
    [GeekBand] C++学习笔记(2)——BigThree、OOP
    [GeekBand] C++ 高级编程技术 (1)
    [GeekBand]C++高级编程技术(2)
    C++中引用的本质分析
    函数的重载(1)
    C++的特点
    布尔类型和三目运算符
    Linux客户端下的latex相关操作
    无光驱上网本上安装win7
  • 原文地址:https://www.cnblogs.com/mcxie/p/13997179.html
Copyright © 2011-2022 走看看